2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)state.c 8.5 (Berkeley) 5/30/95
34 * $FreeBSD: src/libexec/telnetd/state.c,v 1.9.2.4 2002/04/13 11:07:12 markm Exp $
35 * $DragonFly: src/libexec/telnetd/state.c,v 1.3 2006/01/12 13:43:10 corecode Exp $
41 unsigned char doopt
[] = { IAC
, DO
, '%', 'c', 0 };
42 unsigned char dont
[] = { IAC
, DONT
, '%', 'c', 0 };
43 unsigned char will
[] = { IAC
, WILL
, '%', 'c', 0 };
44 unsigned char wont
[] = { IAC
, WONT
, '%', 'c', 0 };
48 * Buffer for sub-options, and macros
49 * for suboptions buffer manipulations
51 unsigned char subbuffer
[512], *subpointer
= subbuffer
, *subend
= subbuffer
;
53 #define SB_CLEAR() subpointer = subbuffer
54 #define SB_TERM() { subend = subpointer; SB_CLEAR(); }
55 #define SB_ACCUM(c) if (subpointer < (subbuffer+sizeof subbuffer)) { \
56 *subpointer++ = (c); \
58 #define SB_GET() ((*subpointer++)&0xff)
59 #define SB_EOF() (subpointer >= subend)
60 #define SB_LEN() (subend - subpointer)
63 unsigned char *subsave
;
64 #define SB_SAVE() subsave = subpointer;
65 #define SB_RESTORE() subpointer = subsave;
72 #define TS_DATA 0 /* base state */
73 #define TS_IAC 1 /* look for double IAC's */
74 #define TS_CR 2 /* CR-LF ->'s CR */
75 #define TS_SB 3 /* throw away begin's... */
76 #define TS_SE 4 /* ...end's (suboption negotiation) */
77 #define TS_WILL 5 /* will option negotiation */
78 #define TS_WONT 6 /* wont " */
79 #define TS_DO 7 /* do " */
80 #define TS_DONT 8 /* dont " */
82 static void doclientstat(void);
88 static int state
= TS_DATA
;
91 if ((&ptyobuf
[BUFSIZ
] - pfrontp
) < 2)
93 c
= *netip
++ & 0377, ncc
--;
98 /* Strip off \n or \0 after a \r */
99 if ((c
== 0) || (c
== '\n')) {
110 * We now map \r\n ==> \r for pragmatic reasons.
111 * Many client implementations send \r\n when
112 * the user hits the CarriageReturn key.
114 * We USED to map \r\n ==> \n, since \r\n says
115 * that we want to be in column 1 of the next
116 * printable line, and \n is the standard
117 * unix way of saying that (\r is only good
118 * if CRMOD is set, which it normally is).
120 if ((c
== '\r') && his_state_is_wont(TELOPT_BINARY
)) {
124 * If we are operating in linemode,
125 * convert to local end-of-line.
127 if (linemode
&& (ncc
> 0) && (('\n' == nc
) ||
128 ((0 == nc
) && tty_iscrnl())) ) {
144 * Send the process on the pty side an
145 * interrupt. Do this with a NULL or
146 * interrupt char; depending on the tty mode.
150 printoption("td: recv IAC", c
));
156 printoption("td: recv IAC", c
));
165 printoption("td: recv IAC", c
));
175 printoption("td: recv IAC", c
));
176 ptyflush(); /* half-hearted */
179 if (slctab
[SLC_AO
].sptr
&&
180 *slctab
[SLC_AO
].sptr
!= (cc_t
)(_POSIX_VDISABLE
)) {
182 (unsigned char)*slctab
[SLC_AO
].sptr
;
185 netclear(); /* clear buffer back */
186 output_data("%c%c", IAC
, DM
);
187 neturg
= nfrontp
-1; /* off by one XXX */
189 printoption("td: send IAC", DM
));
194 * Erase Character and
203 printoption("td: recv IAC", c
));
204 ptyflush(); /* half-hearted */
207 ch
= *slctab
[SLC_EC
].sptr
;
209 ch
= *slctab
[SLC_EL
].sptr
;
210 if (ch
!= (cc_t
)(_POSIX_VDISABLE
))
211 *pfrontp
++ = (unsigned char)ch
;
216 * Check for urgent data...
220 printoption("td: recv IAC", c
));
221 SYNCHing
= stilloob(net
);
227 * Begin option subnegotiation...
250 if (his_state_is_will(TELOPT_EOR
))
255 * Handle RFC 10xx Telnet linemode option additions
256 * to command stream (EOF, SUSP, ABORT).
289 * bad form of suboption negotiation.
290 * handle it in such a way as to avoid
291 * damage to local state. Parse
292 * suboption buffer found so far,
293 * then treat remaining stream as
294 * another command sequence.
297 /* for DIAGNOSTICS */
310 /* for DIAGNOSTICS */
316 suboption(); /* handle sub-option */
342 syslog(LOG_ERR
, "panic state=%d", state
);
343 printf("telnetd: panic state=%d\n", state
);
347 } /* end of telrcv */
350 * The will/wont/do/dont state machines are based on Dave Borman's
351 * Telnet option processing state machine.
353 * These correspond to the following states:
354 * my_state = the last negotiated state
355 * want_state = what I want the state to go to
356 * want_resp = how many requests I have sent
357 * All state defaults are negative, and resp defaults to 0.
359 * When initiating a request to change state to new_state:
361 * if ((want_resp == 0 && new_state == my_state) || want_state == new_state) {
364 * want_state = new_state;
369 * When receiving new_state:
373 * if (want_resp && (new_state == my_state))
376 * if ((want_resp == 0) && (new_state != want_state)) {
377 * if (ok_to_switch_to new_state)
378 * want_state = new_state;
383 * my_state = new_state;
385 * Note that new_state is implied in these functions by the function itself.
386 * will and do imply positive new_state, wont and dont imply negative.
388 * Finally, there is one catch. If we send a negative response to a
389 * positive request, my_state will be the positive while want_state will
390 * remain negative. my_state will revert to negative when the negative
391 * acknowlegment arrives from the peer. Thus, my_state generally tells
392 * us not only the last negotiated state, but also tells us what the peer
393 * wants to be doing as well. It is important to understand this difference
394 * as we may wish to be processing data streams based on our desired state
395 * (want_state) or based on what the peer thinks the state is (my_state).
397 * This all works fine because if the peer sends a positive request, the data
398 * that we receive prior to negative acknowlegment will probably be affected
399 * by the positive state, and we can process it as such (if we can; if we
400 * can't then it really doesn't matter). If it is that important, then the
401 * peer probably should be buffering until this option state negotiation
406 send_do(int option
, int init
)
409 if ((do_dont_resp
[option
] == 0 && his_state_is_will(option
)) ||
410 his_want_state_is_will(option
))
413 * Special case for TELOPT_TM: We send a DO, but pretend
414 * that we sent a DONT, so that we can send more DOs if
417 if (option
== TELOPT_TM
)
418 set_his_want_state_wont(option
);
420 set_his_want_state_will(option
);
421 do_dont_resp
[option
]++;
423 output_data((const char *)doopt
, option
);
425 DIAG(TD_OPTIONS
, printoption("td: send do", option
));
429 willoption(int option
)
432 void (*func
)(void) = 0;
435 * process input from peer.
438 DIAG(TD_OPTIONS
, printoption("td: recv will", option
));
440 if (do_dont_resp
[option
]) {
441 do_dont_resp
[option
]--;
442 if (do_dont_resp
[option
] && his_state_is_will(option
))
443 do_dont_resp
[option
]--;
445 if (do_dont_resp
[option
] == 0) {
446 if (his_want_state_is_wont(option
)) {
458 * See comments below for more info.
460 not42
= 0; /* looks like a 4.2 system */
464 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
466 * This telnetd implementation does not really
467 * support timing marks, it just uses them to
468 * support the kludge linemode stuff. If we
469 * receive a will or wont TM in response to our
470 * do TM request that may have been sent to
471 * determine kludge linemode support, process
472 * it, otherwise TM should get a negative
476 * Handle the linemode kludge stuff.
477 * If we are not currently supporting any
478 * linemode at all, then we assume that this
479 * is the client telling us to use kludge
480 * linemode in response to our query. Set the
481 * linemode type that is to be supported, note
482 * that the client wishes to use linemode, and
483 * eat the will TM as though it never arrived.
485 if (lmodetype
< KLUDGE_LINEMODE
) {
486 lmodetype
= KLUDGE_LINEMODE
;
487 clientstat(TELOPT_LINEMODE
, WILL
, 0);
488 send_wont(TELOPT_SGA
, 1);
489 } else if (lmodetype
== NO_AUTOKLUDGE
) {
490 lmodetype
= KLUDGE_OK
;
492 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
494 * We never respond to a WILL TM, and
495 * we leave the state WONT.
501 * If we are going to support flow control
502 * option, then don't worry peer that we can't
503 * change the flow control characters.
505 slctab
[SLC_XON
].defset
.flag
&= ~SLC_LEVELBITS
;
506 slctab
[SLC_XON
].defset
.flag
|= SLC_DEFAULT
;
507 slctab
[SLC_XOFF
].defset
.flag
&= ~SLC_LEVELBITS
;
508 slctab
[SLC_XOFF
].defset
.flag
|= SLC_DEFAULT
;
513 case TELOPT_XDISPLOC
:
514 case TELOPT_NEW_ENVIRON
:
515 case TELOPT_OLD_ENVIRON
:
520 case TELOPT_LINEMODE
:
521 # ifdef KLUDGELINEMODE
523 * Note client's desire to use linemode.
525 lmodetype
= REAL_LINEMODE
;
526 # endif /* KLUDGELINEMODE */
530 #endif /* LINEMODE */
538 set_his_want_state_will(option
);
541 do_dont_resp
[option
]++;
542 send_dont(option
, 0);
546 * Option processing that should happen when
547 * we receive conformation of a change in
548 * state that we had requested.
552 not42
= 0; /* looks like a 4.2 system */
554 * Egads, he responded "WILL ECHO". Turn
557 send_dont(option
, 1);
559 * "WILL ECHO". Kludge upon kludge!
560 * A 4.2 client is now echoing user input at
561 * the tty. This is probably undesireable and
562 * it should be stopped. The client will
563 * respond WONT TM to the DO TM that we send to
564 * check for kludge linemode. When the WONT TM
565 * arrives, linemode will be turned off and a
566 * change propogated to the pty. This change
567 * will cause us to process the new pty state
568 * in localstat(), which will notice that
569 * linemode is off and send a WILL ECHO
570 * so that we are properly in character mode and
575 case TELOPT_LINEMODE
:
576 # ifdef KLUDGELINEMODE
578 * Note client's desire to use linemode.
580 lmodetype
= REAL_LINEMODE
;
581 # endif /* KLUDGELINEMODE */
584 #endif /* LINEMODE */
593 set_his_state_will(option
);
596 } /* end of willoption */
599 send_dont(int option
, int init
)
602 if ((do_dont_resp
[option
] == 0 && his_state_is_wont(option
)) ||
603 his_want_state_is_wont(option
))
605 set_his_want_state_wont(option
);
606 do_dont_resp
[option
]++;
608 output_data((const char *)dont
, option
);
610 DIAG(TD_OPTIONS
, printoption("td: send dont", option
));
614 wontoption(int option
)
617 * Process client input.
620 DIAG(TD_OPTIONS
, printoption("td: recv wont", option
));
622 if (do_dont_resp
[option
]) {
623 do_dont_resp
[option
]--;
624 if (do_dont_resp
[option
] && his_state_is_wont(option
))
625 do_dont_resp
[option
]--;
627 if (do_dont_resp
[option
] == 0) {
628 if (his_want_state_is_will(option
)) {
629 /* it is always ok to change to negative state */
632 not42
= 1; /* doesn't seem to be a 4.2 system */
642 case TELOPT_LINEMODE
:
643 # ifdef KLUDGELINEMODE
645 * If real linemode is supported, then client is
646 * asking to turn linemode off.
648 if (lmodetype
!= REAL_LINEMODE
)
650 lmodetype
= KLUDGE_LINEMODE
;
651 # endif /* KLUDGELINEMODE */
652 clientstat(TELOPT_LINEMODE
, WONT
, 0);
654 #endif /* LINEMODE */
658 * If we get a WONT TM, and had sent a DO TM,
659 * don't respond with a DONT TM, just leave it
660 * as is. Short circut the state machine to
663 set_his_want_state_wont(TELOPT_TM
);
668 * If we are not going to support flow control
669 * option, then let peer know that we can't
670 * change the flow control characters.
672 slctab
[SLC_XON
].defset
.flag
&= ~SLC_LEVELBITS
;
673 slctab
[SLC_XON
].defset
.flag
|= SLC_CANTCHANGE
;
674 slctab
[SLC_XOFF
].defset
.flag
&= ~SLC_LEVELBITS
;
675 slctab
[SLC_XOFF
].defset
.flag
|= SLC_CANTCHANGE
;
680 * For options that we might spin waiting for
681 * sub-negotiation, if the client turns off the
682 * option rather than responding to the request,
683 * we have to treat it here as if we got a response
684 * to the sub-negotiation, (by updating the timers)
685 * so that we'll break out of the loop.
688 settimer(ttypesubopt
);
692 settimer(tspeedsubopt
);
695 case TELOPT_XDISPLOC
:
696 settimer(xdisplocsubopt
);
699 case TELOPT_OLD_ENVIRON
:
700 settimer(oenvironsubopt
);
703 case TELOPT_NEW_ENVIRON
:
704 settimer(environsubopt
);
710 set_his_want_state_wont(option
);
711 if (his_state_is_will(option
))
712 send_dont(option
, 0);
716 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
717 if (lmodetype
< NO_AUTOKLUDGE
) {
718 lmodetype
= NO_LINEMODE
;
719 clientstat(TELOPT_LINEMODE
, WONT
, 0);
720 send_will(TELOPT_SGA
, 1);
721 send_will(TELOPT_ECHO
, 1);
723 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
731 set_his_state_wont(option
);
733 } /* end of wontoption */
736 send_will(int option
, int init
)
739 if ((will_wont_resp
[option
] == 0 && my_state_is_will(option
))||
740 my_want_state_is_will(option
))
742 set_my_want_state_will(option
);
743 will_wont_resp
[option
]++;
745 output_data((const char *)will
, option
);
747 DIAG(TD_OPTIONS
, printoption("td: send will", option
));
750 #if !defined(LINEMODE) || !defined(KLUDGELINEMODE)
752 * When we get a DONT SGA, we will try once to turn it
753 * back on. If the other side responds DONT SGA, we
754 * leave it at that. This is so that when we talk to
755 * clients that understand KLUDGELINEMODE but not LINEMODE,
756 * we'll keep them in char-at-a-time mode.
767 * Process client input.
770 DIAG(TD_OPTIONS
, printoption("td: recv do", option
));
772 if (will_wont_resp
[option
]) {
773 will_wont_resp
[option
]--;
774 if (will_wont_resp
[option
] && my_state_is_will(option
))
775 will_wont_resp
[option
]--;
777 if ((will_wont_resp
[option
] == 0) && (my_want_state_is_wont(option
))) {
781 # ifdef KLUDGELINEMODE
782 if (lmodetype
== NO_LINEMODE
)
784 if (his_state_is_wont(TELOPT_LINEMODE
))
803 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
805 * If kludge linemode is in use, then we must
806 * process an incoming do SGA for linemode
809 if (lmodetype
== KLUDGE_LINEMODE
) {
811 * Receipt of "do SGA" in kludge
812 * linemode is the peer asking us to
813 * turn off linemode. Make note of
816 clientstat(TELOPT_LINEMODE
, WONT
, 0);
818 * If linemode did not get turned off
819 * then don't tell peer that we did.
820 * Breaking here forces a wont SGA to
828 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
838 * Special case for TM. We send a WILL, but
839 * pretend we sent a WONT.
841 send_will(option
, 0);
842 set_my_want_state_wont(option
);
843 set_my_state_wont(option
);
848 * When we get a LOGOUT option, respond
849 * with a WILL LOGOUT, make sure that
850 * it gets written out to the network,
851 * and then just go away...
853 set_my_want_state_will(TELOPT_LOGOUT
);
854 send_will(TELOPT_LOGOUT
, 0);
855 set_my_state_will(TELOPT_LOGOUT
);
861 case TELOPT_LINEMODE
:
866 case TELOPT_XDISPLOC
:
867 #ifdef TELOPT_ENVIRON
868 case TELOPT_NEW_ENVIRON
:
870 case TELOPT_OLD_ENVIRON
:
875 set_my_want_state_will(option
);
876 send_will(option
, 0);
878 will_wont_resp
[option
]++;
879 send_wont(option
, 0);
882 set_my_state_will(option
);
884 } /* end of dooption */
887 send_wont(int option
, int init
)
890 if ((will_wont_resp
[option
] == 0 && my_state_is_wont(option
)) ||
891 my_want_state_is_wont(option
))
893 set_my_want_state_wont(option
);
894 will_wont_resp
[option
]++;
896 output_data((const char *)wont
, option
);
898 DIAG(TD_OPTIONS
, printoption("td: send wont", option
));
902 dontoption(int option
)
905 * Process client input.
909 DIAG(TD_OPTIONS
, printoption("td: recv dont", option
));
911 if (will_wont_resp
[option
]) {
912 will_wont_resp
[option
]--;
913 if (will_wont_resp
[option
] && my_state_is_wont(option
))
914 will_wont_resp
[option
]--;
916 if ((will_wont_resp
[option
] == 0) && (my_want_state_is_will(option
))) {
924 case TELOPT_ECHO
: /* we should stop echoing */
926 # ifdef KLUDGELINEMODE
927 if ((lmodetype
!= REAL_LINEMODE
) &&
928 (lmodetype
!= KLUDGE_LINEMODE
))
930 if (his_state_is_wont(TELOPT_LINEMODE
))
941 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
943 * If kludge linemode is in use, then we
944 * must process an incoming do SGA for
947 if ((lmodetype
== KLUDGE_LINEMODE
) ||
948 (lmodetype
== KLUDGE_OK
)) {
950 * The client is asking us to turn
953 lmodetype
= KLUDGE_LINEMODE
;
954 clientstat(TELOPT_LINEMODE
, WILL
, 0);
956 * If we did not turn line mode on,
957 * then what do we say? Will SGA?
958 * This violates design of telnet.
964 set_my_want_state_wont(option
);
965 if (my_state_is_will(option
))
966 send_wont(option
, 0);
967 set_my_state_wont(option
);
968 if (turn_on_sga
^= 1)
969 send_will(option
, 1);
971 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
977 set_my_want_state_wont(option
);
978 if (my_state_is_will(option
))
979 send_wont(option
, 0);
981 set_my_state_wont(option
);
983 } /* end of dontoption */
989 # define env_ovar OLD_ENV_VAR
990 # define env_ovalue OLD_ENV_VALUE
991 #endif /* ENV_HACK */
996 * Look at the sub-option buffer, and try to be helpful to the other
999 * Currently we recognize:
1011 DIAG(TD_OPTIONS
, {netflush(); printsub('<', subpointer
, SB_LEN()+2);});
1015 case TELOPT_TSPEED
: {
1018 if (his_state_is_wont(TELOPT_TSPEED
)) /* Ignore if option disabled */
1021 settimer(tspeedsubopt
);
1023 if (SB_EOF() || SB_GET() != TELQUAL_IS
)
1026 xspeed
= atoi((char *)subpointer
);
1028 while (SB_GET() != ',' && !SB_EOF());
1032 rspeed
= atoi((char *)subpointer
);
1033 clientstat(TELOPT_TSPEED
, xspeed
, rspeed
);
1037 } /* end of case TELOPT_TSPEED */
1039 case TELOPT_TTYPE
: { /* Yaaaay! */
1040 static char terminalname
[41];
1042 if (his_state_is_wont(TELOPT_TTYPE
)) /* Ignore if option disabled */
1044 settimer(ttypesubopt
);
1046 if (SB_EOF() || SB_GET() != TELQUAL_IS
) {
1047 return; /* ??? XXX but, this is the most robust */
1050 terminaltype
= terminalname
;
1052 while ((terminaltype
< (terminalname
+ sizeof terminalname
-1)) &&
1060 *terminaltype
++ = c
; /* accumulate name */
1063 terminaltype
= terminalname
;
1065 } /* end of case TELOPT_TTYPE */
1068 int xwinsize
, ywinsize
;
1070 if (his_state_is_wont(TELOPT_NAWS
)) /* Ignore if option disabled */
1075 xwinsize
= SB_GET() << 8;
1078 xwinsize
|= SB_GET();
1081 ywinsize
= SB_GET() << 8;
1084 ywinsize
|= SB_GET();
1085 clientstat(TELOPT_NAWS
, xwinsize
, ywinsize
);
1089 } /* end of case TELOPT_NAWS */
1092 case TELOPT_LINEMODE
: {
1095 if (his_state_is_wont(TELOPT_LINEMODE
)) /* Ignore if option disabled */
1098 * Process linemode suboptions.
1101 break; /* garbage was sent */
1102 request
= SB_GET(); /* get will/wont */
1105 break; /* another garbage check */
1107 if (request
== LM_SLC
) { /* SLC is not preceeded by WILL or WONT */
1109 * Process suboption buffer of slc's
1112 do_opt_slc(subpointer
, subend
- subpointer
);
1115 } else if (request
== LM_MODE
) {
1118 useeditmode
= SB_GET(); /* get mode flag */
1119 clientstat(LM_MODE
, 0, 0);
1125 switch (SB_GET()) { /* what suboption? */
1126 case LM_FORWARDMASK
:
1128 * According to spec, only server can send request for
1129 * forwardmask, and client can only return a positive response.
1130 * So don't worry about it.
1137 } /* end of case TELOPT_LINEMODE */
1139 case TELOPT_STATUS
: {
1147 if (my_state_is_will(TELOPT_STATUS
))
1158 } /* end of case TELOPT_STATUS */
1160 case TELOPT_XDISPLOC
: {
1161 if (SB_EOF() || SB_GET() != TELQUAL_IS
)
1163 settimer(xdisplocsubopt
);
1164 subpointer
[SB_LEN()] = '\0';
1165 if (setenv("DISPLAY", (char *)subpointer
, 1) == -1)
1166 syslog(LOG_ERR
, "setenv: cannot set DISPLAY=%s: %m", (char *)subpointer
);
1168 } /* end of case TELOPT_XDISPLOC */
1170 #ifdef TELOPT_NEW_ENVIRON
1171 case TELOPT_NEW_ENVIRON
:
1173 case TELOPT_OLD_ENVIRON
: {
1175 char *cp
, *varp
, *valp
;
1180 if (c
== TELQUAL_IS
) {
1181 if (subchar
== TELOPT_OLD_ENVIRON
)
1182 settimer(oenvironsubopt
);
1184 settimer(environsubopt
);
1185 } else if (c
!= TELQUAL_INFO
) {
1189 #ifdef TELOPT_NEW_ENVIRON
1190 if (subchar
== TELOPT_NEW_ENVIRON
) {
1193 if ((c
== NEW_ENV_VAR
) || (c
== ENV_USERVAR
))
1201 * We only want to do this if we haven't already decided
1202 * whether or not the other side has its VALUE and VAR
1206 int last
= -1; /* invalid value */
1208 int got_var
= 0, got_value
= 0, got_uservar
= 0;
1211 * The other side might have its VALUE and VAR values
1212 * reversed. To be interoperable, we need to determine
1213 * which way it is. If the first recognized character
1214 * is a VAR or VALUE, then that will tell us what
1215 * type of client it is. If the fist recognized
1216 * character is a USERVAR, then we continue scanning
1217 * the suboption looking for two consecutive
1218 * VAR or VALUE fields. We should not get two
1219 * consecutive VALUE fields, so finding two
1220 * consecutive VALUE or VAR fields will tell us
1221 * what the client is.
1228 if (last
< 0 || last
== OLD_ENV_VAR
1229 || (empty
&& (last
== OLD_ENV_VALUE
)))
1235 if (last
< 0 || last
== OLD_ENV_VALUE
1236 || (empty
&& (last
== OLD_ENV_VAR
)))
1237 goto env_ovar_wrong
;
1239 last
= OLD_ENV_VALUE
;
1242 /* count strings of USERVAR as one */
1243 if (last
!= ENV_USERVAR
)
1246 if (last
== OLD_ENV_VALUE
)
1248 if (last
== OLD_ENV_VAR
)
1249 goto env_ovar_wrong
;
1264 if (last
== OLD_ENV_VALUE
)
1266 if (last
== OLD_ENV_VAR
)
1267 goto env_ovar_wrong
;
1270 * Ok, the first thing was a USERVAR, and there
1271 * are not two consecutive VAR or VALUE commands,
1272 * and none of the VAR or VALUE commands are empty.
1273 * If the client has sent us a well-formed option,
1274 * then the number of VALUEs received should always
1275 * be less than or equal to the number of VARs and
1276 * USERVARs received.
1278 * If we got exactly as many VALUEs as VARs and
1279 * USERVARs, the client has the same definitions.
1281 * If we got exactly as many VARs as VALUEs and
1282 * USERVARS, the client has reversed definitions.
1284 if (got_uservar
+ got_var
== got_value
) {
1286 env_ovar
= OLD_ENV_VAR
;
1287 env_ovalue
= OLD_ENV_VALUE
;
1288 } else if (got_uservar
+ got_value
== got_var
) {
1290 env_ovar
= OLD_ENV_VALUE
;
1291 env_ovalue
= OLD_ENV_VAR
;
1293 output_data("ENVIRON VALUE and VAR are reversed!\r\n"));
1302 if ((c
== env_ovar
) || (c
== ENV_USERVAR
))
1310 cp
= varp
= (char *)subpointer
;
1315 if (subchar
== TELOPT_OLD_ENVIRON
) {
1318 else if (c
== env_ovalue
)
1325 cp
= valp
= (char *)subpointer
;
1332 if (setenv(varp
, valp
, 1) == -1)
1333 syslog(LOG_ERR
, "setenv: cannot set %s=%s: %m", varp
, valp
);
1337 cp
= varp
= (char *)subpointer
;
1353 if (setenv(varp
, valp
, 1) == -1)
1354 syslog(LOG_ERR
, "setenv: cannot set %s=%s: %m", varp
, valp
);
1359 } /* end of case TELOPT_NEW_ENVIRON */
1363 } /* end of switch */
1365 } /* end of suboption */
1370 clientstat(TELOPT_LINEMODE
, WILL
, 0);
1373 #define ADD(c) *ncp++ = c
1374 #define ADD_DATA(c) { *ncp++ = c; if (c == SE || c == IAC) *ncp++ = c; }
1378 unsigned char statusbuf
[256];
1384 netflush(); /* get rid of anything waiting to go out */
1392 * We check the want_state rather than the current state,
1393 * because if we received a DO/WILL for an option that we
1394 * don't support, and the other side didn't send a DONT/WONT
1395 * in response to our WONT/DONT, then the "state" will be
1396 * WILL/DO, and the "want_state" will be WONT/DONT. We
1397 * need to go by the latter.
1399 for (i
= 0; i
< (unsigned char)NTELOPTS
; i
++) {
1400 if (my_want_state_is_will(i
)) {
1406 if (his_want_state_is_will(i
)) {
1414 if (his_want_state_is_will(TELOPT_LFLOW
)) {
1424 if (restartany
>= 0) {
1428 ADD(LFLOW_RESTART_ANY
);
1430 ADD(LFLOW_RESTART_XON
);
1437 if (his_want_state_is_will(TELOPT_LINEMODE
)) {
1438 unsigned char *cp
, *cpe
;
1442 ADD(TELOPT_LINEMODE
);
1448 ADD(TELOPT_LINEMODE
);
1453 for (cpe
= cp
+ len
; cp
< cpe
; cp
++)
1457 #endif /* LINEMODE */
1462 output_datalen(statusbuf
, ncp
- statusbuf
);
1463 netflush(); /* Send it on its way */
1466 {printsub('>', statusbuf
, ncp
- statusbuf
); netflush();});
1470 * This function appends data to nfrontp and advances nfrontp.
1471 * Returns the number of characters written altogether (the
1472 * buffer may have been flushed in the process).
1476 output_data(const char *format
, ...)
1482 va_start(args
, format
);
1483 if ((len
= vasprintf(&buf
, format
, args
)) == -1)
1485 output_datalen(buf
, len
);
1492 output_datalen(const char *buf
, int len
)
1494 int remaining
, copied
;
1496 remaining
= BUFSIZ
- (nfrontp
- netobuf
);
1498 /* Free up enough space if the room is too low*/
1499 if ((len
> BUFSIZ
? BUFSIZ
: len
) > remaining
) {
1501 remaining
= BUFSIZ
- (nfrontp
- netobuf
);
1504 /* Copy out as much as will fit */
1505 copied
= remaining
> len
? len
: remaining
;
1506 memmove(nfrontp
, buf
, copied
);
1509 remaining
-= copied
;