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 };
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--;
100 c = (*decrypt_input)(c);
101 #endif /* ENCRYPTION */
106 /* Strip off \n or \0 after a \r */
107 if ((c == 0) || (c == '\n')) {
118 * We now map \r\n ==> \r for pragmatic reasons.
119 * Many client implementations send \r\n when
120 * the user hits the CarriageReturn key.
122 * We USED to map \r\n ==> \n, since \r\n says
123 * that we want to be in column 1 of the next
124 * printable line, and \n is the standard
125 * unix way of saying that (\r is only good
126 * if CRMOD is set, which it normally is).
128 if ((c == '\r') && his_state_is_wont(TELOPT_BINARY)) {
132 nc = (*decrypt_input)(nc & 0xff);
133 #endif /* ENCRYPTION */
136 * If we are operating in linemode,
137 * convert to local end-of-line.
139 if (linemode && (ncc > 0) && (('\n' == nc) ||
140 ((0 == nc) && tty_iscrnl())) ) {
148 (void)(*decrypt_input)(-1);
149 #endif /* ENCRYPTION */
160 * Send the process on the pty side an
161 * interrupt. Do this with a NULL or
162 * interrupt char; depending on the tty mode.
166 printoption("td: recv IAC", c));
172 printoption("td: recv IAC", c));
181 printoption("td: recv IAC", c));
191 printoption("td: recv IAC", c));
192 ptyflush(); /* half-hearted */
195 if (slctab[SLC_AO].sptr &&
196 *slctab[SLC_AO].sptr != (cc_t)(_POSIX_VDISABLE)) {
198 (unsigned char)*slctab[SLC_AO].sptr;
201 netclear(); /* clear buffer back */
202 output_data("%c%c", IAC, DM);
203 neturg = nfrontp-1; /* off by one XXX */
205 printoption("td: send IAC", DM));
210 * Erase Character and
219 printoption("td: recv IAC", c));
220 ptyflush(); /* half-hearted */
223 ch = *slctab[SLC_EC].sptr;
225 ch = *slctab[SLC_EL].sptr;
226 if (ch != (cc_t)(_POSIX_VDISABLE))
227 *pfrontp++ = (unsigned char)ch;
232 * Check for urgent data...
236 printoption("td: recv IAC", c));
237 SYNCHing = stilloob(net);
243 * Begin option subnegotiation...
266 if (his_state_is_will(TELOPT_EOR))
271 * Handle RFC 10xx Telnet linemode option additions
272 * to command stream (EOF, SUSP, ABORT).
305 * bad form of suboption negotiation.
306 * handle it in such a way as to avoid
307 * damage to local state. Parse
308 * suboption buffer found so far,
309 * then treat remaining stream as
310 * another command sequence.
313 /* for DIAGNOSTICS */
326 /* for DIAGNOSTICS */
332 suboption(); /* handle sub-option */
358 syslog(LOG_ERR, "panic state=%d", state);
359 printf("telnetd: panic state=%d\n", state);
363 } /* end of telrcv */
366 * The will/wont/do/dont state machines are based on Dave Borman's
367 * Telnet option processing state machine.
369 * These correspond to the following states:
370 * my_state = the last negotiated state
371 * want_state = what I want the state to go to
372 * want_resp = how many requests I have sent
373 * All state defaults are negative, and resp defaults to 0.
375 * When initiating a request to change state to new_state:
377 * if ((want_resp == 0 && new_state == my_state) || want_state == new_state) {
380 * want_state = new_state;
385 * When receiving new_state:
389 * if (want_resp && (new_state == my_state))
392 * if ((want_resp == 0) && (new_state != want_state)) {
393 * if (ok_to_switch_to new_state)
394 * want_state = new_state;
399 * my_state = new_state;
401 * Note that new_state is implied in these functions by the function itself.
402 * will and do imply positive new_state, wont and dont imply negative.
404 * Finally, there is one catch. If we send a negative response to a
405 * positive request, my_state will be the positive while want_state will
406 * remain negative. my_state will revert to negative when the negative
407 * acknowlegment arrives from the peer. Thus, my_state generally tells
408 * us not only the last negotiated state, but also tells us what the peer
409 * wants to be doing as well. It is important to understand this difference
410 * as we may wish to be processing data streams based on our desired state
411 * (want_state) or based on what the peer thinks the state is (my_state).
413 * This all works fine because if the peer sends a positive request, the data
414 * that we receive prior to negative acknowlegment will probably be affected
415 * by the positive state, and we can process it as such (if we can; if we
416 * can't then it really doesn't matter). If it is that important, then the
417 * peer probably should be buffering until this option state negotiation
422 send_do(int option, int init)
425 if ((do_dont_resp[option] == 0 && his_state_is_will(option)) ||
426 his_want_state_is_will(option))
429 * Special case for TELOPT_TM: We send a DO, but pretend
430 * that we sent a DONT, so that we can send more DOs if
433 if (option == TELOPT_TM)
434 set_his_want_state_wont(option);
436 set_his_want_state_will(option);
437 do_dont_resp[option]++;
439 output_data((const char *)doopt, option);
441 DIAG(TD_OPTIONS, printoption("td: send do", option));
445 willoption(int option)
448 void (*func)(void) = NULL;
451 * process input from peer.
454 DIAG(TD_OPTIONS, printoption("td: recv will", option));
456 if (do_dont_resp[option]) {
457 do_dont_resp[option]--;
458 if (do_dont_resp[option] && his_state_is_will(option))
459 do_dont_resp[option]--;
461 if (do_dont_resp[option] == 0) {
462 if (his_want_state_is_wont(option)) {
474 * See comments below for more info.
476 not42 = 0; /* looks like a 4.2 system */
480 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
482 * This telnetd implementation does not really
483 * support timing marks, it just uses them to
484 * support the kludge linemode stuff. If we
485 * receive a will or wont TM in response to our
486 * do TM request that may have been sent to
487 * determine kludge linemode support, process
488 * it, otherwise TM should get a negative
492 * Handle the linemode kludge stuff.
493 * If we are not currently supporting any
494 * linemode at all, then we assume that this
495 * is the client telling us to use kludge
496 * linemode in response to our query. Set the
497 * linemode type that is to be supported, note
498 * that the client wishes to use linemode, and
499 * eat the will TM as though it never arrived.
501 if (lmodetype < KLUDGE_LINEMODE) {
502 lmodetype = KLUDGE_LINEMODE;
503 clientstat(TELOPT_LINEMODE, WILL, 0);
504 send_wont(TELOPT_SGA, 1);
505 } else if (lmodetype == NO_AUTOKLUDGE) {
506 lmodetype = KLUDGE_OK;
508 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
510 * We never respond to a WILL TM, and
511 * we leave the state WONT.
517 * If we are going to support flow control
518 * option, then don't worry peer that we can't
519 * change the flow control characters.
521 slctab[SLC_XON].defset.flag &= ~SLC_LEVELBITS;
522 slctab[SLC_XON].defset.flag |= SLC_DEFAULT;
523 slctab[SLC_XOFF].defset.flag &= ~SLC_LEVELBITS;
524 slctab[SLC_XOFF].defset.flag |= SLC_DEFAULT;
530 case TELOPT_XDISPLOC:
531 case TELOPT_NEW_ENVIRON:
532 case TELOPT_OLD_ENVIRON:
537 case TELOPT_LINEMODE:
538 # ifdef KLUDGELINEMODE
540 * Note client's desire to use linemode.
542 lmodetype = REAL_LINEMODE;
543 # endif /* KLUDGELINEMODE */
547 #endif /* LINEMODE */
549 #ifdef AUTHENTICATION
550 case TELOPT_AUTHENTICATION:
558 func = encrypt_send_support;
561 #endif /* ENCRYPTION */
567 set_his_want_state_will(option);
570 do_dont_resp[option]++;
571 send_dont(option, 0);
575 * Option processing that should happen when
576 * we receive conformation of a change in
577 * state that we had requested.
581 not42 = 0; /* looks like a 4.2 system */
583 * Egads, he responded "WILL ECHO". Turn
586 send_dont(option, 1);
588 * "WILL ECHO". Kludge upon kludge!
589 * A 4.2 client is now echoing user input at
590 * the tty. This is probably undesireable and
591 * it should be stopped. The client will
592 * respond WONT TM to the DO TM that we send to
593 * check for kludge linemode. When the WONT TM
594 * arrives, linemode will be turned off and a
595 * change propogated to the pty. This change
596 * will cause us to process the new pty state
597 * in localstat(), which will notice that
598 * linemode is off and send a WILL ECHO
599 * so that we are properly in character mode and
604 case TELOPT_LINEMODE:
605 # ifdef KLUDGELINEMODE
607 * Note client's desire to use linemode.
609 lmodetype = REAL_LINEMODE;
610 # endif /* KLUDGELINEMODE */
613 #endif /* LINEMODE */
615 #ifdef AUTHENTICATION
616 case TELOPT_AUTHENTICATION:
623 func = encrypt_send_support;
625 #endif /* ENCRYPTION */
632 set_his_state_will(option);
635 } /* end of willoption */
638 send_dont(int option, int init)
641 if ((do_dont_resp[option] == 0 && his_state_is_wont(option)) ||
642 his_want_state_is_wont(option))
644 set_his_want_state_wont(option);
645 do_dont_resp[option]++;
647 output_data((const char *)dont, option);
649 DIAG(TD_OPTIONS, printoption("td: send dont", option));
653 wontoption(int option)
656 * Process client input.
659 DIAG(TD_OPTIONS, printoption("td: recv wont", option));
661 if (do_dont_resp[option]) {
662 do_dont_resp[option]--;
663 if (do_dont_resp[option] && his_state_is_wont(option))
664 do_dont_resp[option]--;
666 if (do_dont_resp[option] == 0) {
667 if (his_want_state_is_will(option)) {
668 /* it is always ok to change to negative state */
671 not42 = 1; /* doesn't seem to be a 4.2 system */
681 case TELOPT_LINEMODE:
682 # ifdef KLUDGELINEMODE
684 * If real linemode is supported, then client is
685 * asking to turn linemode off.
687 if (lmodetype != REAL_LINEMODE)
689 lmodetype = KLUDGE_LINEMODE;
690 # endif /* KLUDGELINEMODE */
691 clientstat(TELOPT_LINEMODE, WONT, 0);
693 #endif /* LINEMODE */
697 * If we get a WONT TM, and had sent a DO TM,
698 * don't respond with a DONT TM, just leave it
699 * as is. Short circut the state machine to
702 set_his_want_state_wont(TELOPT_TM);
707 * If we are not going to support flow control
708 * option, then let peer know that we can't
709 * change the flow control characters.
711 slctab[SLC_XON].defset.flag &= ~SLC_LEVELBITS;
712 slctab[SLC_XON].defset.flag |= SLC_CANTCHANGE;
713 slctab[SLC_XOFF].defset.flag &= ~SLC_LEVELBITS;
714 slctab[SLC_XOFF].defset.flag |= SLC_CANTCHANGE;
717 #ifdef AUTHENTICATION
718 case TELOPT_AUTHENTICATION:
719 auth_finished(0, AUTH_REJECT);
724 * For options that we might spin waiting for
725 * sub-negotiation, if the client turns off the
726 * option rather than responding to the request,
727 * we have to treat it here as if we got a response
728 * to the sub-negotiation, (by updating the timers)
729 * so that we'll break out of the loop.
732 settimer(ttypesubopt);
736 settimer(tspeedsubopt);
739 case TELOPT_XDISPLOC:
740 settimer(xdisplocsubopt);
743 case TELOPT_OLD_ENVIRON:
744 settimer(oenvironsubopt);
747 case TELOPT_NEW_ENVIRON:
748 settimer(environsubopt);
754 set_his_want_state_wont(option);
755 if (his_state_is_will(option))
756 send_dont(option, 0);
760 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
761 if (lmodetype < NO_AUTOKLUDGE) {
762 lmodetype = NO_LINEMODE;
763 clientstat(TELOPT_LINEMODE, WONT, 0);
764 send_will(TELOPT_SGA, 1);
765 send_will(TELOPT_ECHO, 1);
767 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
770 #ifdef AUTHENTICATION
771 case TELOPT_AUTHENTICATION:
772 auth_finished(0, AUTH_REJECT);
780 set_his_state_wont(option);
782 } /* end of wontoption */
785 send_will(int option, int init)
788 if ((will_wont_resp[option] == 0 && my_state_is_will(option))||
789 my_want_state_is_will(option))
791 set_my_want_state_will(option);
792 will_wont_resp[option]++;
794 output_data((const char *)will, option);
796 DIAG(TD_OPTIONS, printoption("td: send will", option));
799 #if !defined(LINEMODE) || !defined(KLUDGELINEMODE)
801 * When we get a DONT SGA, we will try once to turn it
802 * back on. If the other side responds DONT SGA, we
803 * leave it at that. This is so that when we talk to
804 * clients that understand KLUDGELINEMODE but not LINEMODE,
805 * we'll keep them in char-at-a-time mode.
816 * Process client input.
819 DIAG(TD_OPTIONS, printoption("td: recv do", option));
821 if (will_wont_resp[option]) {
822 will_wont_resp[option]--;
823 if (will_wont_resp[option] && my_state_is_will(option))
824 will_wont_resp[option]--;
826 if ((will_wont_resp[option] == 0) && (my_want_state_is_wont(option))) {
830 # ifdef KLUDGELINEMODE
831 if (lmodetype == NO_LINEMODE)
833 if (his_state_is_wont(TELOPT_LINEMODE))
852 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
854 * If kludge linemode is in use, then we must
855 * process an incoming do SGA for linemode
858 if (lmodetype == KLUDGE_LINEMODE) {
860 * Receipt of "do SGA" in kludge
861 * linemode is the peer asking us to
862 * turn off linemode. Make note of
865 clientstat(TELOPT_LINEMODE, WONT, 0);
867 * If linemode did not get turned off
868 * then don't tell peer that we did.
869 * Breaking here forces a wont SGA to
877 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
887 * Special case for TM. We send a WILL, but
888 * pretend we sent a WONT.
890 send_will(option, 0);
891 set_my_want_state_wont(option);
892 set_my_state_wont(option);
897 * When we get a LOGOUT option, respond
898 * with a WILL LOGOUT, make sure that
899 * it gets written out to the network,
900 * and then just go away...
902 set_my_want_state_will(TELOPT_LOGOUT);
903 send_will(TELOPT_LOGOUT, 0);
904 set_my_state_will(TELOPT_LOGOUT);
914 #endif /* ENCRYPTION */
915 case TELOPT_LINEMODE:
920 case TELOPT_XDISPLOC:
921 #ifdef TELOPT_ENVIRON
922 case TELOPT_NEW_ENVIRON:
924 case TELOPT_OLD_ENVIRON:
929 set_my_want_state_will(option);
930 send_will(option, 0);
932 will_wont_resp[option]++;
933 send_wont(option, 0);
936 set_my_state_will(option);
938 } /* end of dooption */
941 send_wont(int option, int init)
944 if ((will_wont_resp[option] == 0 && my_state_is_wont(option)) ||
945 my_want_state_is_wont(option))
947 set_my_want_state_wont(option);
948 will_wont_resp[option]++;
950 output_data((const char *)wont, option);
952 DIAG(TD_OPTIONS, printoption("td: send wont", option));
956 dontoption(int option)
959 * Process client input.
963 DIAG(TD_OPTIONS, printoption("td: recv dont", option));
965 if (will_wont_resp[option]) {
966 will_wont_resp[option]--;
967 if (will_wont_resp[option] && my_state_is_wont(option))
968 will_wont_resp[option]--;
970 if ((will_wont_resp[option] == 0) && (my_want_state_is_will(option))) {
978 case TELOPT_ECHO: /* we should stop echoing */
980 # ifdef KLUDGELINEMODE
981 if ((lmodetype != REAL_LINEMODE) &&
982 (lmodetype != KLUDGE_LINEMODE))
984 if (his_state_is_wont(TELOPT_LINEMODE))
995 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
997 * If kludge linemode is in use, then we
998 * must process an incoming do SGA for
1001 if ((lmodetype == KLUDGE_LINEMODE) ||
1002 (lmodetype == KLUDGE_OK)) {
1004 * The client is asking us to turn
1007 lmodetype = KLUDGE_LINEMODE;
1008 clientstat(TELOPT_LINEMODE, WILL, 0);
1010 * If we did not turn line mode on,
1011 * then what do we say? Will SGA?
1012 * This violates design of telnet.
1013 * Gross. Very Gross.
1018 set_my_want_state_wont(option);
1019 if (my_state_is_will(option))
1020 send_wont(option, 0);
1021 set_my_state_wont(option);
1022 if (turn_on_sga ^= 1)
1023 send_will(option, 1);
1025 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
1031 set_my_want_state_wont(option);
1032 if (my_state_is_will(option))
1033 send_wont(option, 0);
1035 set_my_state_wont(option);
1037 } /* end of dontoption */
1041 int env_ovalue = -1;
1042 #else /* ENV_HACK */
1043 # define env_ovar OLD_ENV_VAR
1044 # define env_ovalue OLD_ENV_VALUE
1045 #endif /* ENV_HACK */
1047 /* envvarok(char*) */
1048 /* check that variable is safe to pass to login or shell */
1050 envvarok(char *varp)
1053 if (strcmp(varp, "TERMCAP") && /* to prevent a security hole */
1054 strcmp(varp, "TERMINFO") && /* with tgetent */
1055 strcmp(varp, "TERMPATH") &&
1056 strcmp(varp, "HOME") && /* to prevent the tegetent bug */
1057 strncmp(varp, "LD_", strlen("LD_")) && /* most systems */
1058 strncmp(varp, "_RLD_", strlen("_RLD_")) && /* IRIX */
1059 strcmp(varp, "LIBPATH") && /* AIX */
1060 strcmp(varp, "ENV") &&
1061 strcmp(varp, "BASH_ENV") &&
1062 strcmp(varp, "IFS") &&
1063 strncmp(varp, "KRB5", strlen("KRB5")) && /* Krb5 */
1065 * The above case is a catch-all for now. Here are some of
1066 * the specific ones we must avoid passing, at least until
1067 * we can prove it can be done safely. Keep this list
1068 * around un case someone wants to remove the catch-all.
1070 strcmp(varp, "KRB5_CONFIG") && /* Krb5 */
1071 strcmp(varp, "KRB5CCNAME") && /* Krb5 */
1072 strcmp(varp, "KRB5_KTNAME") && /* Krb5 */
1073 strcmp(varp, "KRBTKFILE") && /* Krb4 */
1074 strcmp(varp, "KRB_CONF") && /* CNS 4 */
1075 strcmp(varp, "KRB_REALMS") && /* CNS 4 */
1076 strcmp(varp, "RESOLV_HOST_CONF")) /* Linux */
1079 syslog(LOG_INFO, "Rejected the attempt to modify the "
1080 "environment variable \"%s\"", varp);
1088 * Look at the sub-option buffer, and try to be helpful to the other
1091 * Currently we recognize:
1103 DIAG(TD_OPTIONS, {netflush(); printsub('<', subpointer, SB_LEN()+2);});
1107 case TELOPT_TSPEED: {
1110 if (his_state_is_wont(TELOPT_TSPEED)) /* Ignore if option disabled */
1113 settimer(tspeedsubopt);
1115 if (SB_EOF() || SB_GET() != TELQUAL_IS)
1118 xspeed = atoi((char *)subpointer);
1120 while (SB_GET() != ',' && !SB_EOF());
1124 rspeed = atoi((char *)subpointer);
1125 clientstat(TELOPT_TSPEED, xspeed, rspeed);
1129 } /* end of case TELOPT_TSPEED */
1131 case TELOPT_TTYPE: { /* Yaaaay! */
1132 static char terminalname[TERMINAL_TYPE_SIZE];
1134 if (his_state_is_wont(TELOPT_TTYPE)) /* Ignore if option disabled */
1136 settimer(ttypesubopt);
1138 if (SB_EOF() || SB_GET() != TELQUAL_IS) {
1139 return; /* ??? XXX but, this is the most robust */
1142 terminaltype = terminalname;
1144 while ((terminaltype < (terminalname + sizeof terminalname-1)) &&
1152 *terminaltype++ = c; /* accumulate name */
1155 terminaltype = terminalname;
1157 } /* end of case TELOPT_TTYPE */
1160 int xwinsize, ywinsize;
1162 if (his_state_is_wont(TELOPT_NAWS)) /* Ignore if option disabled */
1167 xwinsize = SB_GET() << 8;
1170 xwinsize |= SB_GET();
1173 ywinsize = SB_GET() << 8;
1176 ywinsize |= SB_GET();
1177 clientstat(TELOPT_NAWS, xwinsize, ywinsize);
1181 } /* end of case TELOPT_NAWS */
1184 case TELOPT_LINEMODE: {
1187 if (his_state_is_wont(TELOPT_LINEMODE)) /* Ignore if option disabled */
1190 * Process linemode suboptions.
1193 break; /* garbage was sent */
1194 request = SB_GET(); /* get will/wont */
1197 break; /* another garbage check */
1199 if (request == LM_SLC) { /* SLC is not preceeded by WILL or WONT */
1201 * Process suboption buffer of slc's
1204 do_opt_slc(subpointer, subend - subpointer);
1207 } else if (request == LM_MODE) {
1210 useeditmode = SB_GET(); /* get mode flag */
1211 clientstat(LM_MODE, 0, 0);
1217 switch (SB_GET()) { /* what suboption? */
1218 case LM_FORWARDMASK:
1220 * According to spec, only server can send request for
1221 * forwardmask, and client can only return a positive response.
1222 * So don't worry about it.
1229 } /* end of case TELOPT_LINEMODE */
1231 case TELOPT_STATUS: {
1239 if (my_state_is_will(TELOPT_STATUS))
1250 } /* end of case TELOPT_STATUS */
1252 case TELOPT_XDISPLOC: {
1253 if (SB_EOF() || SB_GET() != TELQUAL_IS)
1255 settimer(xdisplocsubopt);
1256 subpointer[SB_LEN()] = '\0';
1257 if (setenv("DISPLAY", (char *)subpointer, 1) == -1)
1258 syslog(LOG_ERR, "setenv: cannot set DISPLAY=%s: %m", (char *)subpointer);
1260 } /* end of case TELOPT_XDISPLOC */
1262 #ifdef TELOPT_NEW_ENVIRON
1263 case TELOPT_NEW_ENVIRON:
1265 case TELOPT_OLD_ENVIRON: {
1267 char *cp, *varp, *valp;
1272 if (c == TELQUAL_IS) {
1273 if (subchar == TELOPT_OLD_ENVIRON)
1274 settimer(oenvironsubopt);
1276 settimer(environsubopt);
1277 } else if (c != TELQUAL_INFO) {
1281 #ifdef TELOPT_NEW_ENVIRON
1282 if (subchar == TELOPT_NEW_ENVIRON) {
1285 if ((c == NEW_ENV_VAR) || (c == ENV_USERVAR))
1293 * We only want to do this if we haven't already decided
1294 * whether or not the other side has its VALUE and VAR
1298 int last = -1; /* invalid value */
1300 int got_var = 0, got_value = 0, got_uservar = 0;
1303 * The other side might have its VALUE and VAR values
1304 * reversed. To be interoperable, we need to determine
1305 * which way it is. If the first recognized character
1306 * is a VAR or VALUE, then that will tell us what
1307 * type of client it is. If the fist recognized
1308 * character is a USERVAR, then we continue scanning
1309 * the suboption looking for two consecutive
1310 * VAR or VALUE fields. We should not get two
1311 * consecutive VALUE fields, so finding two
1312 * consecutive VALUE or VAR fields will tell us
1313 * what the client is.
1320 if (last < 0 || last == OLD_ENV_VAR
1321 || (empty && (last == OLD_ENV_VALUE)))
1327 if (last < 0 || last == OLD_ENV_VALUE
1328 || (empty && (last == OLD_ENV_VAR)))
1329 goto env_ovar_wrong;
1331 last = OLD_ENV_VALUE;
1334 /* count strings of USERVAR as one */
1335 if (last != ENV_USERVAR)
1338 if (last == OLD_ENV_VALUE)
1340 if (last == OLD_ENV_VAR)
1341 goto env_ovar_wrong;
1356 if (last == OLD_ENV_VALUE)
1358 if (last == OLD_ENV_VAR)
1359 goto env_ovar_wrong;
1362 * Ok, the first thing was a USERVAR, and there
1363 * are not two consecutive VAR or VALUE commands,
1364 * and none of the VAR or VALUE commands are empty.
1365 * If the client has sent us a well-formed option,
1366 * then the number of VALUEs received should always
1367 * be less than or equal to the number of VARs and
1368 * USERVARs received.
1370 * If we got exactly as many VALUEs as VARs and
1371 * USERVARs, the client has the same definitions.
1373 * If we got exactly as many VARs as VALUEs and
1374 * USERVARS, the client has reversed definitions.
1376 if (got_uservar + got_var == got_value) {
1378 env_ovar = OLD_ENV_VAR;
1379 env_ovalue = OLD_ENV_VALUE;
1380 } else if (got_uservar + got_value == got_var) {
1382 env_ovar = OLD_ENV_VALUE;
1383 env_ovalue = OLD_ENV_VAR;
1385 output_data("ENVIRON VALUE and VAR are reversed!\r\n"));
1394 if ((c == env_ovar) || (c == ENV_USERVAR))
1402 cp = varp = (char *)subpointer;
1407 if (subchar == TELOPT_OLD_ENVIRON) {
1410 else if (c == env_ovalue)
1417 cp = valp = (char *)subpointer;
1423 if (envvarok(varp)) {
1425 if (setenv(varp, valp, 1) == -1)
1426 syslog(LOG_ERR, "setenv: cannot set %s=%s: %m", varp, valp);
1431 cp = varp = (char *)subpointer;
1446 if (envvarok(varp)) {
1448 if (setenv(varp, valp, 1) == -1)
1449 syslog(LOG_ERR, "setenv: cannot set %s=%s: %m", varp, valp);
1455 } /* end of case TELOPT_NEW_ENVIRON */
1456 #ifdef AUTHENTICATION
1457 case TELOPT_AUTHENTICATION:
1464 * These are sent by us and cannot be sent by
1469 auth_is(subpointer, SB_LEN());
1472 auth_name(subpointer, SB_LEN());
1478 case TELOPT_ENCRYPT:
1482 case ENCRYPT_SUPPORT:
1483 encrypt_support(subpointer, SB_LEN());
1486 encrypt_is(subpointer, SB_LEN());
1489 encrypt_reply(subpointer, SB_LEN());
1492 encrypt_start(subpointer, SB_LEN());
1497 case ENCRYPT_REQSTART:
1498 encrypt_request_start(subpointer, SB_LEN());
1500 case ENCRYPT_REQEND:
1502 * We can always send an REQEND so that we cannot
1503 * get stuck encrypting. We should only get this
1504 * if we have been able to get in the correct mode
1507 encrypt_request_end();
1509 case ENCRYPT_ENC_KEYID:
1510 encrypt_enc_keyid(subpointer, SB_LEN());
1512 case ENCRYPT_DEC_KEYID:
1513 encrypt_dec_keyid(subpointer, SB_LEN());
1519 #endif /* ENCRYPTION */
1523 } /* end of switch */
1525 } /* end of suboption */
1530 clientstat(TELOPT_LINEMODE, WILL, 0);
1533 #define ADD(c) *ncp++ = c
1534 #define ADD_DATA(c) { *ncp++ = c; if (c == SE || c == IAC) *ncp++ = c; }
1538 unsigned char statusbuf[256];
1544 netflush(); /* get rid of anything waiting to go out */
1552 * We check the want_state rather than the current state,
1553 * because if we received a DO/WILL for an option that we
1554 * don't support, and the other side didn't send a DONT/WONT
1555 * in response to our WONT/DONT, then the "state" will be
1556 * WILL/DO, and the "want_state" will be WONT/DONT. We
1557 * need to go by the latter.
1559 for (i = 0; i < (unsigned char)NTELOPTS; i++) {
1560 if (my_want_state_is_will(i)) {
1566 if (his_want_state_is_will(i)) {
1574 if (his_want_state_is_will(TELOPT_LFLOW)) {
1584 if (restartany >= 0) {
1588 ADD(LFLOW_RESTART_ANY);
1590 ADD(LFLOW_RESTART_XON);
1597 if (his_want_state_is_will(TELOPT_LINEMODE)) {
1598 unsigned char *cp, *cpe;
1602 ADD(TELOPT_LINEMODE);
1608 ADD(TELOPT_LINEMODE);
1613 for (cpe = cp + len; cp < cpe; cp++)
1617 #endif /* LINEMODE */
1622 output_datalen(statusbuf, ncp - statusbuf);
1623 netflush(); /* Send it on its way */
1626 {printsub('>', statusbuf, ncp - statusbuf); netflush();});
1630 * This function appends data to nfrontp and advances nfrontp.
1631 * Returns the number of characters written altogether (the
1632 * buffer may have been flushed in the process).
1636 output_data(const char *format, ...)
1642 va_start(args, format);
1643 if ((len = vasprintf(&buf, format, args)) == -1) {
1647 output_datalen(buf, len);
1654 output_datalen(const char *buf, int len)
1656 int remaining, copied;
1658 remaining = BUFSIZ - (nfrontp - netobuf);
1660 /* Free up enough space if the room is too low*/
1661 if ((len > BUFSIZ ? BUFSIZ : len) > remaining) {
1663 remaining = BUFSIZ - (nfrontp - netobuf);
1666 /* Copy out as much as will fit */
1667 copied = remaining > len ? len : remaining;
1668 memmove(nfrontp, buf, copied);
1671 remaining -= copied;