Debian Codegraph @

telnetd/state.c

raw
1 /*
2 Copyright (C) 1993-2026 Free Software Foundation, Inc.
3
4 This file is part of GNU Inetutils.
5
6 GNU Inetutils is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
10
11 GNU Inetutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see `http://www.gnu.org/licenses/'. */
18
19 /*
20 * Copyright (c) 1989, 1993
21 * The Regents of the University of California. All rights reserved.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. Neither the name of the University nor the names of its contributors
32 * may be used to endorse or promote products derived from this software
33 * without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * SUCH DAMAGE.
46 */
47
48 #include <config.h>
49
50 #include "telnetd.h"
51 #include <stdarg.h>
52
53 #if defined HAVE_STREAMSPTY && defined TIOCSIGNAL \
54 && defined HAVE_STROPTS_H
55 # include <stropts.h> /* I_FLUSH, FLUSHR */
56 #endif
57
58 #ifndef NTELOPTS /* OpenSolaris */
59 # define NTELOPTS (1+TELOPT_NEW_ENVIRON)
60 #endif
61
62 /* Format lines for corresponding commands */
63 char doopt[] = { IAC, DO, '%', 'c', 0 };
64 char dont[] = { IAC, DONT, '%', 'c', 0 };
65 char will[] = { IAC, WILL, '%', 'c', 0 };
66 char wont[] = { IAC, WONT, '%', 'c', 0 };
67
68 int not42 = 1;
69
70 /*
71 * Buffer for sub-options, and macros
72 * for suboptions buffer manipulations
73 */
74 unsigned char subbuffer[512], *subpointer = subbuffer, *subend = subbuffer;
75
76 #define SB_CLEAR() subpointer = subbuffer
77 #define SB_TERM() { subend = subpointer; SB_CLEAR(); }
78 #define SB_ACCUM(c) if (subpointer < (subbuffer+sizeof subbuffer)) { \
79 *subpointer++ = (c); \
80 }
81 #define SB_GET() ((*subpointer++)&0xff)
82 #define SB_EOF() (subpointer >= subend)
83 #define SB_LEN() (subend - subpointer)
84
85 #ifdef ENV_HACK
86 unsigned char *subsave;
87 # define SB_SAVE() subsave = subpointer;
88 # define SB_RESTORE() subpointer = subsave;
89 #endif
90
91
92 /*
93 * State for recv fsm
94 */
95 #define TS_DATA 0 /* base state */
96 #define TS_IAC 1 /* look for double IAC's */
97 #define TS_CR 2 /* CR-LF ->'s CR */
98 #define TS_SB 3 /* throw away begin's... */
99 #define TS_SE 4 /* ...end's (suboption negotiation) */
100 #define TS_WILL 5 /* will option negotiation */
101 #define TS_WONT 6 /* wont " */
102 #define TS_DO 7 /* do " */
103 #define TS_DONT 8 /* dont " */
104
105 static void
106 send_eof (void)
107 {
108 init_termbuf ();
109 term_send_eof ();
110 pty_output_byte (slctab[SLC_EOF].sptr ?
111 (unsigned char) *slctab[SLC_EOF].sptr : '\004');
112 }
113
114 /*
115 * When we get an AYT, if ^T is enabled, use that. Otherwise,
116 * just send back "[Yes]".
117 */
118 static void
119 recv_ayt (void)
120 {
121 #if defined SIGINFO && defined TCSIG
122 if (slctab[SLC_AYT].sptr && *slctab[SLC_AYT].sptr != _POSIX_VDISABLE)
123 {
124 ioctl (pty, TCSIG, (char *) SIGINFO);
125 return;
126 }
127 #endif
128 net_output_data ("\r\n[Yes]\r\n");
129 }
130
131 static void
132 send_susp (void)
133 {
134 #ifdef SIGTSTP
135 ptyflush (); /* half-hearted */
136 # ifdef TCSIG
137 ioctl (pty, TCSIG, (char *) SIGTSTP);
138 # else/* TCSIG */
139 pty_output_byte (slctab[SLC_SUSP].sptr ?
140 (unsigned char) *slctab[SLC_SUSP].sptr : '\032');
141 # endif/* TCSIG */
142 #endif /* SIGTSTP */
143 }
144
145 /*
146 * Send quit to process on other side of pty.
147 * If it is in raw mode, just write NULL;
148 * otherwise, write quit char.
149 */
150 static void
151 send_brk (void)
152 {
153 ptyflush (); /* half-hearted */
154 #ifdef TCSIG
155 ioctl (pty, TCSIG, (char *) SIGQUIT);
156 #else /* TCSIG */
157 init_termbuf ();
158 pty_output_byte (slctab[SLC_ABORT].sptr ?
159 (unsigned char) *slctab[SLC_ABORT].sptr : '\034');
160 #endif /* TCSIG */
161 }
162
163 /*
164 * Send interrupt to process on other side of pty.
165 * If it is in raw mode, just write NULL;
166 * otherwise, write intr char.
167 */
168 static void
169 send_intr (void)
170 {
171 ptyflush (); /* half-hearted */
172
173 #if defined HAVE_STREAMSPTY && defined TIOCSIGNAL
174 /* Streams PTY style ioctl to post a signal */
175 {
176 int sig = SIGINT;
177 ioctl (pty, TIOCSIGNAL, &sig);
178 ioctl (pty, I_FLUSH, FLUSHR);
179 }
180 #else
181 # ifdef TCSIG
182 ioctl (pty, TCSIG, (char *) SIGINT);
183 # else/* TCSIG */
184 init_termbuf ();
185 pty_output_byte (slctab[SLC_IP].sptr ?
186 (unsigned char) *slctab[SLC_IP].sptr : '\177');
187 # endif/* TCSIG */
188 #endif
189 }
190
191 void
192 telrcv (void)
193 {
194 int c;
195 static int state = TS_DATA;
196
197 while ((net_input_level () > 0) & !pty_buffer_is_full ())
198 {
199 c = net_get_char (0);
200 #ifdef ENCRYPTION
201 if (decrypt_input)
202 c = (*decrypt_input) (c);
203 #endif /* ENCRYPTION */
204 switch (state)
205 {
206
207 case TS_CR:
208 state = TS_DATA;
209 /* Strip off \n or \0 after a \r */
210 if ((c == 0) || (c == '\n'))
211 break;
212 /* FALL THROUGH */
213
214 case TS_DATA:
215 if (c == IAC)
216 {
217 state = TS_IAC;
218 break;
219 }
220 /*
221 * We now map \r\n ==> \r for pragmatic reasons.
222 * Many client implementations send \r\n when
223 * the user hits the CarriageReturn key.
224 *
225 * We USED to map \r\n ==> \n, since \r\n says
226 * that we want to be in column 1 of the next
227 * printable line, and \n is the standard
228 * unix way of saying that (\r is only good
229 * if CRMOD is set, which it normally is).
230 */
231 if ((c == '\r') && his_state_is_wont (TELOPT_BINARY))
232 {
233 int nc = net_get_char (1);
234 #ifdef ENCRYPTION
235 if (decrypt_input)
236 nc = (*decrypt_input) (nc & 0xff);
237 #endif /* ENCRYPTION */
238 /*
239 * If we are operating in linemode,
240 * convert to local end-of-line.
241 */
242 if (linemode
243 && net_input_level () > 0
244 && (('\n' == nc) || (!nc && tty_iscrnl ())))
245 {
246 net_get_char (0); /* Remove from the buffer */
247 c = '\n';
248 }
249 else
250 {
251 #ifdef ENCRYPTION
252 if (decrypt_input)
253 (*decrypt_input) (-1);
254 #endif /* ENCRYPTION */
255 state = TS_CR;
256 }
257 }
258 pty_output_byte (c);
259 break;
260
261 case TS_IAC:
262 gotiac:
263 switch (c)
264 {
265
266 /*
267 * Send the process on the pty side an
268 * interrupt. Do this with a NULL or
269 * interrupt char; depending on the tty mode.
270 */
271 case IP:
272 send_intr ();
273 break;
274
275 case BREAK:
276 send_brk ();
277 break;
278
279 /*
280 * Are You There?
281 */
282 case AYT:
283 recv_ayt ();
284 break;
285
286 /*
287 * Abort Output
288 */
289 case AO:
290 {
291 ptyflush (); /* half-hearted */
292 init_termbuf ();
293
294 if (slctab[SLC_AO].sptr
295 && *slctab[SLC_AO].sptr != (cc_t) (_POSIX_VDISABLE))
296 pty_output_byte (*slctab[SLC_AO].sptr);
297
298 netclear (); /* clear buffer back */
299 net_output_data ("%c%c", IAC, DM);
300 set_neturg ();
301 break;
302 }
303
304 /*
305 * Erase Character and
306 * Erase Line
307 */
308 case EC:
309 case EL:
310 {
311 cc_t ch = (cc_t) (_POSIX_VDISABLE);
312
313 ptyflush (); /* half-hearted */
314 init_termbuf ();
315 if (c == EC)
316 {
317 if (slctab[SLC_EC].sptr)
318 ch = *slctab[SLC_EC].sptr;
319 }
320 else
321 {
322 if (slctab[SLC_EL].sptr)
323 ch = *slctab[SLC_EL].sptr;
324 }
325 if (ch != (cc_t) (_POSIX_VDISABLE))
326 pty_output_byte ((unsigned char) ch);
327 break;
328 }
329
330 /*
331 * Check for urgent data...
332 */
333 case DM:
334 SYNCHing = stilloob (net);
335 settimer (gotDM);
336 break;
337
338 /*
339 * Begin option subnegotiation...
340 */
341 case SB:
342 state = TS_SB;
343 SB_CLEAR ();
344 continue;
345
346 case WILL:
347 state = TS_WILL;
348 continue;
349
350 case WONT:
351 state = TS_WONT;
352 continue;
353
354 case DO:
355 state = TS_DO;
356 continue;
357
358 case DONT:
359 state = TS_DONT;
360 continue;
361 case EOR:
362 if (his_state_is_will (TELOPT_EOR))
363 send_eof ();
364 break;
365
366 /*
367 * Handle RFC 10xx Telnet linemode option additions
368 * to command stream (EOF, SUSP, ABORT).
369 */
370 case xEOF:
371 send_eof ();
372 break;
373
374 case SUSP:
375 send_susp ();
376 break;
377
378 case ABORT:
379 send_brk ();
380 break;
381
382 case IAC:
383 pty_output_byte (c);
384 break;
385 }
386 state = TS_DATA;
387 break;
388
389 case TS_SB:
390 if (c == IAC)
391 state = TS_SE;
392 else
393 SB_ACCUM (c);
394 break;
395
396 case TS_SE:
397 if (c != SE)
398 {
399 if (c != IAC)
400 {
401 /*
402 * bad form of suboption negotiation.
403 * handle it in such a way as to avoid
404 * damage to local state. Parse
405 * suboption buffer found so far,
406 * then treat remaining stream as
407 * another command sequence.
408 */
409
410 /* for DIAGNOSTICS */
411 SB_ACCUM (IAC);
412 SB_ACCUM (c);
413 subpointer -= 2;
414
415 SB_TERM ();
416 suboption ();
417 state = TS_IAC;
418 goto gotiac;
419 }
420 SB_ACCUM (c);
421 state = TS_SB;
422 }
423 else
424 {
425 /* for DIAGNOSTICS */
426 SB_ACCUM (IAC);
427 SB_ACCUM (SE);
428 subpointer -= 2;
429
430 SB_TERM ();
431 suboption (); /* handle sub-option */
432 state = TS_DATA;
433 }
434 break;
435
436 case TS_WILL:
437 willoption (c);
438 state = TS_DATA;
439 continue;
440
441 case TS_WONT:
442 wontoption (c);
443 state = TS_DATA;
444 continue;
445
446 case TS_DO:
447 dooption (c);
448 state = TS_DATA;
449 continue;
450
451 case TS_DONT:
452 dontoption (c);
453 state = TS_DATA;
454 continue;
455
456 default:
457 syslog (LOG_ERR, "telnetd: panic state=%d\n", state);
458 printf ("telnetd: panic state=%d\n", state);
459 exit (EXIT_FAILURE);
460 }
461 }
462 } /* end of telrcv */
463
464 /*
465 * The will/wont/do/dont state machines are based on Dave Borman's
466 * Telnet option processing state machine.
467 *
468 * These correspond to the following states:
469 * my_state = the last negotiated state
470 * want_state = what I want the state to go to
471 * want_resp = how many requests I have sent
472 * All state defaults are negative, and resp defaults to 0.
473 *
474 * When initiating a request to change state to new_state:
475 *
476 * if ((want_resp == 0 && new_state == my_state) || want_state == new_state) {
477 * do nothing;
478 * } else {
479 * want_state = new_state;
480 * send new_state;
481 * want_resp++;
482 * }
483 *
484 * When receiving new_state:
485 *
486 * if (want_resp) {
487 * want_resp--;
488 * if (want_resp && (new_state == my_state))
489 * want_resp--;
490 * }
491 * if ((want_resp == 0) && (new_state != want_state)) {
492 * if (ok_to_switch_to new_state)
493 * want_state = new_state;
494 * else
495 * want_resp++;
496 * send want_state;
497 * }
498 * my_state = new_state;
499 *
500 * Note that new_state is implied in these functions by the function itself.
501 * will and do imply positive new_state, wont and dont imply negative.
502 *
503 * Finally, there is one catch. If we send a negative response to a
504 * positive request, my_state will be the positive while want_state will
505 * remain negative. my_state will revert to negative when the negative
506 * acknowledgment arrives from the peer. Thus, my_state generally tells
507 * us not only the last negotiated state, but also tells us what the peer
508 * wants to be doing as well. It is important to understand this difference
509 * as we may wish to be processing data streams based on our desired state
510 * (want_state) or based on what the peer thinks the state is (my_state).
511 *
512 * This all works fine because if the peer sends a positive request, the data
513 * that we receive prior to negative acknowledgment will probably be affected
514 * by the positive state, and we can process it as such (if we can; if we
515 * can't then it really doesn't matter). If it is that important, then the
516 * peer probably should be buffering until this option state negotiation
517 * is complete.
518 *
519 */
520 void
521 send_do (int option, int init)
522 {
523 if (init)
524 {
525 if ((do_dont_resp[option] == 0 && his_state_is_will (option)) ||
526 his_want_state_is_will (option))
527 return;
528 /*
529 * Special case for TELOPT_TM: We send a DO, but pretend
530 * that we sent a DONT, so that we can send more DOs if
531 * we want to.
532 */
533 if (option == TELOPT_TM)
534 set_his_want_state_wont (option);
535 else
536 set_his_want_state_will (option);
537 do_dont_resp[option]++;
538 }
539 net_output_data (doopt, option);
540 }
541
542 #ifdef AUTHENTICATION
543 extern void auth_request (void);
544 #endif
545 extern void doclientstat (void);
546 #ifdef ENCRYPTION
547 extern void encrypt_send_support (void);
548 #endif /* ENCRYPTION */
549
550 void
551 willoption (int option)
552 {
553 int changeok = 0;
554 void (*func) (void) = NULL;
555
556 /*
557 * process input from peer.
558 */
559
560 if (do_dont_resp[option])
561 {
562 do_dont_resp[option]--;
563 if (do_dont_resp[option] && his_state_is_will (option))
564 do_dont_resp[option]--;
565 }
566 if (do_dont_resp[option] == 0)
567 {
568 if (his_want_state_is_wont (option))
569 {
570 switch (option)
571 {
572
573 case TELOPT_BINARY:
574 init_termbuf ();
575 tty_binaryin (1);
576 set_termbuf ();
577 changeok++;
578 break;
579
580 case TELOPT_ECHO:
581 /*
582 * See comments below for more info.
583 */
584 not42 = 0; /* looks like a 4.2 system */
585 break;
586
587 case TELOPT_TM:
588 /*
589 * This telnetd implementation does not really
590 * support timing marks, it just uses them to
591 * support the kludge linemode stuff. If we
592 * receive a will or wont TM in response to our
593 * do TM request that may have been sent to
594 * determine kludge linemode support, process
595 * it, otherwise TM should get a negative
596 * response back.
597 */
598 /*
599 * Handle the linemode kludge stuff.
600 * If we are not currently supporting any
601 * linemode at all, then we assume that this
602 * is the client telling us to use kludge
603 * linemode in response to our query. Set the
604 * linemode type that is to be supported, note
605 * that the client wishes to use linemode, and
606 * eat the will TM as though it never arrived.
607 */
608 if (lmodetype < KLUDGE_LINEMODE)
609 {
610 lmodetype = KLUDGE_LINEMODE;
611 clientstat (TELOPT_LINEMODE, WILL, 0);
612 send_wont (TELOPT_SGA, 1);
613 }
614 else if (lmodetype == NO_AUTOKLUDGE)
615 lmodetype = KLUDGE_OK;
616
617 /*
618 * We never respond to a WILL TM, and
619 * we leave the state WONT.
620 */
621 return;
622
623 case TELOPT_LFLOW:
624 /*
625 * If we are going to support flow control
626 * option, then don't worry peer that we can't
627 * change the flow control characters.
628 */
629 slctab[SLC_XON].defset.flag &= ~SLC_LEVELBITS;
630 slctab[SLC_XON].defset.flag |= SLC_DEFAULT;
631 slctab[SLC_XOFF].defset.flag &= ~SLC_LEVELBITS;
632 slctab[SLC_XOFF].defset.flag |= SLC_DEFAULT;
633 /* FALLTHROUGH */
634 case TELOPT_TTYPE:
635 case TELOPT_SGA:
636 case TELOPT_NAWS:
637 case TELOPT_TSPEED:
638 case TELOPT_XDISPLOC:
639 case TELOPT_NEW_ENVIRON:
640 case TELOPT_OLD_ENVIRON:
641 changeok++;
642 break;
643
644 case TELOPT_LINEMODE:
645 /*
646 * Note client's desire to use linemode.
647 */
648 lmodetype = REAL_LINEMODE;
649 func = doclientstat;
650 changeok++;
651 break;
652
653 #ifdef AUTHENTICATION
654 case TELOPT_AUTHENTICATION:
655 func = auth_request;
656 changeok++;
657 break;
658 #endif
659
660 #ifdef ENCRYPTION
661 case TELOPT_ENCRYPT:
662 func = encrypt_send_support;
663 changeok++;
664 break;
665 #endif /* ENCRYPTION */
666
667 default:
668 break;
669 }
670 if (changeok)
671 {
672 set_his_want_state_will (option);
673 send_do (option, 0);
674 }
675 else
676 {
677 do_dont_resp[option]++;
678 send_dont (option, 0);
679 }
680 }
681 else
682 {
683 /*
684 * Option processing that should happen when
685 * we receive conformation of a change in
686 * state that we had requested.
687 */
688 switch (option)
689 {
690 case TELOPT_ECHO:
691 not42 = 0; /* looks like a 4.2 system */
692 /*
693 * Egads, he responded "WILL ECHO". Turn
694 * it off right now!
695 */
696 send_dont (option, 1);
697 /*
698 * "WILL ECHO". Kludge upon kludge!
699 * A 4.2 client is now echoing user input at
700 * the tty. This is probably undesirable and
701 * it should be stopped. The client will
702 * respond WONT TM to the DO TM that we send to
703 * check for kludge linemode. When the WONT TM
704 * arrives, linemode will be turned off and a
705 * change propagated to the pty. This change
706 * will cause us to process the new pty state
707 * in localstat(), which will notice that
708 * linemode is off and send a WILL ECHO
709 * so that we are properly in character mode and
710 * all is well.
711 */
712 break;
713 case TELOPT_LINEMODE:
714 /*
715 * Note client's desire to use linemode.
716 */
717 lmodetype = REAL_LINEMODE;
718 func = doclientstat;
719 break;
720
721 #ifdef AUTHENTICATION
722 case TELOPT_AUTHENTICATION:
723 func = auth_request;
724 break;
725 #endif
726
727 #ifdef ENCRYPTION
728 case TELOPT_ENCRYPT:
729 func = encrypt_send_support;
730 break;
731 #endif /* ENCRYPTION */
732 case TELOPT_LFLOW:
733 func = flowstat;
734 break;
735 }
736 }
737 }
738 set_his_state_will (option);
739 if (func)
740 (*func) ();
741 } /* end of willoption */
742
743 void
744 send_dont (int option, int init)
745 {
746 if (init)
747 {
748 if ((do_dont_resp[option] == 0 && his_state_is_wont (option)) ||
749 his_want_state_is_wont (option))
750 return;
751 set_his_want_state_wont (option);
752 do_dont_resp[option]++;
753 }
754 net_output_data (dont, option);
755 }
756
757 void
758 wontoption (int option)
759 {
760 /*
761 * Process client input.
762 */
763
764 if (do_dont_resp[option])
765 {
766 do_dont_resp[option]--;
767 if (do_dont_resp[option] && his_state_is_wont (option))
768 do_dont_resp[option]--;
769 }
770 if (do_dont_resp[option] == 0)
771 {
772 if (his_want_state_is_will (option))
773 {
774 /* it is always ok to change to negative state */
775 switch (option)
776 {
777 case TELOPT_ECHO:
778 not42 = 1; /* doesn't seem to be a 4.2 system */
779 break;
780
781 case TELOPT_BINARY:
782 init_termbuf ();
783 tty_binaryin (0);
784 set_termbuf ();
785 break;
786
787 case TELOPT_LINEMODE:
788 /*
789 * If real linemode is supported, then client is
790 * asking to turn linemode off.
791 */
792 if (lmodetype != REAL_LINEMODE)
793 break;
794 clientstat (TELOPT_LINEMODE, WONT, 0);
795 break;
796
797 case TELOPT_TM:
798 /*
799 * If we get a WONT TM, and had sent a DO TM,
800 * don't respond with a DONT TM, just leave it
801 * as is. Short circuit the state machine to
802 * achieve this.
803 */
804 set_his_want_state_wont (TELOPT_TM);
805 return;
806
807 case TELOPT_LFLOW:
808 /*
809 * If we are not going to support flow control
810 * option, then let peer know that we can't
811 * change the flow control characters.
812 */
813 slctab[SLC_XON].defset.flag &= ~SLC_LEVELBITS;
814 slctab[SLC_XON].defset.flag |= SLC_CANTCHANGE;
815 slctab[SLC_XOFF].defset.flag &= ~SLC_LEVELBITS;
816 slctab[SLC_XOFF].defset.flag |= SLC_CANTCHANGE;
817 break;
818
819 #if defined AUTHENTICATION
820 case TELOPT_AUTHENTICATION:
821 auth_finished (0, AUTH_REJECT);
822 break;
823 #endif
824
825 /*
826 * For options that we might spin waiting for
827 * sub-negotiation, if the client turns off the
828 * option rather than responding to the request,
829 * we have to treat it here as if we got a response
830 * to the sub-negotiation, (by updating the timers)
831 * so that we'll break out of the loop.
832 */
833 case TELOPT_TTYPE:
834 settimer (ttypesubopt);
835 break;
836
837 case TELOPT_TSPEED:
838 settimer (tspeedsubopt);
839 break;
840
841 case TELOPT_XDISPLOC:
842 settimer (xdisplocsubopt);
843 break;
844
845 case TELOPT_OLD_ENVIRON:
846 settimer (oenvironsubopt);
847 break;
848
849 case TELOPT_NEW_ENVIRON:
850 settimer (environsubopt);
851 break;
852
853 default:
854 break;
855 }
856 set_his_want_state_wont (option);
857 if (his_state_is_will (option))
858 send_dont (option, 0);
859 }
860 else
861 {
862 switch (option)
863 {
864 case TELOPT_TM:
865 if (lmodetype < NO_AUTOKLUDGE)
866 {
867 lmodetype = NO_LINEMODE;
868 clientstat (TELOPT_LINEMODE, WONT, 0);
869 send_will (TELOPT_SGA, 1);
870 send_will (TELOPT_ECHO, 1);
871 }
872 break;
873
874 #if defined AUTHENTICATION
875 case TELOPT_AUTHENTICATION:
876 auth_finished (0, AUTH_REJECT);
877 break;
878 #endif
879 default:
880 break;
881 }
882 }
883 }
884 set_his_state_wont (option);
885
886 } /* end of wontoption */
887
888 void
889 send_will (int option, int init)
890 {
891 if (init)
892 {
893 if ((will_wont_resp[option] == 0 && my_state_is_will (option)) ||
894 my_want_state_is_will (option))
895 return;
896 set_my_want_state_will (option);
897 will_wont_resp[option]++;
898 }
899 net_output_data (will, option);
900 }
901
902 void
903 dooption (int option)
904 {
905 int changeok = 0;
906
907 /*
908 * Process client input.
909 */
910
911 if (will_wont_resp[option])
912 {
913 will_wont_resp[option]--;
914 if (will_wont_resp[option] && my_state_is_will (option))
915 will_wont_resp[option]--;
916 }
917 if ((will_wont_resp[option] == 0) && (my_want_state_is_wont (option)))
918 {
919 switch (option)
920 {
921 case TELOPT_ECHO:
922 if (lmodetype == NO_LINEMODE)
923 {
924 init_termbuf ();
925 tty_setecho (1);
926 set_termbuf ();
927 }
928 changeok++;
929 break;
930
931 case TELOPT_BINARY:
932 init_termbuf ();
933 tty_binaryout (1);
934 set_termbuf ();
935 changeok++;
936 break;
937
938 case TELOPT_SGA:
939 /*
940 * If kludge linemode is in use, then we must
941 * process an incoming do SGA for linemode
942 * purposes.
943 */
944 if (lmodetype == KLUDGE_LINEMODE)
945 {
946 /*
947 * Receipt of "do SGA" in kludge
948 * linemode is the peer asking us to
949 * turn off linemode. Make note of
950 * the request.
951 */
952 clientstat (TELOPT_LINEMODE, WONT, 0);
953 /*
954 * If linemode did not get turned off
955 * then don't tell peer that we did.
956 * Breaking here forces a wont SGA to
957 * be returned.
958 */
959 if (linemode)
960 break;
961 }
962 changeok++;
963 break;
964
965 case TELOPT_STATUS:
966 changeok++;
967 break;
968
969 case TELOPT_TM:
970 /*
971 * Special case for TM. We send a WILL, but
972 * pretend we sent a WONT.
973 */
974 send_will (option, 0);
975 set_my_want_state_wont (option);
976 set_my_state_wont (option);
977 return;
978
979 case TELOPT_LOGOUT:
980 /*
981 * When we get a LOGOUT option, respond
982 * with a WILL LOGOUT, make sure that
983 * it gets written out to the network,
984 * and then just go away...
985 */
986 set_my_want_state_will (TELOPT_LOGOUT);
987 send_will (TELOPT_LOGOUT, 0);
988 set_my_state_will (TELOPT_LOGOUT);
989 netflush ();
990 cleanup (SIGHUP);
991 /* NOT REACHED */
992 break;
993
994 #ifdef ENCRYPTION
995 case TELOPT_ENCRYPT:
996 changeok++;
997 break;
998 #endif /* ENCRYPTION */
999 case TELOPT_LINEMODE:
1000 case TELOPT_TTYPE:
1001 case TELOPT_NAWS:
1002 case TELOPT_TSPEED:
1003 case TELOPT_LFLOW:
1004 case TELOPT_XDISPLOC:
1005 case TELOPT_NEW_ENVIRON:
1006 case TELOPT_OLD_ENVIRON:
1007 default:
1008 break;
1009 }
1010 if (changeok)
1011 {
1012 set_my_want_state_will (option);
1013 send_will (option, 0);
1014 }
1015 else
1016 {
1017 will_wont_resp[option]++;
1018 send_wont (option, 0);
1019 }
1020 }
1021 set_my_state_will (option);
1022
1023 } /* end of dooption */
1024
1025 void
1026 send_wont (int option, int init)
1027 {
1028 if (init)
1029 {
1030 if ((will_wont_resp[option] == 0 && my_state_is_wont (option)) ||
1031 my_want_state_is_wont (option))
1032 return;
1033 set_my_want_state_wont (option);
1034 will_wont_resp[option]++;
1035 }
1036 net_output_data (wont, option);
1037 }
1038
1039 void
1040 dontoption (int option)
1041 {
1042 /*
1043 * Process client input.
1044 */
1045
1046 if (will_wont_resp[option])
1047 {
1048 will_wont_resp[option]--;
1049 if (will_wont_resp[option] && my_state_is_wont (option))
1050 will_wont_resp[option]--;
1051 }
1052 if ((will_wont_resp[option] == 0) && (my_want_state_is_will (option)))
1053 {
1054 switch (option)
1055 {
1056 case TELOPT_BINARY:
1057 init_termbuf ();
1058 tty_binaryout (0);
1059 set_termbuf ();
1060 break;
1061
1062 case TELOPT_ECHO: /* we should stop echoing */
1063 if ((lmodetype != REAL_LINEMODE) && (lmodetype != KLUDGE_LINEMODE))
1064 {
1065 init_termbuf ();
1066 tty_setecho (0);
1067 set_termbuf ();
1068 }
1069 break;
1070
1071 case TELOPT_SGA:
1072 /*
1073 * If kludge linemode is in use, then we
1074 * must process an incoming do SGA for
1075 * linemode purposes.
1076 */
1077 if ((lmodetype == KLUDGE_LINEMODE) || (lmodetype == KLUDGE_OK))
1078 {
1079 /*
1080 * The client is asking us to turn
1081 * linemode on.
1082 */
1083 lmodetype = KLUDGE_LINEMODE;
1084 clientstat (TELOPT_LINEMODE, WILL, 0);
1085 /*
1086 * If we did not turn line mode on,
1087 * then what do we say? Will SGA?
1088 * This violates design of telnet.
1089 * Gross. Very Gross.
1090 */
1091 }
1092 break;
1093
1094 default:
1095 break;
1096 }
1097
1098 set_my_want_state_wont (option);
1099 if (my_state_is_will (option))
1100 send_wont (option, 0);
1101 }
1102 set_my_state_wont (option);
1103
1104 } /* end of dontoption */
1105
1106 #ifdef ENV_HACK
1107 int env_ovar = -1;
1108 int env_ovalue = -1;
1109 #else /* ENV_HACK */
1110 # define env_ovar OLD_ENV_VAR
1111 # define env_ovalue OLD_ENV_VALUE
1112 #endif /* ENV_HACK */
1113
1114 /*
1115 * suboption()
1116 *
1117 * Look at the sub-option buffer, and try to be helpful to the other
1118 * side.
1119 *
1120 * Currently we recognize:
1121 *
1122 * Terminal type is
1123 * Linemode
1124 * Window size
1125 * Terminal speed
1126 */
1127 void
1128 suboption (void)
1129 {
1130 int subchar;
1131
1132 subchar = SB_GET ();
1133 switch (subchar)
1134 {
1135 case TELOPT_TSPEED:
1136 {
1137 int xspeed, rspeed;
1138
1139 if (his_state_is_wont (TELOPT_TSPEED)) /* Ignore if option disabled */
1140 break;
1141
1142 settimer (tspeedsubopt);
1143
1144 if (SB_EOF () || SB_GET () != TELQUAL_IS)
1145 return;
1146
1147 xspeed = atoi ((char *) subpointer);
1148
1149 while (SB_GET () != ',' && !SB_EOF ());
1150 if (SB_EOF ())
1151 return;
1152
1153 rspeed = atoi ((char *) subpointer);
1154 clientstat (TELOPT_TSPEED, xspeed, rspeed);
1155
1156 break;
1157
1158 } /* end of case TELOPT_TSPEED */
1159
1160 case TELOPT_TTYPE:
1161 {
1162 static struct obstack stk;
1163 char *p;
1164
1165 if (his_state_is_wont (TELOPT_TTYPE)) /* Ignore if option disabled */
1166 break;
1167
1168 settimer (ttypesubopt);
1169
1170 if (SB_EOF () || SB_GET () != TELQUAL_IS)
1171 return; /* ??? XXX but, this is the most robust */
1172
1173 free (terminaltype);
1174
1175 obstack_init (&stk);
1176 while (!SB_EOF ())
1177 {
1178 int c = tolower (SB_GET ());
1179 obstack_1grow (&stk, c);
1180 }
1181 obstack_1grow (&stk, 0);
1182 p = obstack_finish (&stk);
1183 terminaltype = xstrdup (p);
1184 obstack_free (&stk, NULL);
1185 break;
1186 } /* end of case TELOPT_TTYPE */
1187
1188 case TELOPT_NAWS:
1189 {
1190 int xwinsize, ywinsize;
1191
1192 if (his_state_is_wont (TELOPT_NAWS)) /* Ignore if option disabled */
1193 break;
1194
1195 if (SB_EOF ())
1196 return;
1197 xwinsize = SB_GET () << 8;
1198 if (SB_EOF ())
1199 return;
1200 xwinsize |= SB_GET ();
1201 if (SB_EOF ())
1202 return;
1203 ywinsize = SB_GET () << 8;
1204 if (SB_EOF ())
1205 return;
1206 ywinsize |= SB_GET ();
1207 clientstat (TELOPT_NAWS, xwinsize, ywinsize);
1208
1209 break;
1210
1211 } /* end of case TELOPT_NAWS */
1212
1213 case TELOPT_LINEMODE:
1214 {
1215 int request;
1216
1217 /* Ignore if option disabled */
1218 if (his_state_is_wont (TELOPT_LINEMODE))
1219 break;
1220 /*
1221 * Process linemode suboptions.
1222 */
1223 if (SB_EOF ())
1224 break; /* garbage was sent */
1225 request = SB_GET (); /* get will/wont */
1226
1227 if (SB_EOF ())
1228 break; /* another garbage check */
1229
1230 if (request == LM_SLC)
1231 { /* SLC is not preceded by WILL or WONT */
1232 /*
1233 * Process suboption buffer of slc's
1234 */
1235 start_slc (1);
1236 do_opt_slc (subpointer, subend - subpointer);
1237 end_slc (0);
1238 break;
1239 }
1240 else if (request == LM_MODE)
1241 {
1242 if (SB_EOF ())
1243 return;
1244 useeditmode = SB_GET (); /* get mode flag */
1245 clientstat (LM_MODE, 0, 0);
1246 break;
1247 }
1248
1249 if (SB_EOF ())
1250 break;
1251 switch (SB_GET ())
1252 { /* what suboption? */
1253 case LM_FORWARDMASK:
1254 /*
1255 * According to spec, only server can send request for
1256 * forwardmask, and client can only return a positive response.
1257 * So don't worry about it.
1258 */
1259
1260 default:
1261 break;
1262 }
1263 break;
1264 } /* end of case TELOPT_LINEMODE */
1265
1266 case TELOPT_STATUS:
1267 {
1268 int mode;
1269
1270 if (SB_EOF ())
1271 break;
1272 mode = SB_GET ();
1273 switch (mode)
1274 {
1275 case TELQUAL_SEND:
1276 if (my_state_is_will (TELOPT_STATUS))
1277 send_status ();
1278 break;
1279
1280 case TELQUAL_IS:
1281 break;
1282
1283 default:
1284 break;
1285 }
1286 break;
1287 } /* end of case TELOPT_STATUS */
1288
1289 case TELOPT_XDISPLOC:
1290 {
1291 if (SB_EOF () || SB_GET () != TELQUAL_IS)
1292 return;
1293 settimer (xdisplocsubopt);
1294 subpointer[SB_LEN ()] = '\0';
1295 setenv ("DISPLAY", (char *) subpointer, 1);
1296 break;
1297 } /* end of case TELOPT_XDISPLOC */
1298
1299 case TELOPT_NEW_ENVIRON:
1300 case TELOPT_OLD_ENVIRON:
1301 {
1302 int c;
1303 char *cp, *varp, *valp;
1304
1305 if (SB_EOF ())
1306 return;
1307 c = SB_GET ();
1308 if (c == TELQUAL_IS)
1309 {
1310 if (subchar == TELOPT_OLD_ENVIRON)
1311 settimer (oenvironsubopt);
1312 else
1313 settimer (environsubopt);
1314 }
1315 else if (c != TELQUAL_INFO)
1316 return;
1317
1318 if (subchar == TELOPT_NEW_ENVIRON)
1319 {
1320 while (!SB_EOF ())
1321 {
1322 c = SB_GET ();
1323 if ((c == NEW_ENV_VAR) || (c == ENV_USERVAR))
1324 break;
1325 }
1326 }
1327 else
1328 {
1329 #ifdef ENV_HACK
1330 /*
1331 * We only want to do this if we haven't already decided
1332 * whether or not the other side has its VALUE and VAR
1333 * reversed.
1334 */
1335 if (env_ovar < 0)
1336 {
1337 int last = -1; /* invalid value */
1338 int empty = 0;
1339 int got_var = 0, got_value = 0, got_uservar = 0;
1340
1341 /*
1342 * The other side might have its VALUE and VAR values
1343 * reversed. To be interoperable, we need to determine
1344 * which way it is. If the first recognized character
1345 * is a VAR or VALUE, then that will tell us what
1346 * type of client it is. If the fist recognized
1347 * character is a USERVAR, then we continue scanning
1348 * the suboption looking for two consecutive
1349 * VAR or VALUE fields. We should not get two
1350 * consecutive VALUE fields, so finding two
1351 * consecutive VALUE or VAR fields will tell us
1352 * what the client is.
1353 */
1354 SB_SAVE ();
1355 while (!SB_EOF ())
1356 {
1357 c = SB_GET ();
1358 switch (c)
1359 {
1360 case OLD_ENV_VAR:
1361 if (last < 0 || last == OLD_ENV_VAR
1362 || (empty && (last == OLD_ENV_VALUE)))
1363 goto env_ovar_ok;
1364 got_var++;
1365 last = OLD_ENV_VAR;
1366 break;
1367 case OLD_ENV_VALUE:
1368 if (last < 0 || last == OLD_ENV_VALUE
1369 || (empty && (last == OLD_ENV_VAR)))
1370 goto env_ovar_wrong;
1371 got_value++;
1372 last = OLD_ENV_VALUE;
1373 break;
1374 case ENV_USERVAR:
1375 /* count strings of USERVAR as one */
1376 if (last != ENV_USERVAR)
1377 got_uservar++;
1378 if (empty)
1379 {
1380 if (last == OLD_ENV_VALUE)
1381 goto env_ovar_ok;
1382 if (last == OLD_ENV_VAR)
1383 goto env_ovar_wrong;
1384 }
1385 last = ENV_USERVAR;
1386 break;
1387 case ENV_ESC:
1388 if (!SB_EOF ())
1389 c = SB_GET ();
1390 /* FALL THROUGH */
1391 default:
1392 empty = 0;
1393 continue;
1394 }
1395 empty = 1;
1396 }
1397 if (empty)
1398 {
1399 if (last == OLD_ENV_VALUE)
1400 goto env_ovar_ok;
1401 if (last == OLD_ENV_VAR)
1402 goto env_ovar_wrong;
1403 }
1404 /*
1405 * Ok, the first thing was a USERVAR, and there
1406 * are not two consecutive VAR or VALUE commands,
1407 * and none of the VAR or VALUE commands are empty.
1408 * If the client has sent us a well-formed option,
1409 * then the number of VALUEs received should always
1410 * be less than or equal to the number of VARs and
1411 * USERVARs received.
1412 *
1413 * If we got exactly as many VALUEs as VARs and
1414 * USERVARs, the client has the same definitions.
1415 *
1416 * If we got exactly as many VARs as VALUEs and
1417 * USERVARS, the client has reversed definitions.
1418 */
1419 if (got_uservar + got_var == got_value)
1420 {
1421 env_ovar_ok:
1422 env_ovar = OLD_ENV_VAR;
1423 env_ovalue = OLD_ENV_VALUE;
1424 }
1425 else if (got_uservar + got_value == got_var)
1426 {
1427 env_ovar_wrong:
1428 env_ovar = OLD_ENV_VALUE;
1429 env_ovalue = OLD_ENV_VAR;
1430 }
1431 }
1432 SB_RESTORE ();
1433 #endif
1434
1435 while (!SB_EOF ())
1436 {
1437 c = SB_GET ();
1438 if ((c == env_ovar) || (c == ENV_USERVAR))
1439 break;
1440 }
1441 }
1442
1443 if (SB_EOF ())
1444 return;
1445
1446 cp = varp = (char *) subpointer;
1447 valp = 0;
1448
1449 while (!SB_EOF ())
1450 {
1451 c = SB_GET ();
1452 if (subchar == TELOPT_OLD_ENVIRON)
1453 {
1454 if (c == env_ovar)
1455 c = NEW_ENV_VAR;
1456 else if (c == env_ovalue)
1457 c = NEW_ENV_VALUE;
1458 }
1459 switch (c)
1460 {
1461
1462 case NEW_ENV_VALUE:
1463 *cp = '\0';
1464 cp = valp = (char *) subpointer;
1465 break;
1466
1467 case NEW_ENV_VAR:
1468 case ENV_USERVAR:
1469 *cp = '\0';
1470 if (accept_env_set && gl_set_search (accept_env_set, varp))
1471 {
1472 if (valp)
1473 setenv (varp, valp, 1);
1474 else
1475 unsetenv (varp);
1476 }
1477 cp = varp = (char *) subpointer;
1478 valp = 0;
1479 break;
1480
1481 case ENV_ESC:
1482 if (SB_EOF ())
1483 break;
1484 c = SB_GET ();
1485 /* FALL THROUGH */
1486 default:
1487 *cp++ = c;
1488 break;
1489 }
1490 }
1491 *cp = '\0';
1492 if (accept_env_set && gl_set_search (accept_env_set, varp))
1493 {
1494 if (valp)
1495 setenv (varp, valp, 1);
1496 else
1497 unsetenv (varp);
1498 }
1499 break;
1500 } /* end of case TELOPT_NEW_ENVIRON */
1501 #if defined AUTHENTICATION
1502 case TELOPT_AUTHENTICATION:
1503 if (SB_EOF ())
1504 break;
1505 switch (SB_GET ())
1506 {
1507 case TELQUAL_SEND:
1508 case TELQUAL_REPLY:
1509 /*
1510 * These are sent by us and cannot be sent by
1511 * the client.
1512 */
1513 break;
1514 case TELQUAL_IS:
1515 auth_is (subpointer, SB_LEN ());
1516 break;
1517 case TELQUAL_NAME:
1518 auth_name (subpointer, SB_LEN ());
1519 break;
1520 }
1521 break;
1522 #endif
1523 #ifdef ENCRYPTION
1524 case TELOPT_ENCRYPT:
1525 if (SB_EOF ())
1526 break;
1527 switch (SB_GET ())
1528 {
1529 case ENCRYPT_SUPPORT:
1530 encrypt_support (subpointer, SB_LEN ());
1531 break;
1532 case ENCRYPT_IS:
1533 encrypt_is (subpointer, SB_LEN ());
1534 break;
1535 case ENCRYPT_REPLY:
1536 encrypt_reply (subpointer, SB_LEN ());
1537 break;
1538 case ENCRYPT_START:
1539 encrypt_start (subpointer, SB_LEN ());
1540 break;
1541 case ENCRYPT_END:
1542 encrypt_end ();
1543 break;
1544 case ENCRYPT_REQSTART:
1545 encrypt_request_start (subpointer, SB_LEN ());
1546 break;
1547 case ENCRYPT_REQEND:
1548 /*
1549 * We can always send an REQEND so that we cannot
1550 * get stuck encrypting. We should only get this
1551 * if we have been able to get in the correct mode
1552 * anyhow.
1553 */
1554 encrypt_request_end ();
1555 break;
1556 case ENCRYPT_ENC_KEYID:
1557 encrypt_enc_keyid (subpointer, SB_LEN ());
1558 break;
1559 case ENCRYPT_DEC_KEYID:
1560 encrypt_dec_keyid (subpointer, SB_LEN ());
1561 break;
1562 default:
1563 break;
1564 }
1565 break;
1566 #endif /* ENCRYPTION */
1567
1568 default:
1569 break;
1570 } /* end of switch */
1571
1572 } /* end of suboption */
1573
1574 void
1575 doclientstat (void)
1576 {
1577 clientstat (TELOPT_LINEMODE, WILL, 0);
1578 }
1579
1580 void
1581 send_status (void)
1582 {
1583 #define ADD(c) \
1584 do { \
1585 if (ep > ncp) \
1586 *ncp++ = c; \
1587 else \
1588 goto trunc; \
1589 } while (0)
1590 #define ADD_DATA(c) \
1591 do { \
1592 ADD(c); if (c == SE || c == IAC) ADD(c); \
1593 } while (0)
1594
1595 unsigned char statusbuf[256];
1596 unsigned char *ep;
1597 unsigned char *ncp;
1598 unsigned char i;
1599
1600 ncp = statusbuf;
1601 ep = statusbuf + sizeof (statusbuf);
1602
1603 netflush (); /* get rid of anything waiting to go out */
1604
1605 ADD (IAC);
1606 ADD (SB);
1607 ADD (TELOPT_STATUS);
1608 ADD (TELQUAL_IS);
1609
1610 /*
1611 * We check the want_state rather than the current state,
1612 * because if we received a DO/WILL for an option that we
1613 * don't support, and the other side didn't send a DONT/WONT
1614 * in response to our WONT/DONT, then the "state" will be
1615 * WILL/DO, and the "want_state" will be WONT/DONT. We
1616 * need to go by the latter.
1617 */
1618 for (i = 0; i < (unsigned char) NTELOPTS; i++)
1619 {
1620 if (my_want_state_is_will (i))
1621 {
1622 ADD (WILL);
1623 ADD_DATA (i);
1624 }
1625 if (his_want_state_is_will (i))
1626 {
1627 ADD (DO);
1628 ADD_DATA (i);
1629 }
1630 }
1631
1632 if (his_want_state_is_will (TELOPT_LFLOW))
1633 {
1634 ADD (SB);
1635 ADD (TELOPT_LFLOW);
1636 if (flowmode)
1637 ADD (LFLOW_ON);
1638 else
1639 ADD (LFLOW_OFF);
1640
1641 ADD (SE);
1642
1643 if (restartany >= 0)
1644 {
1645 ADD (SB);
1646 ADD (TELOPT_LFLOW);
1647 if (restartany)
1648 ADD (LFLOW_RESTART_ANY);
1649 else
1650 ADD (LFLOW_RESTART_XON);
1651 ADD (SE);
1652 }
1653 }
1654
1655 if (his_want_state_is_will (TELOPT_LINEMODE))
1656 {
1657 unsigned char *cp, *cpe;
1658 int len;
1659
1660 ADD (SB);
1661 ADD (TELOPT_LINEMODE);
1662 ADD (LM_MODE);
1663 ADD_DATA (editmode);
1664 ADD (SE);
1665
1666 ADD (SB);
1667 ADD (TELOPT_LINEMODE);
1668 ADD (LM_SLC);
1669 start_slc (0);
1670 send_slc ();
1671 len = end_slc (&cp);
1672 for (cpe = cp + len; cp < cpe; cp++)
1673 ADD_DATA (*cp);
1674 ADD (SE);
1675 }
1676
1677 ADD (IAC);
1678 ADD (SE);
1679
1680 net_output_datalen (statusbuf, ncp - statusbuf);
1681 netflush (); /* Send it on its way */
1682 return;
1683
1684 trunc:
1685 /* XXX bark? */
1686 return;
1687 #undef ADD
1688 #undef ADD_DATA
1689 }