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