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