No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / appl / telnet / telnetd / state.c
blob94ec7a61638d45e915b4a8220b33435028728afc
1 /*
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
7 * are met:
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
31 * SUCH DAMAGE.
34 #include "telnetd.h"
36 __RCSID("$Heimdal: state.c 18110 2006-09-19 08:25:20Z lha $"
37 "$NetBSD$");
39 unsigned char doopt[] = { IAC, DO, '%', 'c', 0 };
40 unsigned char dont[] = { IAC, DONT, '%', 'c', 0 };
41 unsigned char will[] = { IAC, WILL, '%', 'c', 0 };
42 unsigned char wont[] = { IAC, WONT, '%', 'c', 0 };
43 int not42 = 1;
46 * Buffer for sub-options, and macros
47 * for suboptions buffer manipulations
49 unsigned char subbuffer[1024*64], *subpointer= subbuffer, *subend= subbuffer;
51 #define SB_CLEAR() subpointer = subbuffer
52 #define SB_TERM() { subend = subpointer; SB_CLEAR(); }
53 #define SB_ACCUM(c) if (subpointer < (subbuffer+sizeof subbuffer)) { \
54 *subpointer++ = (c); \
56 #define SB_GET() ((*subpointer++)&0xff)
57 #define SB_EOF() (subpointer >= subend)
58 #define SB_LEN() (subend - subpointer)
60 #ifdef ENV_HACK
61 unsigned char *subsave;
62 #define SB_SAVE() subsave = subpointer;
63 #define SB_RESTORE() subpointer = subsave;
64 #endif
68 * State for recv fsm
70 #define TS_DATA 0 /* base state */
71 #define TS_IAC 1 /* look for double IAC's */
72 #define TS_CR 2 /* CR-LF ->'s CR */
73 #define TS_SB 3 /* throw away begin's... */
74 #define TS_SE 4 /* ...end's (suboption negotiation) */
75 #define TS_WILL 5 /* will option negotiation */
76 #define TS_WONT 6 /* wont -''- */
77 #define TS_DO 7 /* do -''- */
78 #define TS_DONT 8 /* dont -''- */
80 void
81 telrcv(void)
83 int c;
84 static int state = TS_DATA;
86 while (ncc > 0) {
87 if ((&ptyobuf[BUFSIZ] - pfrontp) < 2)
88 break;
89 c = *netip++ & 0377, ncc--;
90 #ifdef ENCRYPTION
91 if (decrypt_input)
92 c = (*decrypt_input)(c);
93 #endif
94 switch (state) {
96 case TS_CR:
97 state = TS_DATA;
98 /* Strip off \n or \0 after a \r */
99 if ((c == 0) || (c == '\n')) {
100 break;
102 /* FALL THROUGH */
104 case TS_DATA:
105 if (c == IAC) {
106 state = TS_IAC;
107 break;
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)) {
121 int nc = *netip;
122 #ifdef ENCRYPTION
123 if (decrypt_input)
124 nc = (*decrypt_input)(nc & 0xff);
125 #endif
127 #ifdef ENCRYPTION
128 if (decrypt_input)
129 (void)(*decrypt_input)(-1);
130 #endif
131 state = TS_CR;
134 *pfrontp++ = c;
135 break;
137 case TS_IAC:
138 gotiac: switch (c) {
141 * Send the process on the pty side an
142 * interrupt. Do this with a NULL or
143 * interrupt char; depending on the tty mode.
145 case IP:
146 DIAG(TD_OPTIONS,
147 printoption("td: recv IAC", c));
148 interrupt();
149 break;
151 case BREAK:
152 DIAG(TD_OPTIONS,
153 printoption("td: recv IAC", c));
154 sendbrk();
155 break;
158 * Are You There?
160 case AYT:
161 DIAG(TD_OPTIONS,
162 printoption("td: recv IAC", c));
163 recv_ayt();
164 break;
167 * Abort Output
169 case AO:
171 DIAG(TD_OPTIONS,
172 printoption("td: recv IAC", c));
173 ptyflush(); /* half-hearted */
174 init_termbuf();
176 if (slctab[SLC_AO].sptr &&
177 *slctab[SLC_AO].sptr != (cc_t)(_POSIX_VDISABLE)) {
178 *pfrontp++ =
179 (unsigned char)*slctab[SLC_AO].sptr;
182 netclear(); /* clear buffer back */
183 output_data ("%c%c", IAC, DM);
184 neturg = nfrontp-1; /* off by one XXX */
185 DIAG(TD_OPTIONS,
186 printoption("td: send IAC", DM));
187 break;
191 * Erase Character and
192 * Erase Line
194 case EC:
195 case EL:
197 cc_t ch;
199 DIAG(TD_OPTIONS,
200 printoption("td: recv IAC", c));
201 ptyflush(); /* half-hearted */
202 init_termbuf();
203 if (c == EC)
204 ch = *slctab[SLC_EC].sptr;
205 else
206 ch = *slctab[SLC_EL].sptr;
207 if (ch != (cc_t)(_POSIX_VDISABLE))
208 *pfrontp++ = (unsigned char)ch;
209 break;
213 * Check for urgent data...
215 case DM:
216 DIAG(TD_OPTIONS,
217 printoption("td: recv IAC", c));
218 SYNCHing = stilloob(net);
219 settimer(gotDM);
220 break;
224 * Begin option subnegotiation...
226 case SB:
227 state = TS_SB;
228 SB_CLEAR();
229 continue;
231 case WILL:
232 state = TS_WILL;
233 continue;
235 case WONT:
236 state = TS_WONT;
237 continue;
239 case DO:
240 state = TS_DO;
241 continue;
243 case DONT:
244 state = TS_DONT;
245 continue;
246 case EOR:
247 if (his_state_is_will(TELOPT_EOR))
248 doeof();
249 break;
252 * Handle RFC 10xx Telnet linemode option additions
253 * to command stream (EOF, SUSP, ABORT).
255 case xEOF:
256 doeof();
257 break;
259 case SUSP:
260 sendsusp();
261 break;
263 case ABORT:
264 sendbrk();
265 break;
267 case IAC:
268 *pfrontp++ = c;
269 break;
271 state = TS_DATA;
272 break;
274 case TS_SB:
275 if (c == IAC) {
276 state = TS_SE;
277 } else {
278 SB_ACCUM(c);
280 break;
282 case TS_SE:
283 if (c != SE) {
284 if (c != IAC) {
286 * bad form of suboption negotiation.
287 * handle it in such a way as to avoid
288 * damage to local state. Parse
289 * suboption buffer found so far,
290 * then treat remaining stream as
291 * another command sequence.
294 /* for DIAGNOSTICS */
295 SB_ACCUM(IAC);
296 SB_ACCUM(c);
297 subpointer -= 2;
299 SB_TERM();
300 suboption();
301 state = TS_IAC;
302 goto gotiac;
304 SB_ACCUM(c);
305 state = TS_SB;
306 } else {
307 /* for DIAGNOSTICS */
308 SB_ACCUM(IAC);
309 SB_ACCUM(SE);
310 subpointer -= 2;
312 SB_TERM();
313 suboption(); /* handle sub-option */
314 state = TS_DATA;
316 break;
318 case TS_WILL:
319 willoption(c);
320 state = TS_DATA;
321 continue;
323 case TS_WONT:
324 wontoption(c);
325 if (c==TELOPT_ENCRYPT && his_do_dont_is_changing(TELOPT_ENCRYPT) )
326 dontoption(c);
327 state = TS_DATA;
328 continue;
330 case TS_DO:
331 dooption(c);
332 state = TS_DATA;
333 continue;
335 case TS_DONT:
336 dontoption(c);
337 state = TS_DATA;
338 continue;
340 default:
341 syslog(LOG_ERR, "telnetd: panic state=%d\n", state);
342 printf("telnetd: panic state=%d\n", state);
343 exit(1);
346 } /* end of telrcv */
349 * The will/wont/do/dont state machines are based on Dave Borman's
350 * Telnet option processing state machine.
352 * These correspond to the following states:
353 * my_state = the last negotiated state
354 * want_state = what I want the state to go to
355 * want_resp = how many requests I have sent
356 * All state defaults are negative, and resp defaults to 0.
358 * When initiating a request to change state to new_state:
360 * if ((want_resp == 0 && new_state == my_state) || want_state == new_state) {
361 * do nothing;
362 * } else {
363 * want_state = new_state;
364 * send new_state;
365 * want_resp++;
368 * When receiving new_state:
370 * if (want_resp) {
371 * want_resp--;
372 * if (want_resp && (new_state == my_state))
373 * want_resp--;
375 * if ((want_resp == 0) && (new_state != want_state)) {
376 * if (ok_to_switch_to new_state)
377 * want_state = new_state;
378 * else
379 * want_resp++;
380 * send want_state;
382 * my_state = new_state;
384 * Note that new_state is implied in these functions by the function itself.
385 * will and do imply positive new_state, wont and dont imply negative.
387 * Finally, there is one catch. If we send a negative response to a
388 * positive request, my_state will be the positive while want_state will
389 * remain negative. my_state will revert to negative when the negative
390 * acknowlegment arrives from the peer. Thus, my_state generally tells
391 * us not only the last negotiated state, but also tells us what the peer
392 * wants to be doing as well. It is important to understand this difference
393 * as we may wish to be processing data streams based on our desired state
394 * (want_state) or based on what the peer thinks the state is (my_state).
396 * This all works fine because if the peer sends a positive request, the data
397 * that we receive prior to negative acknowlegment will probably be affected
398 * by the positive state, and we can process it as such (if we can; if we
399 * can't then it really doesn't matter). If it is that important, then the
400 * peer probably should be buffering until this option state negotiation
401 * is complete.
404 void
405 send_do(int option, int init)
407 if (init) {
408 if ((do_dont_resp[option] == 0 && his_state_is_will(option)) ||
409 his_want_state_is_will(option))
410 return;
412 * Special case for TELOPT_TM: We send a DO, but pretend
413 * that we sent a DONT, so that we can send more DOs if
414 * we want to.
416 if (option == TELOPT_TM)
417 set_his_want_state_wont(option);
418 else
419 set_his_want_state_will(option);
420 do_dont_resp[option]++;
422 output_data((const char *)doopt, option);
424 DIAG(TD_OPTIONS, printoption("td: send do", option));
427 #ifdef AUTHENTICATION
428 extern void auth_request(void);
429 #endif
430 #ifdef ENCRYPTION
431 extern void encrypt_send_support(void);
432 #endif
434 void
435 willoption(int option)
437 int changeok = 0;
438 void (*func)(void) = NULL;
441 * process input from peer.
444 DIAG(TD_OPTIONS, printoption("td: recv will", option));
446 if (do_dont_resp[option]) {
447 do_dont_resp[option]--;
448 if (do_dont_resp[option] && his_state_is_will(option))
449 do_dont_resp[option]--;
451 if (do_dont_resp[option] == 0) {
452 if (his_want_state_is_wont(option)) {
453 switch (option) {
455 case TELOPT_BINARY:
456 init_termbuf();
457 tty_binaryin(1);
458 set_termbuf();
459 changeok++;
460 break;
462 case TELOPT_ECHO:
464 * See comments below for more info.
466 not42 = 0; /* looks like a 4.2 system */
467 break;
469 case TELOPT_TM:
471 * We never respond to a WILL TM, and
472 * we leave the state WONT.
474 return;
476 case TELOPT_LFLOW:
478 * If we are going to support flow control
479 * option, then don't worry peer that we can't
480 * change the flow control characters.
482 slctab[SLC_XON].defset.flag &= ~SLC_LEVELBITS;
483 slctab[SLC_XON].defset.flag |= SLC_DEFAULT;
484 slctab[SLC_XOFF].defset.flag &= ~SLC_LEVELBITS;
485 slctab[SLC_XOFF].defset.flag |= SLC_DEFAULT;
486 case TELOPT_TTYPE:
487 case TELOPT_SGA:
488 case TELOPT_NAWS:
489 case TELOPT_TSPEED:
490 case TELOPT_XDISPLOC:
491 case TELOPT_NEW_ENVIRON:
492 case TELOPT_OLD_ENVIRON:
493 changeok++;
494 break;
497 #ifdef AUTHENTICATION
498 case TELOPT_AUTHENTICATION:
499 func = auth_request;
500 changeok++;
501 break;
502 #endif
504 #ifdef ENCRYPTION
505 case TELOPT_ENCRYPT:
506 func = encrypt_send_support;
507 changeok++;
508 break;
509 #endif
511 default:
512 break;
514 if (changeok) {
515 set_his_want_state_will(option);
516 send_do(option, 0);
517 } else {
518 do_dont_resp[option]++;
519 send_dont(option, 0);
521 } else {
523 * Option processing that should happen when
524 * we receive conformation of a change in
525 * state that we had requested.
527 switch (option) {
528 case TELOPT_ECHO:
529 not42 = 0; /* looks like a 4.2 system */
531 * Egads, he responded "WILL ECHO". Turn
532 * it off right now!
534 send_dont(option, 1);
536 * "WILL ECHO". Kludge upon kludge!
537 * A 4.2 client is now echoing user input at
538 * the tty. This is probably undesireable and
539 * it should be stopped. The client will
540 * respond WONT TM to the DO TM that we send to
541 * check for kludge linemode. When the WONT TM
542 * arrives, linemode will be turned off and a
543 * change propogated to the pty. This change
544 * will cause us to process the new pty state
545 * in localstat(), which will notice that
546 * linemode is off and send a WILL ECHO
547 * so that we are properly in character mode and
548 * all is well.
550 break;
552 #ifdef AUTHENTICATION
553 case TELOPT_AUTHENTICATION:
554 func = auth_request;
555 break;
556 #endif
558 #ifdef ENCRYPTION
559 case TELOPT_ENCRYPT:
560 func = encrypt_send_support;
561 break;
562 #endif
564 case TELOPT_LFLOW:
565 func = flowstat;
566 break;
570 set_his_state_will(option);
571 if (func)
572 (*func)();
573 } /* end of willoption */
575 void
576 send_dont(int option, int init)
578 if (init) {
579 if ((do_dont_resp[option] == 0 && his_state_is_wont(option)) ||
580 his_want_state_is_wont(option))
581 return;
582 set_his_want_state_wont(option);
583 do_dont_resp[option]++;
585 output_data((const char *)dont, option);
587 DIAG(TD_OPTIONS, printoption("td: send dont", option));
590 void
591 wontoption(int option)
594 * Process client input.
597 DIAG(TD_OPTIONS, printoption("td: recv wont", option));
599 if (do_dont_resp[option]) {
600 do_dont_resp[option]--;
601 if (do_dont_resp[option] && his_state_is_wont(option))
602 do_dont_resp[option]--;
604 if (do_dont_resp[option] == 0) {
605 if (his_want_state_is_will(option)) {
606 /* it is always ok to change to negative state */
607 switch (option) {
608 case TELOPT_ECHO:
609 not42 = 1; /* doesn't seem to be a 4.2 system */
610 break;
612 case TELOPT_BINARY:
613 init_termbuf();
614 tty_binaryin(0);
615 set_termbuf();
616 break;
618 case TELOPT_TM:
620 * If we get a WONT TM, and had sent a DO TM,
621 * don't respond with a DONT TM, just leave it
622 * as is. Short circut the state machine to
623 * achive this.
625 set_his_want_state_wont(TELOPT_TM);
626 return;
628 case TELOPT_LFLOW:
630 * If we are not going to support flow control
631 * option, then let peer know that we can't
632 * change the flow control characters.
634 slctab[SLC_XON].defset.flag &= ~SLC_LEVELBITS;
635 slctab[SLC_XON].defset.flag |= SLC_CANTCHANGE;
636 slctab[SLC_XOFF].defset.flag &= ~SLC_LEVELBITS;
637 slctab[SLC_XOFF].defset.flag |= SLC_CANTCHANGE;
638 break;
640 #ifdef AUTHENTICATION
641 case TELOPT_AUTHENTICATION:
642 auth_finished(0, AUTH_REJECT);
643 break;
644 #endif
647 * For options that we might spin waiting for
648 * sub-negotiation, if the client turns off the
649 * option rather than responding to the request,
650 * we have to treat it here as if we got a response
651 * to the sub-negotiation, (by updating the timers)
652 * so that we'll break out of the loop.
654 case TELOPT_TTYPE:
655 settimer(ttypesubopt);
656 break;
658 case TELOPT_TSPEED:
659 settimer(tspeedsubopt);
660 break;
662 case TELOPT_XDISPLOC:
663 settimer(xdisplocsubopt);
664 break;
666 case TELOPT_OLD_ENVIRON:
667 settimer(oenvironsubopt);
668 break;
670 case TELOPT_NEW_ENVIRON:
671 settimer(environsubopt);
672 break;
674 default:
675 break;
677 set_his_want_state_wont(option);
678 if (his_state_is_will(option))
679 send_dont(option, 0);
680 } else {
681 switch (option) {
682 case TELOPT_TM:
683 break;
685 #ifdef AUTHENTICATION
686 case TELOPT_AUTHENTICATION:
687 auth_finished(0, AUTH_REJECT);
688 break;
689 #endif
690 default:
691 break;
695 set_his_state_wont(option);
697 } /* end of wontoption */
699 void
700 send_will(int option, int init)
702 if (init) {
703 if ((will_wont_resp[option] == 0 && my_state_is_will(option))||
704 my_want_state_is_will(option))
705 return;
706 set_my_want_state_will(option);
707 will_wont_resp[option]++;
709 output_data ((const char *)will, option);
711 DIAG(TD_OPTIONS, printoption("td: send will", option));
715 * When we get a DONT SGA, we will try once to turn it
716 * back on. If the other side responds DONT SGA, we
717 * leave it at that. This is so that when we talk to
718 * clients that understand KLUDGELINEMODE but not LINEMODE,
719 * we'll keep them in char-at-a-time mode.
721 int turn_on_sga = 0;
723 void
724 dooption(int option)
726 int changeok = 0;
729 * Process client input.
732 DIAG(TD_OPTIONS, printoption("td: recv do", option));
734 if (will_wont_resp[option]) {
735 will_wont_resp[option]--;
736 if (will_wont_resp[option] && my_state_is_will(option))
737 will_wont_resp[option]--;
739 if ((will_wont_resp[option] == 0) && (my_want_state_is_wont(option))) {
740 switch (option) {
741 case TELOPT_ECHO:
743 init_termbuf();
744 tty_setecho(1);
745 set_termbuf();
747 changeok++;
748 break;
750 case TELOPT_BINARY:
751 init_termbuf();
752 tty_binaryout(1);
753 set_termbuf();
754 changeok++;
755 break;
757 case TELOPT_SGA:
758 turn_on_sga = 0;
759 changeok++;
760 break;
762 case TELOPT_STATUS:
763 changeok++;
764 break;
766 case TELOPT_TM:
768 * Special case for TM. We send a WILL, but
769 * pretend we sent a WONT.
771 send_will(option, 0);
772 set_my_want_state_wont(option);
773 set_my_state_wont(option);
774 return;
776 case TELOPT_LOGOUT:
778 * When we get a LOGOUT option, respond
779 * with a WILL LOGOUT, make sure that
780 * it gets written out to the network,
781 * and then just go away...
783 set_my_want_state_will(TELOPT_LOGOUT);
784 send_will(TELOPT_LOGOUT, 0);
785 set_my_state_will(TELOPT_LOGOUT);
786 netflush();
787 cleanup(0);
788 /* NOT REACHED */
789 break;
791 #ifdef ENCRYPTION
792 case TELOPT_ENCRYPT:
793 changeok++;
794 break;
795 #endif
796 case TELOPT_LINEMODE:
797 case TELOPT_TTYPE:
798 case TELOPT_NAWS:
799 case TELOPT_TSPEED:
800 case TELOPT_LFLOW:
801 case TELOPT_XDISPLOC:
802 #ifdef TELOPT_ENVIRON
803 case TELOPT_NEW_ENVIRON:
804 #endif
805 case TELOPT_OLD_ENVIRON:
806 default:
807 break;
809 if (changeok) {
810 set_my_want_state_will(option);
811 send_will(option, 0);
812 } else {
813 will_wont_resp[option]++;
814 send_wont(option, 0);
817 set_my_state_will(option);
819 } /* end of dooption */
821 void
822 send_wont(int option, int init)
824 if (init) {
825 if ((will_wont_resp[option] == 0 && my_state_is_wont(option)) ||
826 my_want_state_is_wont(option))
827 return;
828 set_my_want_state_wont(option);
829 will_wont_resp[option]++;
831 output_data ((const char *)wont, option);
833 DIAG(TD_OPTIONS, printoption("td: send wont", option));
836 void
837 dontoption(int option)
840 * Process client input.
844 DIAG(TD_OPTIONS, printoption("td: recv dont", option));
846 if (will_wont_resp[option]) {
847 will_wont_resp[option]--;
848 if (will_wont_resp[option] && my_state_is_wont(option))
849 will_wont_resp[option]--;
851 if ((will_wont_resp[option] == 0) && (my_want_state_is_will(option))) {
852 switch (option) {
853 case TELOPT_BINARY:
854 init_termbuf();
855 tty_binaryout(0);
856 set_termbuf();
857 break;
859 case TELOPT_ECHO: /* we should stop echoing */
861 init_termbuf();
862 tty_setecho(0);
863 set_termbuf();
865 break;
867 case TELOPT_SGA:
868 set_my_want_state_wont(option);
869 if (my_state_is_will(option))
870 send_wont(option, 0);
871 set_my_state_wont(option);
872 if (turn_on_sga ^= 1)
873 send_will(option, 1);
874 return;
876 default:
877 break;
880 set_my_want_state_wont(option);
881 if (my_state_is_will(option))
882 send_wont(option, 0);
884 set_my_state_wont(option);
886 } /* end of dontoption */
888 #ifdef ENV_HACK
889 int env_ovar = -1;
890 int env_ovalue = -1;
891 #else /* ENV_HACK */
892 # define env_ovar OLD_ENV_VAR
893 # define env_ovalue OLD_ENV_VALUE
894 #endif /* ENV_HACK */
897 * suboption()
899 * Look at the sub-option buffer, and try to be helpful to the other
900 * side.
902 * Currently we recognize:
904 * Terminal type is
905 * Linemode
906 * Window size
907 * Terminal speed
909 void
910 suboption(void)
912 int subchar;
914 DIAG(TD_OPTIONS, {netflush(); printsub('<', subpointer, SB_LEN()+2);});
916 subchar = SB_GET();
917 switch (subchar) {
918 case TELOPT_TSPEED: {
919 int xspeed, rspeed;
921 if (his_state_is_wont(TELOPT_TSPEED)) /* Ignore if option disabled */
922 break;
924 settimer(tspeedsubopt);
926 if (SB_EOF() || SB_GET() != TELQUAL_IS)
927 return;
929 xspeed = atoi((char *)subpointer);
931 while (SB_GET() != ',' && !SB_EOF());
932 if (SB_EOF())
933 return;
935 rspeed = atoi((char *)subpointer);
936 clientstat(TELOPT_TSPEED, xspeed, rspeed);
938 break;
940 } /* end of case TELOPT_TSPEED */
942 case TELOPT_TTYPE: { /* Yaaaay! */
943 char *p;
945 if (his_state_is_wont(TELOPT_TTYPE)) /* Ignore if option disabled */
946 break;
947 settimer(ttypesubopt);
949 if (SB_EOF() || SB_GET() != TELQUAL_IS) {
950 return; /* ??? XXX but, this is the most robust */
953 p = terminaltype;
955 while ((p < (terminaltype + sizeof terminaltype-1)) &&
956 !SB_EOF()) {
957 int c;
959 c = SB_GET();
960 if (isupper(c)) {
961 c = tolower(c);
963 *p++ = c; /* accumulate name */
965 *p = 0;
966 break;
967 } /* end of case TELOPT_TTYPE */
969 case TELOPT_NAWS: {
970 int xwinsize, ywinsize;
972 if (his_state_is_wont(TELOPT_NAWS)) /* Ignore if option disabled */
973 break;
975 if (SB_EOF())
976 return;
977 xwinsize = SB_GET() << 8;
978 if (SB_EOF())
979 return;
980 xwinsize |= SB_GET();
981 if (SB_EOF())
982 return;
983 ywinsize = SB_GET() << 8;
984 if (SB_EOF())
985 return;
986 ywinsize |= SB_GET();
987 clientstat(TELOPT_NAWS, xwinsize, ywinsize);
989 break;
991 } /* end of case TELOPT_NAWS */
993 case TELOPT_STATUS: {
994 int mode;
996 if (SB_EOF())
997 break;
998 mode = SB_GET();
999 switch (mode) {
1000 case TELQUAL_SEND:
1001 if (my_state_is_will(TELOPT_STATUS))
1002 send_status();
1003 break;
1005 case TELQUAL_IS:
1006 break;
1008 default:
1009 break;
1011 break;
1012 } /* end of case TELOPT_STATUS */
1014 case TELOPT_XDISPLOC: {
1015 if (SB_EOF() || SB_GET() != TELQUAL_IS)
1016 return;
1017 settimer(xdisplocsubopt);
1018 subpointer[SB_LEN()] = '\0';
1019 esetenv("DISPLAY", (char *)subpointer, 1);
1020 break;
1021 } /* end of case TELOPT_XDISPLOC */
1023 #ifdef TELOPT_NEW_ENVIRON
1024 case TELOPT_NEW_ENVIRON:
1025 #endif
1026 case TELOPT_OLD_ENVIRON: {
1027 int c;
1028 char *cp, *varp, *valp;
1030 if (SB_EOF())
1031 return;
1032 c = SB_GET();
1033 if (c == TELQUAL_IS) {
1034 if (subchar == TELOPT_OLD_ENVIRON)
1035 settimer(oenvironsubopt);
1036 else
1037 settimer(environsubopt);
1038 } else if (c != TELQUAL_INFO) {
1039 return;
1042 #ifdef TELOPT_NEW_ENVIRON
1043 if (subchar == TELOPT_NEW_ENVIRON) {
1044 while (!SB_EOF()) {
1045 c = SB_GET();
1046 if ((c == NEW_ENV_VAR) || (c == ENV_USERVAR))
1047 break;
1049 } else
1050 #endif
1052 #ifdef ENV_HACK
1054 * We only want to do this if we haven't already decided
1055 * whether or not the other side has its VALUE and VAR
1056 * reversed.
1058 if (env_ovar < 0) {
1059 int last = -1; /* invalid value */
1060 int empty = 0;
1061 int got_var = 0, got_value = 0, got_uservar = 0;
1064 * The other side might have its VALUE and VAR values
1065 * reversed. To be interoperable, we need to determine
1066 * which way it is. If the first recognized character
1067 * is a VAR or VALUE, then that will tell us what
1068 * type of client it is. If the fist recognized
1069 * character is a USERVAR, then we continue scanning
1070 * the suboption looking for two consecutive
1071 * VAR or VALUE fields. We should not get two
1072 * consecutive VALUE fields, so finding two
1073 * consecutive VALUE or VAR fields will tell us
1074 * what the client is.
1076 SB_SAVE();
1077 while (!SB_EOF()) {
1078 c = SB_GET();
1079 switch(c) {
1080 case OLD_ENV_VAR:
1081 if (last < 0 || last == OLD_ENV_VAR
1082 || (empty && (last == OLD_ENV_VALUE)))
1083 goto env_ovar_ok;
1084 got_var++;
1085 last = OLD_ENV_VAR;
1086 break;
1087 case OLD_ENV_VALUE:
1088 if (last < 0 || last == OLD_ENV_VALUE
1089 || (empty && (last == OLD_ENV_VAR)))
1090 goto env_ovar_wrong;
1091 got_value++;
1092 last = OLD_ENV_VALUE;
1093 break;
1094 case ENV_USERVAR:
1095 /* count strings of USERVAR as one */
1096 if (last != ENV_USERVAR)
1097 got_uservar++;
1098 if (empty) {
1099 if (last == OLD_ENV_VALUE)
1100 goto env_ovar_ok;
1101 if (last == OLD_ENV_VAR)
1102 goto env_ovar_wrong;
1104 last = ENV_USERVAR;
1105 break;
1106 case ENV_ESC:
1107 if (!SB_EOF())
1108 c = SB_GET();
1109 /* FALL THROUGH */
1110 default:
1111 empty = 0;
1112 continue;
1114 empty = 1;
1116 if (empty) {
1117 if (last == OLD_ENV_VALUE)
1118 goto env_ovar_ok;
1119 if (last == OLD_ENV_VAR)
1120 goto env_ovar_wrong;
1123 * Ok, the first thing was a USERVAR, and there
1124 * are not two consecutive VAR or VALUE commands,
1125 * and none of the VAR or VALUE commands are empty.
1126 * If the client has sent us a well-formed option,
1127 * then the number of VALUEs received should always
1128 * be less than or equal to the number of VARs and
1129 * USERVARs received.
1131 * If we got exactly as many VALUEs as VARs and
1132 * USERVARs, the client has the same definitions.
1134 * If we got exactly as many VARs as VALUEs and
1135 * USERVARS, the client has reversed definitions.
1137 if (got_uservar + got_var == got_value) {
1138 env_ovar_ok:
1139 env_ovar = OLD_ENV_VAR;
1140 env_ovalue = OLD_ENV_VALUE;
1141 } else if (got_uservar + got_value == got_var) {
1142 env_ovar_wrong:
1143 env_ovar = OLD_ENV_VALUE;
1144 env_ovalue = OLD_ENV_VAR;
1145 DIAG(TD_OPTIONS, {
1146 output_data("ENVIRON VALUE and VAR are reversed!\r\n");
1151 SB_RESTORE();
1152 #endif
1154 while (!SB_EOF()) {
1155 c = SB_GET();
1156 if ((c == env_ovar) || (c == ENV_USERVAR))
1157 break;
1161 if (SB_EOF())
1162 return;
1164 cp = varp = (char *)subpointer;
1165 valp = 0;
1167 while (!SB_EOF()) {
1168 c = SB_GET();
1169 if (subchar == TELOPT_OLD_ENVIRON) {
1170 if (c == env_ovar)
1171 c = NEW_ENV_VAR;
1172 else if (c == env_ovalue)
1173 c = NEW_ENV_VALUE;
1175 switch (c) {
1177 case NEW_ENV_VALUE:
1178 *cp = '\0';
1179 cp = valp = (char *)subpointer;
1180 break;
1182 case NEW_ENV_VAR:
1183 case ENV_USERVAR:
1184 *cp = '\0';
1185 if (valp)
1186 esetenv(varp, valp, 1);
1187 else
1188 unsetenv(varp);
1189 cp = varp = (char *)subpointer;
1190 valp = 0;
1191 break;
1193 case ENV_ESC:
1194 if (SB_EOF())
1195 break;
1196 c = SB_GET();
1197 /* FALL THROUGH */
1198 default:
1199 *cp++ = c;
1200 break;
1203 *cp = '\0';
1204 if (valp)
1205 esetenv(varp, valp, 1);
1206 else
1207 unsetenv(varp);
1208 break;
1209 } /* end of case TELOPT_NEW_ENVIRON */
1210 #ifdef AUTHENTICATION
1211 case TELOPT_AUTHENTICATION:
1212 if (SB_EOF())
1213 break;
1214 switch(SB_GET()) {
1215 case TELQUAL_SEND:
1216 case TELQUAL_REPLY:
1218 * These are sent by us and cannot be sent by
1219 * the client.
1221 break;
1222 case TELQUAL_IS:
1223 auth_is(subpointer, SB_LEN());
1224 break;
1225 case TELQUAL_NAME:
1226 auth_name(subpointer, SB_LEN());
1227 break;
1229 break;
1230 #endif
1231 #ifdef ENCRYPTION
1232 case TELOPT_ENCRYPT:
1233 if (SB_EOF())
1234 break;
1235 switch(SB_GET()) {
1236 case ENCRYPT_SUPPORT:
1237 encrypt_support(subpointer, SB_LEN());
1238 break;
1239 case ENCRYPT_IS:
1240 encrypt_is(subpointer, SB_LEN());
1241 break;
1242 case ENCRYPT_REPLY:
1243 encrypt_reply(subpointer, SB_LEN());
1244 break;
1245 case ENCRYPT_START:
1246 encrypt_start(subpointer, SB_LEN());
1247 break;
1248 case ENCRYPT_END:
1249 if (require_encryption)
1250 fatal(net, "Output encryption is not possible to turn off");
1251 encrypt_end();
1252 break;
1253 case ENCRYPT_REQSTART:
1254 encrypt_request_start(subpointer, SB_LEN());
1255 break;
1256 case ENCRYPT_REQEND:
1258 * We can always send an REQEND so that we cannot
1259 * get stuck encrypting. We should only get this
1260 * if we have been able to get in the correct mode
1261 * anyhow.
1263 if (require_encryption)
1264 fatal(net, "Input encryption is not possible to turn off");
1265 encrypt_request_end();
1266 break;
1267 case ENCRYPT_ENC_KEYID:
1268 encrypt_enc_keyid(subpointer, SB_LEN());
1269 break;
1270 case ENCRYPT_DEC_KEYID:
1271 encrypt_dec_keyid(subpointer, SB_LEN());
1272 break;
1273 default:
1274 break;
1276 break;
1277 #endif
1279 default:
1280 break;
1281 } /* end of switch */
1283 } /* end of suboption */
1285 void
1286 doclientstat(void)
1288 clientstat(TELOPT_LINEMODE, WILL, 0);
1291 #undef ADD
1292 #define ADD(c) *ncp++ = c
1293 #define ADD_DATA(c) { *ncp++ = c; if (c == SE || c == IAC) *ncp++ = c; }
1295 void
1296 send_status(void)
1298 unsigned char statusbuf[256];
1299 unsigned char *ncp;
1300 unsigned char i;
1302 ncp = statusbuf;
1304 netflush(); /* get rid of anything waiting to go out */
1306 ADD(IAC);
1307 ADD(SB);
1308 ADD(TELOPT_STATUS);
1309 ADD(TELQUAL_IS);
1312 * We check the want_state rather than the current state,
1313 * because if we received a DO/WILL for an option that we
1314 * don't support, and the other side didn't send a DONT/WONT
1315 * in response to our WONT/DONT, then the "state" will be
1316 * WILL/DO, and the "want_state" will be WONT/DONT. We
1317 * need to go by the latter.
1319 for (i = 0; i < (unsigned char)NTELOPTS; i++) {
1320 if (my_want_state_is_will(i)) {
1321 ADD(WILL);
1322 ADD_DATA(i);
1324 if (his_want_state_is_will(i)) {
1325 ADD(DO);
1326 ADD_DATA(i);
1330 if (his_want_state_is_will(TELOPT_LFLOW)) {
1331 ADD(SB);
1332 ADD(TELOPT_LFLOW);
1333 if (flowmode) {
1334 ADD(LFLOW_ON);
1335 } else {
1336 ADD(LFLOW_OFF);
1338 ADD(SE);
1340 if (restartany >= 0) {
1341 ADD(SB);
1342 ADD(TELOPT_LFLOW);
1343 if (restartany) {
1344 ADD(LFLOW_RESTART_ANY);
1345 } else {
1346 ADD(LFLOW_RESTART_XON);
1348 ADD(SE);
1353 ADD(IAC);
1354 ADD(SE);
1356 writenet(statusbuf, ncp - statusbuf);
1357 netflush(); /* Send it on its way */
1359 DIAG(TD_OPTIONS,
1360 {printsub('>', statusbuf, ncp - statusbuf); netflush();});