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