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;
528 case TELOPT_XDISPLOC:
529 case TELOPT_NEW_ENVIRON:
530 case TELOPT_OLD_ENVIRON:
535 case TELOPT_LINEMODE:
536 # ifdef KLUDGELINEMODE
538 * Note client's desire to use linemode.
540 lmodetype = REAL_LINEMODE;
541 # endif /* KLUDGELINEMODE */
545 #endif /* LINEMODE */
547 #ifdef AUTHENTICATION
548 case TELOPT_AUTHENTICATION:
556 func = encrypt_send_support;
559 #endif /* ENCRYPTION */
565 set_his_want_state_will(option);
568 do_dont_resp[option]++;
569 send_dont(option, 0);
573 * Option processing that should happen when
574 * we receive conformation of a change in
575 * state that we had requested.
579 not42 = 0; /* looks like a 4.2 system */
581 * Egads, he responded "WILL ECHO". Turn
584 send_dont(option, 1);
586 * "WILL ECHO". Kludge upon kludge!
587 * A 4.2 client is now echoing user input at
588 * the tty. This is probably undesireable and
589 * it should be stopped. The client will
590 * respond WONT TM to the DO TM that we send to
591 * check for kludge linemode. When the WONT TM
592 * arrives, linemode will be turned off and a
593 * change propogated to the pty. This change
594 * will cause us to process the new pty state
595 * in localstat(), which will notice that
596 * linemode is off and send a WILL ECHO
597 * so that we are properly in character mode and
602 case TELOPT_LINEMODE:
603 # ifdef KLUDGELINEMODE
605 * Note client's desire to use linemode.
607 lmodetype = REAL_LINEMODE;
608 # endif /* KLUDGELINEMODE */
611 #endif /* LINEMODE */
613 #ifdef AUTHENTICATION
614 case TELOPT_AUTHENTICATION:
621 func = encrypt_send_support;
623 #endif /* ENCRYPTION */
630 set_his_state_will(option);
633 } /* end of willoption */
636 send_dont(int option, int init)
639 if ((do_dont_resp[option] == 0 && his_state_is_wont(option)) ||
640 his_want_state_is_wont(option))
642 set_his_want_state_wont(option);
643 do_dont_resp[option]++;
645 output_data((const char *)dont, option);
647 DIAG(TD_OPTIONS, printoption("td: send dont", option));
651 wontoption(int option)
654 * Process client input.
657 DIAG(TD_OPTIONS, printoption("td: recv wont", option));
659 if (do_dont_resp[option]) {
660 do_dont_resp[option]--;
661 if (do_dont_resp[option] && his_state_is_wont(option))
662 do_dont_resp[option]--;
664 if (do_dont_resp[option] == 0) {
665 if (his_want_state_is_will(option)) {
666 /* it is always ok to change to negative state */
669 not42 = 1; /* doesn't seem to be a 4.2 system */
679 case TELOPT_LINEMODE:
680 # ifdef KLUDGELINEMODE
682 * If real linemode is supported, then client is
683 * asking to turn linemode off.
685 if (lmodetype != REAL_LINEMODE)
687 lmodetype = KLUDGE_LINEMODE;
688 # endif /* KLUDGELINEMODE */
689 clientstat(TELOPT_LINEMODE, WONT, 0);
691 #endif /* LINEMODE */
695 * If we get a WONT TM, and had sent a DO TM,
696 * don't respond with a DONT TM, just leave it
697 * as is. Short circut the state machine to
700 set_his_want_state_wont(TELOPT_TM);
705 * If we are not going to support flow control
706 * option, then let peer know that we can't
707 * change the flow control characters.
709 slctab[SLC_XON].defset.flag &= ~SLC_LEVELBITS;
710 slctab[SLC_XON].defset.flag |= SLC_CANTCHANGE;
711 slctab[SLC_XOFF].defset.flag &= ~SLC_LEVELBITS;
712 slctab[SLC_XOFF].defset.flag |= SLC_CANTCHANGE;
715 #ifdef AUTHENTICATION
716 case TELOPT_AUTHENTICATION:
717 auth_finished(0, AUTH_REJECT);
722 * For options that we might spin waiting for
723 * sub-negotiation, if the client turns off the
724 * option rather than responding to the request,
725 * we have to treat it here as if we got a response
726 * to the sub-negotiation, (by updating the timers)
727 * so that we'll break out of the loop.
730 settimer(ttypesubopt);
734 settimer(tspeedsubopt);
737 case TELOPT_XDISPLOC:
738 settimer(xdisplocsubopt);
741 case TELOPT_OLD_ENVIRON:
742 settimer(oenvironsubopt);
745 case TELOPT_NEW_ENVIRON:
746 settimer(environsubopt);
752 set_his_want_state_wont(option);
753 if (his_state_is_will(option))
754 send_dont(option, 0);
758 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
759 if (lmodetype < NO_AUTOKLUDGE) {
760 lmodetype = NO_LINEMODE;
761 clientstat(TELOPT_LINEMODE, WONT, 0);
762 send_will(TELOPT_SGA, 1);
763 send_will(TELOPT_ECHO, 1);
765 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
768 #ifdef AUTHENTICATION
769 case TELOPT_AUTHENTICATION:
770 auth_finished(0, AUTH_REJECT);
778 set_his_state_wont(option);
780 } /* end of wontoption */
783 send_will(int option, int init)
786 if ((will_wont_resp[option] == 0 && my_state_is_will(option))||
787 my_want_state_is_will(option))
789 set_my_want_state_will(option);
790 will_wont_resp[option]++;
792 output_data((const char *)will, option);
794 DIAG(TD_OPTIONS, printoption("td: send will", option));
797 #if !defined(LINEMODE) || !defined(KLUDGELINEMODE)
799 * When we get a DONT SGA, we will try once to turn it
800 * back on. If the other side responds DONT SGA, we
801 * leave it at that. This is so that when we talk to
802 * clients that understand KLUDGELINEMODE but not LINEMODE,
803 * we'll keep them in char-at-a-time mode.
814 * Process client input.
817 DIAG(TD_OPTIONS, printoption("td: recv do", option));
819 if (will_wont_resp[option]) {
820 will_wont_resp[option]--;
821 if (will_wont_resp[option] && my_state_is_will(option))
822 will_wont_resp[option]--;
824 if ((will_wont_resp[option] == 0) && (my_want_state_is_wont(option))) {
828 # ifdef KLUDGELINEMODE
829 if (lmodetype == NO_LINEMODE)
831 if (his_state_is_wont(TELOPT_LINEMODE))
850 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
852 * If kludge linemode is in use, then we must
853 * process an incoming do SGA for linemode
856 if (lmodetype == KLUDGE_LINEMODE) {
858 * Receipt of "do SGA" in kludge
859 * linemode is the peer asking us to
860 * turn off linemode. Make note of
863 clientstat(TELOPT_LINEMODE, WONT, 0);
865 * If linemode did not get turned off
866 * then don't tell peer that we did.
867 * Breaking here forces a wont SGA to
875 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
885 * Special case for TM. We send a WILL, but
886 * pretend we sent a WONT.
888 send_will(option, 0);
889 set_my_want_state_wont(option);
890 set_my_state_wont(option);
895 * When we get a LOGOUT option, respond
896 * with a WILL LOGOUT, make sure that
897 * it gets written out to the network,
898 * and then just go away...
900 set_my_want_state_will(TELOPT_LOGOUT);
901 send_will(TELOPT_LOGOUT, 0);
902 set_my_state_will(TELOPT_LOGOUT);
912 #endif /* ENCRYPTION */
913 case TELOPT_LINEMODE:
918 case TELOPT_XDISPLOC:
919 #ifdef TELOPT_ENVIRON
920 case TELOPT_NEW_ENVIRON:
922 case TELOPT_OLD_ENVIRON:
927 set_my_want_state_will(option);
928 send_will(option, 0);
930 will_wont_resp[option]++;
931 send_wont(option, 0);
934 set_my_state_will(option);
936 } /* end of dooption */
939 send_wont(int option, int init)
942 if ((will_wont_resp[option] == 0 && my_state_is_wont(option)) ||
943 my_want_state_is_wont(option))
945 set_my_want_state_wont(option);
946 will_wont_resp[option]++;
948 output_data((const char *)wont, option);
950 DIAG(TD_OPTIONS, printoption("td: send wont", option));
954 dontoption(int option)
957 * Process client input.
961 DIAG(TD_OPTIONS, printoption("td: recv dont", option));
963 if (will_wont_resp[option]) {
964 will_wont_resp[option]--;
965 if (will_wont_resp[option] && my_state_is_wont(option))
966 will_wont_resp[option]--;
968 if ((will_wont_resp[option] == 0) && (my_want_state_is_will(option))) {
976 case TELOPT_ECHO: /* we should stop echoing */
978 # ifdef KLUDGELINEMODE
979 if ((lmodetype != REAL_LINEMODE) &&
980 (lmodetype != KLUDGE_LINEMODE))
982 if (his_state_is_wont(TELOPT_LINEMODE))
993 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
995 * If kludge linemode is in use, then we
996 * must process an incoming do SGA for
999 if ((lmodetype == KLUDGE_LINEMODE) ||
1000 (lmodetype == KLUDGE_OK)) {
1002 * The client is asking us to turn
1005 lmodetype = KLUDGE_LINEMODE;
1006 clientstat(TELOPT_LINEMODE, WILL, 0);
1008 * If we did not turn line mode on,
1009 * then what do we say? Will SGA?
1010 * This violates design of telnet.
1011 * Gross. Very Gross.
1016 set_my_want_state_wont(option);
1017 if (my_state_is_will(option))
1018 send_wont(option, 0);
1019 set_my_state_wont(option);
1020 if (turn_on_sga ^= 1)
1021 send_will(option, 1);
1023 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
1029 set_my_want_state_wont(option);
1030 if (my_state_is_will(option))
1031 send_wont(option, 0);
1033 set_my_state_wont(option);
1035 } /* end of dontoption */
1039 int env_ovalue = -1;
1040 #else /* ENV_HACK */
1041 # define env_ovar OLD_ENV_VAR
1042 # define env_ovalue OLD_ENV_VALUE
1043 #endif /* ENV_HACK */
1045 /* envvarok(char*) */
1046 /* check that variable is safe to pass to login or shell */
1048 envvarok(char *varp)
1051 if (strcmp(varp, "TERMCAP") && /* to prevent a security hole */
1052 strcmp(varp, "TERMINFO") && /* with tgetent */
1053 strcmp(varp, "TERMPATH") &&
1054 strcmp(varp, "HOME") && /* to prevent the tegetent bug */
1055 strncmp(varp, "LD_", strlen("LD_")) && /* most systems */
1056 strncmp(varp, "_RLD_", strlen("_RLD_")) && /* IRIX */
1057 strcmp(varp, "LIBPATH") && /* AIX */
1058 strcmp(varp, "ENV") &&
1059 strcmp(varp, "BASH_ENV") &&
1060 strcmp(varp, "IFS") &&
1061 strncmp(varp, "KRB5", strlen("KRB5")) && /* Krb5 */
1063 * The above case is a catch-all for now. Here are some of
1064 * the specific ones we must avoid passing, at least until
1065 * we can prove it can be done safely. Keep this list
1066 * around un case someone wants to remove the catch-all.
1068 strcmp(varp, "KRB5_CONFIG") && /* Krb5 */
1069 strcmp(varp, "KRB5CCNAME") && /* Krb5 */
1070 strcmp(varp, "KRB5_KTNAME") && /* Krb5 */
1071 strcmp(varp, "KRBTKFILE") && /* Krb4 */
1072 strcmp(varp, "KRB_CONF") && /* CNS 4 */
1073 strcmp(varp, "KRB_REALMS") && /* CNS 4 */
1074 strcmp(varp, "RESOLV_HOST_CONF")) /* Linux */
1077 syslog(LOG_INFO, "Rejected the attempt to modify the "
1078 "environment variable \"%s\"", varp);
1086 * Look at the sub-option buffer, and try to be helpful to the other
1089 * Currently we recognize:
1101 DIAG(TD_OPTIONS, {netflush(); printsub('<', subpointer, SB_LEN()+2);});
1105 case TELOPT_TSPEED: {
1108 if (his_state_is_wont(TELOPT_TSPEED)) /* Ignore if option disabled */
1111 settimer(tspeedsubopt);
1113 if (SB_EOF() || SB_GET() != TELQUAL_IS)
1116 xspeed = atoi((char *)subpointer);
1118 while (SB_GET() != ',' && !SB_EOF());
1122 rspeed = atoi((char *)subpointer);
1123 clientstat(TELOPT_TSPEED, xspeed, rspeed);
1127 } /* end of case TELOPT_TSPEED */
1129 case TELOPT_TTYPE: { /* Yaaaay! */
1130 static char terminalname[TERMINAL_TYPE_SIZE];
1132 if (his_state_is_wont(TELOPT_TTYPE)) /* Ignore if option disabled */
1134 settimer(ttypesubopt);
1136 if (SB_EOF() || SB_GET() != TELQUAL_IS) {
1137 return; /* ??? XXX but, this is the most robust */
1140 terminaltype = terminalname;
1142 while ((terminaltype < (terminalname + sizeof terminalname-1)) &&
1150 *terminaltype++ = c; /* accumulate name */
1153 terminaltype = terminalname;
1155 } /* end of case TELOPT_TTYPE */
1158 int xwinsize, ywinsize;
1160 if (his_state_is_wont(TELOPT_NAWS)) /* Ignore if option disabled */
1165 xwinsize = SB_GET() << 8;
1168 xwinsize |= SB_GET();
1171 ywinsize = SB_GET() << 8;
1174 ywinsize |= SB_GET();
1175 clientstat(TELOPT_NAWS, xwinsize, ywinsize);
1179 } /* end of case TELOPT_NAWS */
1182 case TELOPT_LINEMODE: {
1185 if (his_state_is_wont(TELOPT_LINEMODE)) /* Ignore if option disabled */
1188 * Process linemode suboptions.
1191 break; /* garbage was sent */
1192 request = SB_GET(); /* get will/wont */
1195 break; /* another garbage check */
1197 if (request == LM_SLC) { /* SLC is not preceeded by WILL or WONT */
1199 * Process suboption buffer of slc's
1202 do_opt_slc(subpointer, subend - subpointer);
1205 } else if (request == LM_MODE) {
1208 useeditmode = SB_GET(); /* get mode flag */
1209 clientstat(LM_MODE, 0, 0);
1215 switch (SB_GET()) { /* what suboption? */
1216 case LM_FORWARDMASK:
1218 * According to spec, only server can send request for
1219 * forwardmask, and client can only return a positive response.
1220 * So don't worry about it.
1227 } /* end of case TELOPT_LINEMODE */
1229 case TELOPT_STATUS: {
1237 if (my_state_is_will(TELOPT_STATUS))
1248 } /* end of case TELOPT_STATUS */
1250 case TELOPT_XDISPLOC: {
1251 if (SB_EOF() || SB_GET() != TELQUAL_IS)
1253 settimer(xdisplocsubopt);
1254 subpointer[SB_LEN()] = '\0';
1255 if (setenv("DISPLAY", (char *)subpointer, 1) == -1)
1256 syslog(LOG_ERR, "setenv: cannot set DISPLAY=%s: %m", (char *)subpointer);
1258 } /* end of case TELOPT_XDISPLOC */
1260 #ifdef TELOPT_NEW_ENVIRON
1261 case TELOPT_NEW_ENVIRON:
1263 case TELOPT_OLD_ENVIRON: {
1265 char *cp, *varp, *valp;
1270 if (c == TELQUAL_IS) {
1271 if (subchar == TELOPT_OLD_ENVIRON)
1272 settimer(oenvironsubopt);
1274 settimer(environsubopt);
1275 } else if (c != TELQUAL_INFO) {
1279 #ifdef TELOPT_NEW_ENVIRON
1280 if (subchar == TELOPT_NEW_ENVIRON) {
1283 if ((c == NEW_ENV_VAR) || (c == ENV_USERVAR))
1291 * We only want to do this if we haven't already decided
1292 * whether or not the other side has its VALUE and VAR
1296 int last = -1; /* invalid value */
1298 int got_var = 0, got_value = 0, got_uservar = 0;
1301 * The other side might have its VALUE and VAR values
1302 * reversed. To be interoperable, we need to determine
1303 * which way it is. If the first recognized character
1304 * is a VAR or VALUE, then that will tell us what
1305 * type of client it is. If the fist recognized
1306 * character is a USERVAR, then we continue scanning
1307 * the suboption looking for two consecutive
1308 * VAR or VALUE fields. We should not get two
1309 * consecutive VALUE fields, so finding two
1310 * consecutive VALUE or VAR fields will tell us
1311 * what the client is.
1318 if (last < 0 || last == OLD_ENV_VAR
1319 || (empty && (last == OLD_ENV_VALUE)))
1325 if (last < 0 || last == OLD_ENV_VALUE
1326 || (empty && (last == OLD_ENV_VAR)))
1327 goto env_ovar_wrong;
1329 last = OLD_ENV_VALUE;
1332 /* count strings of USERVAR as one */
1333 if (last != ENV_USERVAR)
1336 if (last == OLD_ENV_VALUE)
1338 if (last == OLD_ENV_VAR)
1339 goto env_ovar_wrong;
1354 if (last == OLD_ENV_VALUE)
1356 if (last == OLD_ENV_VAR)
1357 goto env_ovar_wrong;
1360 * Ok, the first thing was a USERVAR, and there
1361 * are not two consecutive VAR or VALUE commands,
1362 * and none of the VAR or VALUE commands are empty.
1363 * If the client has sent us a well-formed option,
1364 * then the number of VALUEs received should always
1365 * be less than or equal to the number of VARs and
1366 * USERVARs received.
1368 * If we got exactly as many VALUEs as VARs and
1369 * USERVARs, the client has the same definitions.
1371 * If we got exactly as many VARs as VALUEs and
1372 * USERVARS, the client has reversed definitions.
1374 if (got_uservar + got_var == got_value) {
1376 env_ovar = OLD_ENV_VAR;
1377 env_ovalue = OLD_ENV_VALUE;
1378 } else if (got_uservar + got_value == got_var) {
1380 env_ovar = OLD_ENV_VALUE;
1381 env_ovalue = OLD_ENV_VAR;
1383 output_data("ENVIRON VALUE and VAR are reversed!\r\n"));
1392 if ((c == env_ovar) || (c == ENV_USERVAR))
1400 cp = varp = (char *)subpointer;
1405 if (subchar == TELOPT_OLD_ENVIRON) {
1408 else if (c == env_ovalue)
1415 cp = valp = (char *)subpointer;
1421 if (envvarok(varp)) {
1423 if (setenv(varp, valp, 1) == -1)
1424 syslog(LOG_ERR, "setenv: cannot set %s=%s: %m", varp, valp);
1429 cp = varp = (char *)subpointer;
1444 if (envvarok(varp)) {
1446 if (setenv(varp, valp, 1) == -1)
1447 syslog(LOG_ERR, "setenv: cannot set %s=%s: %m", varp, valp);
1453 } /* end of case TELOPT_NEW_ENVIRON */
1454 #ifdef AUTHENTICATION
1455 case TELOPT_AUTHENTICATION:
1462 * These are sent by us and cannot be sent by
1467 auth_is(subpointer, SB_LEN());
1470 auth_name(subpointer, SB_LEN());
1476 case TELOPT_ENCRYPT:
1480 case ENCRYPT_SUPPORT:
1481 encrypt_support(subpointer, SB_LEN());
1484 encrypt_is(subpointer, SB_LEN());
1487 encrypt_reply(subpointer, SB_LEN());
1490 encrypt_start(subpointer, SB_LEN());
1495 case ENCRYPT_REQSTART:
1496 encrypt_request_start(subpointer, SB_LEN());
1498 case ENCRYPT_REQEND:
1500 * We can always send an REQEND so that we cannot
1501 * get stuck encrypting. We should only get this
1502 * if we have been able to get in the correct mode
1505 encrypt_request_end();
1507 case ENCRYPT_ENC_KEYID:
1508 encrypt_enc_keyid(subpointer, SB_LEN());
1510 case ENCRYPT_DEC_KEYID:
1511 encrypt_dec_keyid(subpointer, SB_LEN());
1517 #endif /* ENCRYPTION */
1521 } /* end of switch */
1523 } /* end of suboption */
1528 clientstat(TELOPT_LINEMODE, WILL, 0);
1531 #define ADD(c) *ncp++ = c
1532 #define ADD_DATA(c) { *ncp++ = c; if (c == SE || c == IAC) *ncp++ = c; }
1536 unsigned char statusbuf[256];
1542 netflush(); /* get rid of anything waiting to go out */
1550 * We check the want_state rather than the current state,
1551 * because if we received a DO/WILL for an option that we
1552 * don't support, and the other side didn't send a DONT/WONT
1553 * in response to our WONT/DONT, then the "state" will be
1554 * WILL/DO, and the "want_state" will be WONT/DONT. We
1555 * need to go by the latter.
1557 for (i = 0; i < (unsigned char)NTELOPTS; i++) {
1558 if (my_want_state_is_will(i)) {
1564 if (his_want_state_is_will(i)) {
1572 if (his_want_state_is_will(TELOPT_LFLOW)) {
1582 if (restartany >= 0) {
1586 ADD(LFLOW_RESTART_ANY);
1588 ADD(LFLOW_RESTART_XON);
1595 if (his_want_state_is_will(TELOPT_LINEMODE)) {
1596 unsigned char *cp, *cpe;
1600 ADD(TELOPT_LINEMODE);
1606 ADD(TELOPT_LINEMODE);
1611 for (cpe = cp + len; cp < cpe; cp++)
1615 #endif /* LINEMODE */
1620 output_datalen(statusbuf, ncp - statusbuf);
1621 netflush(); /* Send it on its way */
1624 {printsub('>', statusbuf, ncp - statusbuf); netflush();});
1628 * This function appends data to nfrontp and advances nfrontp.
1629 * Returns the number of characters written altogether (the
1630 * buffer may have been flushed in the process).
1634 output_data(const char *format, ...)
1640 va_start(args, format);
1641 if ((len = vasprintf(&buf, format, args)) == -1)
1643 output_datalen(buf, len);
1650 output_datalen(const char *buf, int len)
1652 int remaining, copied;
1654 remaining = BUFSIZ - (nfrontp - netobuf);
1656 /* Free up enough space if the room is too low*/
1657 if ((len > BUFSIZ ? BUFSIZ : len) > remaining) {
1659 remaining = BUFSIZ - (nfrontp - netobuf);
1662 /* Copy out as much as will fit */
1663 copied = remaining > len ? len : remaining;
1664 memmove(nfrontp, buf, copied);
1667 remaining -= copied;