]>
Commit | Line | Data |
---|---|---|
b7080c8e A |
1 | /* |
2 | * Copyright (c) 1988, 1990, 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 | ||
7ba0088d A |
34 | #include <sys/cdefs.h> |
35 | ||
36 | #ifdef __FBSDID | |
37 | __FBSDID("$FreeBSD: src/crypto/telnet/telnet/telnet.c,v 1.4.2.5 2002/04/13 10:59:08 markm Exp $"); | |
38 | #endif | |
39 | ||
40 | #ifndef __unused | |
41 | #define __unused __attribute__((__unused__)) | |
42 | #endif | |
43 | ||
b7080c8e | 44 | #ifndef lint |
7ba0088d A |
45 | static const char sccsid[] = "@(#)telnet.c 8.4 (Berkeley) 5/30/95"; |
46 | #endif | |
b7080c8e A |
47 | |
48 | #include <sys/types.h> | |
49 | ||
b7080c8e A |
50 | /* By the way, we need to include curses.h before telnet.h since, |
51 | * among other things, telnet.h #defines 'DO', which is a variable | |
52 | * declared in curses.h. | |
53 | */ | |
b7080c8e A |
54 | |
55 | #include <ctype.h> | |
7ba0088d A |
56 | #include <curses.h> |
57 | #include <signal.h> | |
58 | #include <stdlib.h> | |
7ba0088d A |
59 | #include <unistd.h> |
60 | #include <arpa/telnet.h> | |
b7080c8e A |
61 | |
62 | #include "ring.h" | |
63 | ||
64 | #include "defines.h" | |
65 | #include "externs.h" | |
66 | #include "types.h" | |
67 | #include "general.h" | |
68 | ||
7ba0088d A |
69 | #ifdef AUTHENTICATION |
70 | #include <libtelnet/auth.h> | |
71 | #endif | |
72 | #ifdef ENCRYPTION | |
73 | #include <libtelnet/encrypt.h> | |
74 | #endif | |
75 | #include <libtelnet/misc.h> | |
b7080c8e A |
76 | \f |
77 | #define strip(x) ((my_want_state_is_wont(TELOPT_BINARY)) ? ((x)&0x7f) : (x)) | |
78 | ||
79 | static unsigned char subbuffer[SUBBUFSIZE], | |
80 | *subpointer, *subend; /* buffer for sub-options */ | |
81 | #define SB_CLEAR() subpointer = subbuffer; | |
82 | #define SB_TERM() { subend = subpointer; SB_CLEAR(); } | |
83 | #define SB_ACCUM(c) if (subpointer < (subbuffer+sizeof subbuffer)) { \ | |
84 | *subpointer++ = (c); \ | |
85 | } | |
86 | ||
87 | #define SB_GET() ((*subpointer++)&0xff) | |
88 | #define SB_PEEK() ((*subpointer)&0xff) | |
89 | #define SB_EOF() (subpointer >= subend) | |
90 | #define SB_LEN() (subend - subpointer) | |
91 | ||
92 | char options[256]; /* The combined options */ | |
93 | char do_dont_resp[256]; | |
94 | char will_wont_resp[256]; | |
95 | ||
96 | int | |
97 | eight = 0, | |
98 | autologin = 0, /* Autologin anyone? */ | |
99 | skiprc = 0, | |
100 | connected, | |
101 | showoptions, | |
b7080c8e A |
102 | ISend, /* trying to send network data in */ |
103 | debug = 0, | |
104 | crmod, | |
105 | netdata, /* Print out network data flow */ | |
106 | crlf, /* Should '\r' be mapped to <CR><LF> (or <CR><NUL>)? */ | |
b7080c8e A |
107 | telnetport, |
108 | SYNCHing, /* we are in TELNET SYNCH mode */ | |
109 | flushout, /* flush output */ | |
110 | autoflush = 0, /* flush output when interrupting? */ | |
111 | autosynch, /* send interrupt characters with SYNCH? */ | |
112 | localflow, /* we handle flow control locally */ | |
113 | restartany, /* if flow control enabled, restart on any character */ | |
114 | localchars, /* we recognize interrupt/quit */ | |
115 | donelclchars, /* the user has set "localchars" */ | |
116 | donebinarytoggle, /* the user has put us in binary */ | |
117 | dontlecho, /* do we suppress local echoing right now? */ | |
7ba0088d A |
118 | globalmode, |
119 | doaddrlookup = 1, /* do a reverse address lookup? */ | |
120 | clienteof = 0; | |
b7080c8e A |
121 | |
122 | char *prompt = 0; | |
7ba0088d A |
123 | #ifdef ENCRYPTION |
124 | char *line; /* hack around breakage in sra.c :-( !! */ | |
125 | #endif | |
b7080c8e A |
126 | |
127 | cc_t escape; | |
128 | cc_t rlogin; | |
129 | #ifdef KLUDGELINEMODE | |
130 | cc_t echoc; | |
131 | #endif | |
132 | ||
133 | /* | |
134 | * Telnet receiver states for fsm | |
135 | */ | |
136 | #define TS_DATA 0 | |
137 | #define TS_IAC 1 | |
138 | #define TS_WILL 2 | |
139 | #define TS_WONT 3 | |
140 | #define TS_DO 4 | |
141 | #define TS_DONT 5 | |
142 | #define TS_CR 6 | |
143 | #define TS_SB 7 /* sub-option collection */ | |
144 | #define TS_SE 8 /* looking for sub-option end */ | |
145 | ||
146 | static int telrcv_state; | |
147 | #ifdef OLD_ENVIRON | |
148 | unsigned char telopt_environ = TELOPT_NEW_ENVIRON; | |
149 | #else | |
150 | # define telopt_environ TELOPT_NEW_ENVIRON | |
151 | #endif | |
152 | ||
7ba0088d | 153 | jmp_buf toplevel; |
b7080c8e A |
154 | jmp_buf peerdied; |
155 | ||
156 | int flushline; | |
157 | int linemode; | |
158 | ||
159 | #ifdef KLUDGELINEMODE | |
160 | int kludgelinemode = 1; | |
161 | #endif | |
162 | ||
7ba0088d A |
163 | static int is_unique(char *, char **, char **); |
164 | ||
b7080c8e A |
165 | /* |
166 | * The following are some clocks used to decide how to interpret | |
167 | * the relationship between various variables. | |
168 | */ | |
169 | ||
170 | Clocks clocks; | |
b7080c8e A |
171 | \f |
172 | /* | |
173 | * Initialize telnet environment. | |
174 | */ | |
175 | ||
7ba0088d A |
176 | void |
177 | init_telnet(void) | |
b7080c8e A |
178 | { |
179 | env_init(); | |
180 | ||
181 | SB_CLEAR(); | |
182 | ClearArray(options); | |
183 | ||
7ba0088d A |
184 | connected = ISend = localflow = donebinarytoggle = 0; |
185 | #ifdef AUTHENTICATION | |
186 | #ifdef ENCRYPTION | |
b7080c8e | 187 | auth_encrypt_connect(connected); |
7ba0088d A |
188 | #endif |
189 | #endif | |
b7080c8e A |
190 | restartany = -1; |
191 | ||
192 | SYNCHing = 0; | |
193 | ||
194 | /* Don't change NetTrace */ | |
195 | ||
196 | escape = CONTROL(']'); | |
197 | rlogin = _POSIX_VDISABLE; | |
198 | #ifdef KLUDGELINEMODE | |
199 | echoc = CONTROL('E'); | |
200 | #endif | |
201 | ||
202 | flushline = 1; | |
203 | telrcv_state = TS_DATA; | |
204 | } | |
205 | \f | |
206 | ||
b7080c8e A |
207 | /* |
208 | * These routines are in charge of sending option negotiations | |
209 | * to the other side. | |
210 | * | |
211 | * The basic idea is that we send the negotiation if either side | |
212 | * is in disagreement as to what the current state should be. | |
213 | */ | |
214 | ||
7ba0088d A |
215 | void |
216 | send_do(int c, int init) | |
b7080c8e A |
217 | { |
218 | if (init) { | |
219 | if (((do_dont_resp[c] == 0) && my_state_is_do(c)) || | |
220 | my_want_state_is_do(c)) | |
221 | return; | |
222 | set_my_want_state_do(c); | |
223 | do_dont_resp[c]++; | |
224 | } | |
225 | NET2ADD(IAC, DO); | |
226 | NETADD(c); | |
227 | printoption("SENT", DO, c); | |
228 | } | |
229 | ||
7ba0088d A |
230 | void |
231 | send_dont(int c, int init) | |
b7080c8e A |
232 | { |
233 | if (init) { | |
234 | if (((do_dont_resp[c] == 0) && my_state_is_dont(c)) || | |
235 | my_want_state_is_dont(c)) | |
236 | return; | |
237 | set_my_want_state_dont(c); | |
238 | do_dont_resp[c]++; | |
239 | } | |
240 | NET2ADD(IAC, DONT); | |
241 | NETADD(c); | |
242 | printoption("SENT", DONT, c); | |
243 | } | |
244 | ||
7ba0088d A |
245 | void |
246 | send_will(int c, int init) | |
b7080c8e A |
247 | { |
248 | if (init) { | |
249 | if (((will_wont_resp[c] == 0) && my_state_is_will(c)) || | |
250 | my_want_state_is_will(c)) | |
251 | return; | |
252 | set_my_want_state_will(c); | |
253 | will_wont_resp[c]++; | |
254 | } | |
255 | NET2ADD(IAC, WILL); | |
256 | NETADD(c); | |
257 | printoption("SENT", WILL, c); | |
258 | } | |
259 | ||
7ba0088d A |
260 | void |
261 | send_wont(int c, int init) | |
b7080c8e A |
262 | { |
263 | if (init) { | |
264 | if (((will_wont_resp[c] == 0) && my_state_is_wont(c)) || | |
265 | my_want_state_is_wont(c)) | |
266 | return; | |
267 | set_my_want_state_wont(c); | |
268 | will_wont_resp[c]++; | |
269 | } | |
270 | NET2ADD(IAC, WONT); | |
271 | NETADD(c); | |
272 | printoption("SENT", WONT, c); | |
273 | } | |
274 | ||
7ba0088d A |
275 | void |
276 | willoption(int option) | |
b7080c8e A |
277 | { |
278 | int new_state_ok = 0; | |
279 | ||
280 | if (do_dont_resp[option]) { | |
281 | --do_dont_resp[option]; | |
282 | if (do_dont_resp[option] && my_state_is_do(option)) | |
283 | --do_dont_resp[option]; | |
284 | } | |
285 | ||
286 | if ((do_dont_resp[option] == 0) && my_want_state_is_dont(option)) { | |
287 | ||
288 | switch (option) { | |
289 | ||
290 | case TELOPT_ECHO: | |
b7080c8e A |
291 | case TELOPT_BINARY: |
292 | case TELOPT_SGA: | |
293 | settimer(modenegotiated); | |
294 | /* FALL THROUGH */ | |
295 | case TELOPT_STATUS: | |
7ba0088d | 296 | #ifdef AUTHENTICATION |
b7080c8e A |
297 | case TELOPT_AUTHENTICATION: |
298 | #endif | |
299 | #ifdef ENCRYPTION | |
300 | case TELOPT_ENCRYPT: | |
301 | #endif /* ENCRYPTION */ | |
302 | new_state_ok = 1; | |
303 | break; | |
304 | ||
305 | case TELOPT_TM: | |
306 | if (flushout) | |
307 | flushout = 0; | |
308 | /* | |
309 | * Special case for TM. If we get back a WILL, | |
310 | * pretend we got back a WONT. | |
311 | */ | |
312 | set_my_want_state_dont(option); | |
313 | set_my_state_dont(option); | |
314 | return; /* Never reply to TM will's/wont's */ | |
315 | ||
316 | case TELOPT_LINEMODE: | |
317 | default: | |
318 | break; | |
319 | } | |
320 | ||
321 | if (new_state_ok) { | |
322 | set_my_want_state_do(option); | |
323 | send_do(option, 0); | |
324 | setconnmode(0); /* possibly set new tty mode */ | |
325 | } else { | |
326 | do_dont_resp[option]++; | |
327 | send_dont(option, 0); | |
328 | } | |
329 | } | |
330 | set_my_state_do(option); | |
331 | #ifdef ENCRYPTION | |
332 | if (option == TELOPT_ENCRYPT) | |
333 | encrypt_send_support(); | |
334 | #endif /* ENCRYPTION */ | |
335 | } | |
336 | ||
7ba0088d A |
337 | void |
338 | wontoption(int option) | |
b7080c8e A |
339 | { |
340 | if (do_dont_resp[option]) { | |
341 | --do_dont_resp[option]; | |
342 | if (do_dont_resp[option] && my_state_is_dont(option)) | |
343 | --do_dont_resp[option]; | |
344 | } | |
345 | ||
346 | if ((do_dont_resp[option] == 0) && my_want_state_is_do(option)) { | |
347 | ||
348 | switch (option) { | |
349 | ||
350 | #ifdef KLUDGELINEMODE | |
351 | case TELOPT_SGA: | |
352 | if (!kludgelinemode) | |
353 | break; | |
354 | /* FALL THROUGH */ | |
355 | #endif | |
356 | case TELOPT_ECHO: | |
357 | settimer(modenegotiated); | |
358 | break; | |
359 | ||
360 | case TELOPT_TM: | |
361 | if (flushout) | |
362 | flushout = 0; | |
363 | set_my_want_state_dont(option); | |
364 | set_my_state_dont(option); | |
365 | return; /* Never reply to TM will's/wont's */ | |
366 | ||
367 | default: | |
368 | break; | |
369 | } | |
370 | set_my_want_state_dont(option); | |
371 | if (my_state_is_do(option)) | |
372 | send_dont(option, 0); | |
373 | setconnmode(0); /* Set new tty mode */ | |
374 | } else if (option == TELOPT_TM) { | |
375 | /* | |
376 | * Special case for TM. | |
377 | */ | |
378 | if (flushout) | |
379 | flushout = 0; | |
380 | set_my_want_state_dont(option); | |
381 | } | |
382 | set_my_state_dont(option); | |
383 | } | |
384 | ||
7ba0088d A |
385 | static void |
386 | dooption(int option) | |
b7080c8e A |
387 | { |
388 | int new_state_ok = 0; | |
389 | ||
390 | if (will_wont_resp[option]) { | |
391 | --will_wont_resp[option]; | |
392 | if (will_wont_resp[option] && my_state_is_will(option)) | |
393 | --will_wont_resp[option]; | |
394 | } | |
395 | ||
396 | if (will_wont_resp[option] == 0) { | |
397 | if (my_want_state_is_wont(option)) { | |
398 | ||
399 | switch (option) { | |
400 | ||
401 | case TELOPT_TM: | |
402 | /* | |
403 | * Special case for TM. We send a WILL, but pretend | |
404 | * we sent WONT. | |
405 | */ | |
406 | send_will(option, 0); | |
407 | set_my_want_state_wont(TELOPT_TM); | |
408 | set_my_state_wont(TELOPT_TM); | |
409 | return; | |
410 | ||
b7080c8e A |
411 | case TELOPT_BINARY: /* binary mode */ |
412 | case TELOPT_NAWS: /* window size */ | |
413 | case TELOPT_TSPEED: /* terminal speed */ | |
414 | case TELOPT_LFLOW: /* local flow control */ | |
415 | case TELOPT_TTYPE: /* terminal type option */ | |
416 | case TELOPT_SGA: /* no big deal */ | |
417 | #ifdef ENCRYPTION | |
418 | case TELOPT_ENCRYPT: /* encryption variable option */ | |
419 | #endif /* ENCRYPTION */ | |
420 | new_state_ok = 1; | |
421 | break; | |
422 | ||
423 | case TELOPT_NEW_ENVIRON: /* New environment variable option */ | |
424 | #ifdef OLD_ENVIRON | |
425 | if (my_state_is_will(TELOPT_OLD_ENVIRON)) | |
426 | send_wont(TELOPT_OLD_ENVIRON, 1); /* turn off the old */ | |
427 | goto env_common; | |
428 | case TELOPT_OLD_ENVIRON: /* Old environment variable option */ | |
429 | if (my_state_is_will(TELOPT_NEW_ENVIRON)) | |
430 | break; /* Don't enable if new one is in use! */ | |
431 | env_common: | |
432 | telopt_environ = option; | |
433 | #endif | |
434 | new_state_ok = 1; | |
435 | break; | |
436 | ||
7ba0088d | 437 | #ifdef AUTHENTICATION |
b7080c8e A |
438 | case TELOPT_AUTHENTICATION: |
439 | if (autologin) | |
440 | new_state_ok = 1; | |
441 | break; | |
442 | #endif | |
443 | ||
444 | case TELOPT_XDISPLOC: /* X Display location */ | |
7ba0088d | 445 | if (env_getvalue("DISPLAY")) |
b7080c8e A |
446 | new_state_ok = 1; |
447 | break; | |
448 | ||
449 | case TELOPT_LINEMODE: | |
450 | #ifdef KLUDGELINEMODE | |
451 | kludgelinemode = 0; | |
452 | send_do(TELOPT_SGA, 1); | |
453 | #endif | |
454 | set_my_want_state_will(TELOPT_LINEMODE); | |
455 | send_will(option, 0); | |
456 | set_my_state_will(TELOPT_LINEMODE); | |
457 | slc_init(); | |
458 | return; | |
459 | ||
460 | case TELOPT_ECHO: /* We're never going to echo... */ | |
461 | default: | |
462 | break; | |
463 | } | |
464 | ||
465 | if (new_state_ok) { | |
466 | set_my_want_state_will(option); | |
467 | send_will(option, 0); | |
468 | setconnmode(0); /* Set new tty mode */ | |
469 | } else { | |
470 | will_wont_resp[option]++; | |
471 | send_wont(option, 0); | |
472 | } | |
473 | } else { | |
474 | /* | |
475 | * Handle options that need more things done after the | |
476 | * other side has acknowledged the option. | |
477 | */ | |
478 | switch (option) { | |
479 | case TELOPT_LINEMODE: | |
480 | #ifdef KLUDGELINEMODE | |
481 | kludgelinemode = 0; | |
482 | send_do(TELOPT_SGA, 1); | |
483 | #endif | |
484 | set_my_state_will(option); | |
485 | slc_init(); | |
486 | send_do(TELOPT_SGA, 0); | |
487 | return; | |
488 | } | |
489 | } | |
490 | } | |
491 | set_my_state_will(option); | |
492 | } | |
493 | ||
7ba0088d A |
494 | static void |
495 | dontoption(int option) | |
b7080c8e A |
496 | { |
497 | ||
498 | if (will_wont_resp[option]) { | |
499 | --will_wont_resp[option]; | |
500 | if (will_wont_resp[option] && my_state_is_wont(option)) | |
501 | --will_wont_resp[option]; | |
502 | } | |
503 | ||
504 | if ((will_wont_resp[option] == 0) && my_want_state_is_will(option)) { | |
505 | switch (option) { | |
506 | case TELOPT_LINEMODE: | |
507 | linemode = 0; /* put us back to the default state */ | |
508 | break; | |
509 | #ifdef OLD_ENVIRON | |
510 | case TELOPT_NEW_ENVIRON: | |
511 | /* | |
512 | * The new environ option wasn't recognized, try | |
513 | * the old one. | |
514 | */ | |
515 | send_will(TELOPT_OLD_ENVIRON, 1); | |
516 | telopt_environ = TELOPT_OLD_ENVIRON; | |
517 | break; | |
518 | #endif | |
519 | } | |
520 | /* we always accept a DONT */ | |
521 | set_my_want_state_wont(option); | |
522 | if (my_state_is_will(option)) | |
523 | send_wont(option, 0); | |
524 | setconnmode(0); /* Set new tty mode */ | |
525 | } | |
526 | set_my_state_wont(option); | |
527 | } | |
528 | ||
529 | /* | |
530 | * Given a buffer returned by tgetent(), this routine will turn | |
7ba0088d | 531 | * the pipe separated list of names in the buffer into an array |
b7080c8e A |
532 | * of pointers to null terminated names. We toss out any bad, |
533 | * duplicate, or verbose names (names with spaces). | |
534 | */ | |
535 | ||
7ba0088d A |
536 | static const char *name_unknown = "UNKNOWN"; |
537 | static const char *unknown[] = { NULL, NULL }; | |
b7080c8e | 538 | |
7ba0088d A |
539 | static const char ** |
540 | mklist(char *buf, char *name) | |
b7080c8e | 541 | { |
7ba0088d A |
542 | int n; |
543 | char c, *cp, **argvp, *cp2, **argv, **avt; | |
b7080c8e A |
544 | |
545 | if (name) { | |
7ba0088d | 546 | if (strlen(name) > 40) { |
b7080c8e A |
547 | name = 0; |
548 | unknown[0] = name_unknown; | |
549 | } else { | |
550 | unknown[0] = name; | |
551 | upcase(name); | |
552 | } | |
553 | } else | |
554 | unknown[0] = name_unknown; | |
555 | /* | |
556 | * Count up the number of names. | |
557 | */ | |
558 | for (n = 1, cp = buf; *cp && *cp != ':'; cp++) { | |
559 | if (*cp == '|') | |
560 | n++; | |
561 | } | |
562 | /* | |
563 | * Allocate an array to put the name pointers into | |
564 | */ | |
565 | argv = (char **)malloc((n+3)*sizeof(char *)); | |
566 | if (argv == 0) | |
567 | return(unknown); | |
568 | ||
569 | /* | |
570 | * Fill up the array of pointers to names. | |
571 | */ | |
572 | *argv = 0; | |
573 | argvp = argv+1; | |
574 | n = 0; | |
575 | for (cp = cp2 = buf; (c = *cp); cp++) { | |
576 | if (c == '|' || c == ':') { | |
577 | *cp++ = '\0'; | |
578 | /* | |
579 | * Skip entries that have spaces or are over 40 | |
580 | * characters long. If this is our environment | |
581 | * name, then put it up front. Otherwise, as | |
582 | * long as this is not a duplicate name (case | |
583 | * insensitive) add it to the list. | |
584 | */ | |
585 | if (n || (cp - cp2 > 41)) | |
586 | ; | |
587 | else if (name && (strncasecmp(name, cp2, cp-cp2) == 0)) | |
588 | *argv = cp2; | |
589 | else if (is_unique(cp2, argv+1, argvp)) | |
590 | *argvp++ = cp2; | |
591 | if (c == ':') | |
592 | break; | |
593 | /* | |
594 | * Skip multiple delimiters. Reset cp2 to | |
595 | * the beginning of the next name. Reset n, | |
596 | * the flag for names with spaces. | |
597 | */ | |
598 | while ((c = *cp) == '|') | |
599 | cp++; | |
600 | cp2 = cp; | |
601 | n = 0; | |
602 | } | |
603 | /* | |
604 | * Skip entries with spaces or non-ascii values. | |
605 | * Convert lower case letters to upper case. | |
606 | */ | |
607 | if ((c == ' ') || !isascii(c)) | |
608 | n = 1; | |
609 | else if (islower(c)) | |
610 | *cp = toupper(c); | |
611 | } | |
612 | ||
613 | /* | |
614 | * Check for an old V6 2 character name. If the second | |
615 | * name points to the beginning of the buffer, and is | |
616 | * only 2 characters long, move it to the end of the array. | |
617 | */ | |
618 | if ((argv[1] == buf) && (strlen(argv[1]) == 2)) { | |
619 | --argvp; | |
620 | for (avt = &argv[1]; avt < argvp; avt++) | |
621 | *avt = *(avt+1); | |
622 | *argvp++ = buf; | |
623 | } | |
624 | ||
625 | /* | |
626 | * Duplicate last name, for TTYPE option, and null | |
627 | * terminate the array. If we didn't find a match on | |
628 | * our terminal name, put that name at the beginning. | |
629 | */ | |
630 | cp = *(argvp-1); | |
631 | *argvp++ = cp; | |
632 | *argvp = 0; | |
633 | ||
634 | if (*argv == 0) { | |
635 | if (name) | |
636 | *argv = name; | |
637 | else { | |
638 | --argvp; | |
639 | for (avt = argv; avt < argvp; avt++) | |
640 | *avt = *(avt+1); | |
641 | } | |
642 | } | |
643 | if (*argv) | |
7ba0088d | 644 | return((const char **)argv); |
b7080c8e A |
645 | else |
646 | return(unknown); | |
647 | } | |
648 | ||
7ba0088d A |
649 | static int |
650 | is_unique(char *name, char **as, char **ae) | |
b7080c8e | 651 | { |
7ba0088d A |
652 | char **ap; |
653 | int n; | |
b7080c8e A |
654 | |
655 | n = strlen(name) + 1; | |
656 | for (ap = as; ap < ae; ap++) | |
657 | if (strncasecmp(*ap, name, n) == 0) | |
658 | return(0); | |
659 | return (1); | |
660 | } | |
661 | ||
662 | #ifdef TERMCAP | |
663 | char termbuf[1024]; | |
664 | ||
7ba0088d A |
665 | /*ARGSUSED*/ |
666 | static int | |
667 | setupterm(char *tname, int fd, int *errp) | |
b7080c8e A |
668 | { |
669 | if (tgetent(termbuf, tname) == 1) { | |
670 | termbuf[1023] = '\0'; | |
671 | if (errp) | |
672 | *errp = 1; | |
673 | return(0); | |
674 | } | |
675 | if (errp) | |
676 | *errp = 0; | |
677 | return(-1); | |
678 | } | |
679 | #else | |
680 | #define termbuf ttytype | |
681 | extern char ttytype[]; | |
682 | #endif | |
683 | ||
684 | int resettermname = 1; | |
685 | ||
7ba0088d A |
686 | static const char * |
687 | gettermname(void) | |
b7080c8e A |
688 | { |
689 | char *tname; | |
7ba0088d A |
690 | static const char **tnamep = 0; |
691 | static const char **next; | |
b7080c8e A |
692 | int err; |
693 | ||
694 | if (resettermname) { | |
695 | resettermname = 0; | |
696 | if (tnamep && tnamep != unknown) | |
697 | free(tnamep); | |
7ba0088d | 698 | if ((tname = env_getvalue("TERM")) && |
b7080c8e A |
699 | (setupterm(tname, 1, &err) == 0)) { |
700 | tnamep = mklist(termbuf, tname); | |
701 | } else { | |
7ba0088d | 702 | if (tname && (strlen(tname) <= 40)) { |
b7080c8e A |
703 | unknown[0] = tname; |
704 | upcase(tname); | |
705 | } else | |
706 | unknown[0] = name_unknown; | |
707 | tnamep = unknown; | |
708 | } | |
709 | next = tnamep; | |
710 | } | |
711 | if (*next == 0) | |
712 | next = tnamep; | |
713 | return(*next++); | |
714 | } | |
715 | /* | |
716 | * suboption() | |
717 | * | |
718 | * Look at the sub-option buffer, and try to be helpful to the other | |
719 | * side. | |
720 | * | |
721 | * Currently we recognize: | |
722 | * | |
723 | * Terminal type, send request. | |
724 | * Terminal speed (send request). | |
725 | * Local flow control (is request). | |
726 | * Linemode | |
727 | */ | |
728 | ||
7ba0088d A |
729 | static void |
730 | suboption(void) | |
b7080c8e A |
731 | { |
732 | unsigned char subchar; | |
733 | ||
734 | printsub('<', subbuffer, SB_LEN()+2); | |
735 | switch (subchar = SB_GET()) { | |
736 | case TELOPT_TTYPE: | |
737 | if (my_want_state_is_wont(TELOPT_TTYPE)) | |
738 | return; | |
739 | if (SB_EOF() || SB_GET() != TELQUAL_SEND) { | |
740 | return; | |
741 | } else { | |
7ba0088d | 742 | const char *name; |
b7080c8e A |
743 | unsigned char temp[50]; |
744 | int len; | |
745 | ||
b7080c8e A |
746 | name = gettermname(); |
747 | len = strlen(name) + 4 + 2; | |
748 | if (len < NETROOM()) { | |
7ba0088d | 749 | sprintf(temp, "%c%c%c%c%s%c%c", IAC, SB, TELOPT_TTYPE, |
b7080c8e A |
750 | TELQUAL_IS, name, IAC, SE); |
751 | ring_supply_data(&netoring, temp, len); | |
752 | printsub('>', &temp[2], len-2); | |
753 | } else { | |
754 | ExitString("No room in buffer for terminal type.\n", 1); | |
755 | /*NOTREACHED*/ | |
756 | } | |
757 | } | |
758 | break; | |
759 | case TELOPT_TSPEED: | |
760 | if (my_want_state_is_wont(TELOPT_TSPEED)) | |
761 | return; | |
762 | if (SB_EOF()) | |
763 | return; | |
764 | if (SB_GET() == TELQUAL_SEND) { | |
765 | long ospeed, ispeed; | |
766 | unsigned char temp[50]; | |
767 | int len; | |
768 | ||
769 | TerminalSpeeds(&ispeed, &ospeed); | |
770 | ||
7ba0088d | 771 | sprintf((char *)temp, "%c%c%c%c%ld,%ld%c%c", IAC, SB, TELOPT_TSPEED, |
b7080c8e A |
772 | TELQUAL_IS, ospeed, ispeed, IAC, SE); |
773 | len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */ | |
774 | ||
775 | if (len < NETROOM()) { | |
776 | ring_supply_data(&netoring, temp, len); | |
777 | printsub('>', temp+2, len - 2); | |
778 | } | |
779 | /*@*/ else printf("lm_will: not enough room in buffer\n"); | |
780 | } | |
781 | break; | |
782 | case TELOPT_LFLOW: | |
783 | if (my_want_state_is_wont(TELOPT_LFLOW)) | |
784 | return; | |
785 | if (SB_EOF()) | |
786 | return; | |
787 | switch(SB_GET()) { | |
788 | case LFLOW_RESTART_ANY: | |
789 | restartany = 1; | |
790 | break; | |
791 | case LFLOW_RESTART_XON: | |
792 | restartany = 0; | |
793 | break; | |
794 | case LFLOW_ON: | |
795 | localflow = 1; | |
796 | break; | |
797 | case LFLOW_OFF: | |
798 | localflow = 0; | |
799 | break; | |
800 | default: | |
801 | return; | |
802 | } | |
803 | setcommandmode(); | |
804 | setconnmode(0); | |
805 | break; | |
806 | ||
807 | case TELOPT_LINEMODE: | |
808 | if (my_want_state_is_wont(TELOPT_LINEMODE)) | |
809 | return; | |
810 | if (SB_EOF()) | |
811 | return; | |
812 | switch (SB_GET()) { | |
813 | case WILL: | |
814 | lm_will(subpointer, SB_LEN()); | |
815 | break; | |
816 | case WONT: | |
817 | lm_wont(subpointer, SB_LEN()); | |
818 | break; | |
819 | case DO: | |
820 | lm_do(subpointer, SB_LEN()); | |
821 | break; | |
822 | case DONT: | |
823 | lm_dont(subpointer, SB_LEN()); | |
824 | break; | |
825 | case LM_SLC: | |
826 | slc(subpointer, SB_LEN()); | |
827 | break; | |
828 | case LM_MODE: | |
829 | lm_mode(subpointer, SB_LEN(), 0); | |
830 | break; | |
831 | default: | |
832 | break; | |
833 | } | |
834 | break; | |
835 | ||
836 | #ifdef OLD_ENVIRON | |
837 | case TELOPT_OLD_ENVIRON: | |
838 | #endif | |
839 | case TELOPT_NEW_ENVIRON: | |
840 | if (SB_EOF()) | |
841 | return; | |
842 | switch(SB_PEEK()) { | |
843 | case TELQUAL_IS: | |
844 | case TELQUAL_INFO: | |
845 | if (my_want_state_is_dont(subchar)) | |
846 | return; | |
847 | break; | |
848 | case TELQUAL_SEND: | |
849 | if (my_want_state_is_wont(subchar)) { | |
850 | return; | |
851 | } | |
852 | break; | |
853 | default: | |
854 | return; | |
855 | } | |
856 | env_opt(subpointer, SB_LEN()); | |
857 | break; | |
858 | ||
859 | case TELOPT_XDISPLOC: | |
860 | if (my_want_state_is_wont(TELOPT_XDISPLOC)) | |
861 | return; | |
862 | if (SB_EOF()) | |
863 | return; | |
864 | if (SB_GET() == TELQUAL_SEND) { | |
865 | unsigned char temp[50], *dp; | |
866 | int len; | |
867 | ||
7ba0088d A |
868 | if ((dp = env_getvalue("DISPLAY")) == NULL || |
869 | strlen(dp) > sizeof(temp) - 7) { | |
b7080c8e A |
870 | /* |
871 | * Something happened, we no longer have a DISPLAY | |
7ba0088d | 872 | * variable. Or it is too long. So, turn off the option. |
b7080c8e A |
873 | */ |
874 | send_wont(TELOPT_XDISPLOC, 1); | |
875 | break; | |
876 | } | |
7ba0088d A |
877 | snprintf(temp, sizeof(temp), "%c%c%c%c%s%c%c", IAC, SB, |
878 | TELOPT_XDISPLOC, TELQUAL_IS, dp, IAC, SE); | |
b7080c8e A |
879 | len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */ |
880 | ||
881 | if (len < NETROOM()) { | |
882 | ring_supply_data(&netoring, temp, len); | |
883 | printsub('>', temp+2, len - 2); | |
884 | } | |
885 | /*@*/ else printf("lm_will: not enough room in buffer\n"); | |
886 | } | |
887 | break; | |
888 | ||
7ba0088d | 889 | #ifdef AUTHENTICATION |
b7080c8e A |
890 | case TELOPT_AUTHENTICATION: { |
891 | if (!autologin) | |
892 | break; | |
893 | if (SB_EOF()) | |
894 | return; | |
895 | switch(SB_GET()) { | |
896 | case TELQUAL_IS: | |
897 | if (my_want_state_is_dont(TELOPT_AUTHENTICATION)) | |
898 | return; | |
899 | auth_is(subpointer, SB_LEN()); | |
900 | break; | |
901 | case TELQUAL_SEND: | |
902 | if (my_want_state_is_wont(TELOPT_AUTHENTICATION)) | |
903 | return; | |
904 | auth_send(subpointer, SB_LEN()); | |
905 | break; | |
906 | case TELQUAL_REPLY: | |
907 | if (my_want_state_is_wont(TELOPT_AUTHENTICATION)) | |
908 | return; | |
909 | auth_reply(subpointer, SB_LEN()); | |
910 | break; | |
911 | case TELQUAL_NAME: | |
912 | if (my_want_state_is_dont(TELOPT_AUTHENTICATION)) | |
913 | return; | |
914 | auth_name(subpointer, SB_LEN()); | |
915 | break; | |
916 | } | |
917 | } | |
918 | break; | |
919 | #endif | |
920 | #ifdef ENCRYPTION | |
921 | case TELOPT_ENCRYPT: | |
922 | if (SB_EOF()) | |
923 | return; | |
924 | switch(SB_GET()) { | |
925 | case ENCRYPT_START: | |
926 | if (my_want_state_is_dont(TELOPT_ENCRYPT)) | |
927 | return; | |
928 | encrypt_start(subpointer, SB_LEN()); | |
929 | break; | |
930 | case ENCRYPT_END: | |
931 | if (my_want_state_is_dont(TELOPT_ENCRYPT)) | |
932 | return; | |
933 | encrypt_end(); | |
934 | break; | |
935 | case ENCRYPT_SUPPORT: | |
936 | if (my_want_state_is_wont(TELOPT_ENCRYPT)) | |
937 | return; | |
938 | encrypt_support(subpointer, SB_LEN()); | |
939 | break; | |
940 | case ENCRYPT_REQSTART: | |
941 | if (my_want_state_is_wont(TELOPT_ENCRYPT)) | |
942 | return; | |
943 | encrypt_request_start(subpointer, SB_LEN()); | |
944 | break; | |
945 | case ENCRYPT_REQEND: | |
946 | if (my_want_state_is_wont(TELOPT_ENCRYPT)) | |
947 | return; | |
948 | /* | |
949 | * We can always send an REQEND so that we cannot | |
950 | * get stuck encrypting. We should only get this | |
951 | * if we have been able to get in the correct mode | |
952 | * anyhow. | |
953 | */ | |
954 | encrypt_request_end(); | |
955 | break; | |
956 | case ENCRYPT_IS: | |
957 | if (my_want_state_is_dont(TELOPT_ENCRYPT)) | |
958 | return; | |
959 | encrypt_is(subpointer, SB_LEN()); | |
960 | break; | |
961 | case ENCRYPT_REPLY: | |
962 | if (my_want_state_is_wont(TELOPT_ENCRYPT)) | |
963 | return; | |
964 | encrypt_reply(subpointer, SB_LEN()); | |
965 | break; | |
966 | case ENCRYPT_ENC_KEYID: | |
967 | if (my_want_state_is_dont(TELOPT_ENCRYPT)) | |
968 | return; | |
969 | encrypt_enc_keyid(subpointer, SB_LEN()); | |
970 | break; | |
971 | case ENCRYPT_DEC_KEYID: | |
972 | if (my_want_state_is_wont(TELOPT_ENCRYPT)) | |
973 | return; | |
974 | encrypt_dec_keyid(subpointer, SB_LEN()); | |
975 | break; | |
976 | default: | |
977 | break; | |
978 | } | |
979 | break; | |
980 | #endif /* ENCRYPTION */ | |
981 | default: | |
982 | break; | |
983 | } | |
984 | } | |
985 | ||
986 | static unsigned char str_lm[] = { IAC, SB, TELOPT_LINEMODE, 0, 0, IAC, SE }; | |
987 | ||
7ba0088d A |
988 | void |
989 | lm_will(unsigned char *cmd, int len) | |
b7080c8e A |
990 | { |
991 | if (len < 1) { | |
992 | /*@*/ printf("lm_will: no command!!!\n"); /* Should not happen... */ | |
993 | return; | |
994 | } | |
995 | switch(cmd[0]) { | |
996 | case LM_FORWARDMASK: /* We shouldn't ever get this... */ | |
997 | default: | |
998 | str_lm[3] = DONT; | |
999 | str_lm[4] = cmd[0]; | |
7ba0088d | 1000 | if (NETROOM() > (int)sizeof(str_lm)) { |
b7080c8e A |
1001 | ring_supply_data(&netoring, str_lm, sizeof(str_lm)); |
1002 | printsub('>', &str_lm[2], sizeof(str_lm)-2); | |
1003 | } | |
1004 | /*@*/ else printf("lm_will: not enough room in buffer\n"); | |
1005 | break; | |
1006 | } | |
1007 | } | |
1008 | ||
7ba0088d A |
1009 | void |
1010 | lm_wont(unsigned char *cmd, int len) | |
b7080c8e A |
1011 | { |
1012 | if (len < 1) { | |
1013 | /*@*/ printf("lm_wont: no command!!!\n"); /* Should not happen... */ | |
1014 | return; | |
1015 | } | |
1016 | switch(cmd[0]) { | |
1017 | case LM_FORWARDMASK: /* We shouldn't ever get this... */ | |
1018 | default: | |
1019 | /* We are always DONT, so don't respond */ | |
1020 | return; | |
1021 | } | |
1022 | } | |
1023 | ||
7ba0088d A |
1024 | void |
1025 | lm_do(unsigned char *cmd, int len) | |
b7080c8e A |
1026 | { |
1027 | if (len < 1) { | |
1028 | /*@*/ printf("lm_do: no command!!!\n"); /* Should not happen... */ | |
1029 | return; | |
1030 | } | |
1031 | switch(cmd[0]) { | |
1032 | case LM_FORWARDMASK: | |
1033 | default: | |
1034 | str_lm[3] = WONT; | |
1035 | str_lm[4] = cmd[0]; | |
7ba0088d | 1036 | if (NETROOM() > (int)sizeof(str_lm)) { |
b7080c8e A |
1037 | ring_supply_data(&netoring, str_lm, sizeof(str_lm)); |
1038 | printsub('>', &str_lm[2], sizeof(str_lm)-2); | |
1039 | } | |
1040 | /*@*/ else printf("lm_do: not enough room in buffer\n"); | |
1041 | break; | |
1042 | } | |
1043 | } | |
1044 | ||
7ba0088d A |
1045 | void |
1046 | lm_dont(unsigned char *cmd, int len) | |
b7080c8e A |
1047 | { |
1048 | if (len < 1) { | |
1049 | /*@*/ printf("lm_dont: no command!!!\n"); /* Should not happen... */ | |
1050 | return; | |
1051 | } | |
1052 | switch(cmd[0]) { | |
1053 | case LM_FORWARDMASK: | |
1054 | default: | |
1055 | /* we are always WONT, so don't respond */ | |
1056 | break; | |
1057 | } | |
1058 | } | |
1059 | ||
1060 | static unsigned char str_lm_mode[] = { | |
1061 | IAC, SB, TELOPT_LINEMODE, LM_MODE, 0, IAC, SE | |
1062 | }; | |
1063 | ||
7ba0088d A |
1064 | void |
1065 | lm_mode(unsigned char *cmd, int len, int init) | |
b7080c8e A |
1066 | { |
1067 | if (len != 1) | |
1068 | return; | |
1069 | if ((linemode&MODE_MASK&~MODE_ACK) == *cmd) | |
1070 | return; | |
1071 | if (*cmd&MODE_ACK) | |
1072 | return; | |
1073 | linemode = *cmd&(MODE_MASK&~MODE_ACK); | |
1074 | str_lm_mode[4] = linemode; | |
1075 | if (!init) | |
1076 | str_lm_mode[4] |= MODE_ACK; | |
7ba0088d | 1077 | if (NETROOM() > (int)sizeof(str_lm_mode)) { |
b7080c8e A |
1078 | ring_supply_data(&netoring, str_lm_mode, sizeof(str_lm_mode)); |
1079 | printsub('>', &str_lm_mode[2], sizeof(str_lm_mode)-2); | |
1080 | } | |
1081 | /*@*/ else printf("lm_mode: not enough room in buffer\n"); | |
1082 | setconnmode(0); /* set changed mode */ | |
1083 | } | |
1084 | ||
1085 | \f | |
1086 | ||
1087 | /* | |
1088 | * slc() | |
1089 | * Handle special character suboption of LINEMODE. | |
1090 | */ | |
1091 | ||
1092 | struct spc { | |
1093 | cc_t val; | |
1094 | cc_t *valp; | |
1095 | char flags; /* Current flags & level */ | |
1096 | char mylevel; /* Maximum level & flags */ | |
1097 | } spc_data[NSLC+1]; | |
1098 | ||
1099 | #define SLC_IMPORT 0 | |
1100 | #define SLC_EXPORT 1 | |
1101 | #define SLC_RVALUE 2 | |
1102 | static int slc_mode = SLC_EXPORT; | |
1103 | ||
7ba0088d A |
1104 | void |
1105 | slc_init(void) | |
b7080c8e | 1106 | { |
7ba0088d | 1107 | struct spc *spcp; |
b7080c8e A |
1108 | |
1109 | localchars = 1; | |
1110 | for (spcp = spc_data; spcp < &spc_data[NSLC+1]; spcp++) { | |
1111 | spcp->val = 0; | |
1112 | spcp->valp = 0; | |
1113 | spcp->flags = spcp->mylevel = SLC_NOSUPPORT; | |
1114 | } | |
1115 | ||
1116 | #define initfunc(func, flags) { \ | |
1117 | spcp = &spc_data[func]; \ | |
7ba0088d | 1118 | if ((spcp->valp = tcval(func))) { \ |
b7080c8e A |
1119 | spcp->val = *spcp->valp; \ |
1120 | spcp->mylevel = SLC_VARIABLE|flags; \ | |
1121 | } else { \ | |
1122 | spcp->val = 0; \ | |
1123 | spcp->mylevel = SLC_DEFAULT; \ | |
1124 | } \ | |
1125 | } | |
1126 | ||
1127 | initfunc(SLC_SYNCH, 0); | |
1128 | /* No BRK */ | |
1129 | initfunc(SLC_AO, 0); | |
1130 | initfunc(SLC_AYT, 0); | |
1131 | /* No EOR */ | |
1132 | initfunc(SLC_ABORT, SLC_FLUSHIN|SLC_FLUSHOUT); | |
1133 | initfunc(SLC_EOF, 0); | |
1134 | #ifndef SYSV_TERMIO | |
1135 | initfunc(SLC_SUSP, SLC_FLUSHIN); | |
1136 | #endif | |
1137 | initfunc(SLC_EC, 0); | |
1138 | initfunc(SLC_EL, 0); | |
1139 | #ifndef SYSV_TERMIO | |
1140 | initfunc(SLC_EW, 0); | |
1141 | initfunc(SLC_RP, 0); | |
1142 | initfunc(SLC_LNEXT, 0); | |
1143 | #endif | |
1144 | initfunc(SLC_XON, 0); | |
1145 | initfunc(SLC_XOFF, 0); | |
1146 | #ifdef SYSV_TERMIO | |
1147 | spc_data[SLC_XON].mylevel = SLC_CANTCHANGE; | |
1148 | spc_data[SLC_XOFF].mylevel = SLC_CANTCHANGE; | |
1149 | #endif | |
1150 | initfunc(SLC_FORW1, 0); | |
1151 | #ifdef USE_TERMIO | |
1152 | initfunc(SLC_FORW2, 0); | |
1153 | /* No FORW2 */ | |
1154 | #endif | |
1155 | ||
1156 | initfunc(SLC_IP, SLC_FLUSHIN|SLC_FLUSHOUT); | |
1157 | #undef initfunc | |
1158 | ||
1159 | if (slc_mode == SLC_EXPORT) | |
1160 | slc_export(); | |
1161 | else | |
1162 | slc_import(1); | |
1163 | ||
1164 | } | |
1165 | ||
7ba0088d A |
1166 | void |
1167 | slcstate(void) | |
b7080c8e A |
1168 | { |
1169 | printf("Special characters are %s values\n", | |
1170 | slc_mode == SLC_IMPORT ? "remote default" : | |
1171 | slc_mode == SLC_EXPORT ? "local" : | |
1172 | "remote"); | |
1173 | } | |
1174 | ||
7ba0088d A |
1175 | void |
1176 | slc_mode_export(void) | |
b7080c8e A |
1177 | { |
1178 | slc_mode = SLC_EXPORT; | |
1179 | if (my_state_is_will(TELOPT_LINEMODE)) | |
1180 | slc_export(); | |
1181 | } | |
1182 | ||
7ba0088d A |
1183 | void |
1184 | slc_mode_import(int def) | |
b7080c8e A |
1185 | { |
1186 | slc_mode = def ? SLC_IMPORT : SLC_RVALUE; | |
1187 | if (my_state_is_will(TELOPT_LINEMODE)) | |
1188 | slc_import(def); | |
1189 | } | |
1190 | ||
1191 | unsigned char slc_import_val[] = { | |
1192 | IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_VARIABLE, 0, IAC, SE | |
1193 | }; | |
1194 | unsigned char slc_import_def[] = { | |
1195 | IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_DEFAULT, 0, IAC, SE | |
1196 | }; | |
1197 | ||
7ba0088d A |
1198 | void |
1199 | slc_import(int def) | |
b7080c8e | 1200 | { |
7ba0088d | 1201 | if (NETROOM() > (int)sizeof(slc_import_val)) { |
b7080c8e A |
1202 | if (def) { |
1203 | ring_supply_data(&netoring, slc_import_def, sizeof(slc_import_def)); | |
1204 | printsub('>', &slc_import_def[2], sizeof(slc_import_def)-2); | |
1205 | } else { | |
1206 | ring_supply_data(&netoring, slc_import_val, sizeof(slc_import_val)); | |
1207 | printsub('>', &slc_import_val[2], sizeof(slc_import_val)-2); | |
1208 | } | |
1209 | } | |
1210 | /*@*/ else printf("slc_import: not enough room\n"); | |
1211 | } | |
1212 | ||
7ba0088d A |
1213 | void |
1214 | slc_export(void) | |
b7080c8e | 1215 | { |
7ba0088d | 1216 | struct spc *spcp; |
b7080c8e A |
1217 | |
1218 | TerminalDefaultChars(); | |
1219 | ||
1220 | slc_start_reply(); | |
1221 | for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) { | |
1222 | if (spcp->mylevel != SLC_NOSUPPORT) { | |
1223 | if (spcp->val == (cc_t)(_POSIX_VDISABLE)) | |
1224 | spcp->flags = SLC_NOSUPPORT; | |
1225 | else | |
1226 | spcp->flags = spcp->mylevel; | |
1227 | if (spcp->valp) | |
1228 | spcp->val = *spcp->valp; | |
1229 | slc_add_reply(spcp - spc_data, spcp->flags, spcp->val); | |
1230 | } | |
1231 | } | |
1232 | slc_end_reply(); | |
1233 | (void)slc_update(); | |
1234 | setconnmode(1); /* Make sure the character values are set */ | |
1235 | } | |
1236 | ||
7ba0088d A |
1237 | void |
1238 | slc(unsigned char *cp, int len) | |
b7080c8e | 1239 | { |
7ba0088d A |
1240 | struct spc *spcp; |
1241 | int func,level; | |
b7080c8e A |
1242 | |
1243 | slc_start_reply(); | |
1244 | ||
1245 | for (; len >= 3; len -=3, cp +=3) { | |
1246 | ||
1247 | func = cp[SLC_FUNC]; | |
1248 | ||
1249 | if (func == 0) { | |
1250 | /* | |
1251 | * Client side: always ignore 0 function. | |
1252 | */ | |
1253 | continue; | |
1254 | } | |
1255 | if (func > NSLC) { | |
1256 | if ((cp[SLC_FLAGS] & SLC_LEVELBITS) != SLC_NOSUPPORT) | |
1257 | slc_add_reply(func, SLC_NOSUPPORT, 0); | |
1258 | continue; | |
1259 | } | |
1260 | ||
1261 | spcp = &spc_data[func]; | |
1262 | ||
1263 | level = cp[SLC_FLAGS]&(SLC_LEVELBITS|SLC_ACK); | |
1264 | ||
1265 | if ((cp[SLC_VALUE] == (unsigned char)spcp->val) && | |
1266 | ((level&SLC_LEVELBITS) == (spcp->flags&SLC_LEVELBITS))) { | |
1267 | continue; | |
1268 | } | |
1269 | ||
1270 | if (level == (SLC_DEFAULT|SLC_ACK)) { | |
1271 | /* | |
1272 | * This is an error condition, the SLC_ACK | |
1273 | * bit should never be set for the SLC_DEFAULT | |
1274 | * level. Our best guess to recover is to | |
1275 | * ignore the SLC_ACK bit. | |
1276 | */ | |
1277 | cp[SLC_FLAGS] &= ~SLC_ACK; | |
1278 | } | |
1279 | ||
1280 | if (level == ((spcp->flags&SLC_LEVELBITS)|SLC_ACK)) { | |
1281 | spcp->val = (cc_t)cp[SLC_VALUE]; | |
1282 | spcp->flags = cp[SLC_FLAGS]; /* include SLC_ACK */ | |
1283 | continue; | |
1284 | } | |
1285 | ||
1286 | level &= ~SLC_ACK; | |
1287 | ||
1288 | if (level <= (spcp->mylevel&SLC_LEVELBITS)) { | |
1289 | spcp->flags = cp[SLC_FLAGS]|SLC_ACK; | |
1290 | spcp->val = (cc_t)cp[SLC_VALUE]; | |
1291 | } | |
1292 | if (level == SLC_DEFAULT) { | |
1293 | if ((spcp->mylevel&SLC_LEVELBITS) != SLC_DEFAULT) | |
1294 | spcp->flags = spcp->mylevel; | |
1295 | else | |
1296 | spcp->flags = SLC_NOSUPPORT; | |
1297 | } | |
1298 | slc_add_reply(func, spcp->flags, spcp->val); | |
1299 | } | |
1300 | slc_end_reply(); | |
1301 | if (slc_update()) | |
1302 | setconnmode(1); /* set the new character values */ | |
1303 | } | |
1304 | ||
7ba0088d A |
1305 | void |
1306 | slc_check(void) | |
b7080c8e | 1307 | { |
7ba0088d | 1308 | struct spc *spcp; |
b7080c8e A |
1309 | |
1310 | slc_start_reply(); | |
1311 | for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) { | |
1312 | if (spcp->valp && spcp->val != *spcp->valp) { | |
1313 | spcp->val = *spcp->valp; | |
1314 | if (spcp->val == (cc_t)(_POSIX_VDISABLE)) | |
1315 | spcp->flags = SLC_NOSUPPORT; | |
1316 | else | |
1317 | spcp->flags = spcp->mylevel; | |
1318 | slc_add_reply(spcp - spc_data, spcp->flags, spcp->val); | |
1319 | } | |
1320 | } | |
1321 | slc_end_reply(); | |
1322 | setconnmode(1); | |
1323 | } | |
1324 | ||
b7080c8e | 1325 | unsigned char slc_reply[128]; |
2b484d24 | 1326 | unsigned char const * const slc_reply_eom = &slc_reply[sizeof(slc_reply)]; |
b7080c8e A |
1327 | unsigned char *slc_replyp; |
1328 | ||
7ba0088d A |
1329 | void |
1330 | slc_start_reply(void) | |
b7080c8e A |
1331 | { |
1332 | slc_replyp = slc_reply; | |
1333 | *slc_replyp++ = IAC; | |
1334 | *slc_replyp++ = SB; | |
1335 | *slc_replyp++ = TELOPT_LINEMODE; | |
1336 | *slc_replyp++ = LM_SLC; | |
1337 | } | |
1338 | ||
7ba0088d A |
1339 | void |
1340 | slc_add_reply(unsigned char func, unsigned char flags, cc_t value) | |
b7080c8e | 1341 | { |
2b484d24 A |
1342 | /* A sequence of up to 6 bytes my be written for this member of the SLC |
1343 | * suboption list by this function. The end of negotiation command, | |
1344 | * which is written by slc_end_reply(), will require 2 additional | |
1345 | * bytes. Do not proceed unless there is sufficient space for these | |
1346 | * items. | |
1347 | */ | |
1348 | if (&slc_replyp[6+2] > slc_reply_eom) | |
1349 | return; | |
b7080c8e A |
1350 | if ((*slc_replyp++ = func) == IAC) |
1351 | *slc_replyp++ = IAC; | |
1352 | if ((*slc_replyp++ = flags) == IAC) | |
1353 | *slc_replyp++ = IAC; | |
1354 | if ((*slc_replyp++ = (unsigned char)value) == IAC) | |
1355 | *slc_replyp++ = IAC; | |
1356 | } | |
1357 | ||
7ba0088d A |
1358 | void |
1359 | slc_end_reply(void) | |
b7080c8e | 1360 | { |
7ba0088d | 1361 | int len; |
b7080c8e A |
1362 | |
1363 | *slc_replyp++ = IAC; | |
1364 | *slc_replyp++ = SE; | |
1365 | len = slc_replyp - slc_reply; | |
1366 | if (len <= 6) | |
1367 | return; | |
1368 | if (NETROOM() > len) { | |
1369 | ring_supply_data(&netoring, slc_reply, slc_replyp - slc_reply); | |
1370 | printsub('>', &slc_reply[2], slc_replyp - slc_reply - 2); | |
1371 | } | |
1372 | /*@*/else printf("slc_end_reply: not enough room\n"); | |
1373 | } | |
1374 | ||
7ba0088d A |
1375 | int |
1376 | slc_update(void) | |
b7080c8e | 1377 | { |
7ba0088d | 1378 | struct spc *spcp; |
b7080c8e A |
1379 | int need_update = 0; |
1380 | ||
1381 | for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) { | |
1382 | if (!(spcp->flags&SLC_ACK)) | |
1383 | continue; | |
1384 | spcp->flags &= ~SLC_ACK; | |
1385 | if (spcp->valp && (*spcp->valp != spcp->val)) { | |
1386 | *spcp->valp = spcp->val; | |
1387 | need_update = 1; | |
1388 | } | |
1389 | } | |
1390 | return(need_update); | |
1391 | } | |
1392 | ||
1393 | #ifdef OLD_ENVIRON | |
1394 | # ifdef ENV_HACK | |
1395 | /* | |
1396 | * Earlier version of telnet/telnetd from the BSD code had | |
1397 | * the definitions of VALUE and VAR reversed. To ensure | |
1398 | * maximum interoperability, we assume that the server is | |
1399 | * an older BSD server, until proven otherwise. The newer | |
1400 | * BSD servers should be able to handle either definition, | |
1401 | * so it is better to use the wrong values if we don't | |
1402 | * know what type of server it is. | |
1403 | */ | |
1404 | int env_auto = 1; | |
1405 | int old_env_var = OLD_ENV_VAR; | |
1406 | int old_env_value = OLD_ENV_VALUE; | |
1407 | # else | |
1408 | # define old_env_var OLD_ENV_VAR | |
1409 | # define old_env_value OLD_ENV_VALUE | |
1410 | # endif | |
1411 | #endif | |
1412 | ||
7ba0088d A |
1413 | void |
1414 | env_opt(unsigned char *buf, int len) | |
b7080c8e | 1415 | { |
7ba0088d A |
1416 | unsigned char *ep = 0, *epc = 0; |
1417 | int i; | |
b7080c8e A |
1418 | |
1419 | switch(buf[0]&0xff) { | |
1420 | case TELQUAL_SEND: | |
1421 | env_opt_start(); | |
1422 | if (len == 1) { | |
1423 | env_opt_add(NULL); | |
1424 | } else for (i = 1; i < len; i++) { | |
1425 | switch (buf[i]&0xff) { | |
1426 | #ifdef OLD_ENVIRON | |
1427 | case OLD_ENV_VAR: | |
1428 | # ifdef ENV_HACK | |
1429 | if (telopt_environ == TELOPT_OLD_ENVIRON | |
1430 | && env_auto) { | |
1431 | /* Server has the same definitions */ | |
1432 | old_env_var = OLD_ENV_VAR; | |
1433 | old_env_value = OLD_ENV_VALUE; | |
1434 | } | |
1435 | /* FALL THROUGH */ | |
1436 | # endif | |
1437 | case OLD_ENV_VALUE: | |
1438 | /* | |
1439 | * Although OLD_ENV_VALUE is not legal, we will | |
1440 | * still recognize it, just in case it is an | |
1441 | * old server that has VAR & VALUE mixed up... | |
1442 | */ | |
1443 | /* FALL THROUGH */ | |
1444 | #else | |
1445 | case NEW_ENV_VAR: | |
1446 | #endif | |
1447 | case ENV_USERVAR: | |
1448 | if (ep) { | |
1449 | *epc = 0; | |
1450 | env_opt_add(ep); | |
1451 | } | |
1452 | ep = epc = &buf[i+1]; | |
1453 | break; | |
1454 | case ENV_ESC: | |
1455 | i++; | |
1456 | /*FALL THROUGH*/ | |
1457 | default: | |
1458 | if (epc) | |
1459 | *epc++ = buf[i]; | |
1460 | break; | |
1461 | } | |
1462 | } | |
1463 | if (ep) { | |
1464 | *epc = 0; | |
1465 | env_opt_add(ep); | |
1466 | } | |
1467 | env_opt_end(1); | |
1468 | break; | |
1469 | ||
1470 | case TELQUAL_IS: | |
1471 | case TELQUAL_INFO: | |
1472 | /* Ignore for now. We shouldn't get it anyway. */ | |
1473 | break; | |
1474 | ||
1475 | default: | |
1476 | break; | |
1477 | } | |
1478 | } | |
1479 | ||
1480 | #define OPT_REPLY_SIZE 256 | |
1481 | unsigned char *opt_reply; | |
1482 | unsigned char *opt_replyp; | |
1483 | unsigned char *opt_replyend; | |
1484 | ||
7ba0088d A |
1485 | void |
1486 | env_opt_start(void) | |
b7080c8e A |
1487 | { |
1488 | if (opt_reply) | |
1489 | opt_reply = (unsigned char *)realloc(opt_reply, OPT_REPLY_SIZE); | |
1490 | else | |
1491 | opt_reply = (unsigned char *)malloc(OPT_REPLY_SIZE); | |
1492 | if (opt_reply == NULL) { | |
1493 | /*@*/ printf("env_opt_start: malloc()/realloc() failed!!!\n"); | |
1494 | opt_reply = opt_replyp = opt_replyend = NULL; | |
1495 | return; | |
1496 | } | |
1497 | opt_replyp = opt_reply; | |
1498 | opt_replyend = opt_reply + OPT_REPLY_SIZE; | |
1499 | *opt_replyp++ = IAC; | |
1500 | *opt_replyp++ = SB; | |
1501 | *opt_replyp++ = telopt_environ; | |
1502 | *opt_replyp++ = TELQUAL_IS; | |
1503 | } | |
1504 | ||
7ba0088d A |
1505 | void |
1506 | env_opt_start_info(void) | |
b7080c8e A |
1507 | { |
1508 | env_opt_start(); | |
1509 | if (opt_replyp) | |
1510 | opt_replyp[-1] = TELQUAL_INFO; | |
1511 | } | |
1512 | ||
7ba0088d A |
1513 | void |
1514 | env_opt_add(unsigned char *ep) | |
b7080c8e | 1515 | { |
7ba0088d | 1516 | unsigned char *vp, c; |
b7080c8e A |
1517 | |
1518 | if (opt_reply == NULL) /*XXX*/ | |
1519 | return; /*XXX*/ | |
1520 | ||
1521 | if (ep == NULL || *ep == '\0') { | |
1522 | /* Send user defined variables first. */ | |
1523 | env_default(1, 0); | |
7ba0088d | 1524 | while ((ep = env_default(0, 0))) |
b7080c8e A |
1525 | env_opt_add(ep); |
1526 | ||
1527 | /* Now add the list of well know variables. */ | |
1528 | env_default(1, 1); | |
7ba0088d | 1529 | while ((ep = env_default(0, 1))) |
b7080c8e A |
1530 | env_opt_add(ep); |
1531 | return; | |
1532 | } | |
1533 | vp = env_getvalue(ep); | |
2b484d24 A |
1534 | if (opt_replyp + 2*(vp ? strlen((char *)vp) : 0) + |
1535 | 2*strlen((char *)ep) + 6 > opt_replyend) | |
b7080c8e | 1536 | { |
7ba0088d | 1537 | int len; |
2b484d24 | 1538 | opt_replyend += OPT_REPLY_SIZE + 2*strlen((char *)ep) + 2*(vp ? strlen((char *)vp) : 0); |
b7080c8e A |
1539 | len = opt_replyend - opt_reply; |
1540 | opt_reply = (unsigned char *)realloc(opt_reply, len); | |
1541 | if (opt_reply == NULL) { | |
1542 | /*@*/ printf("env_opt_add: realloc() failed!!!\n"); | |
1543 | opt_reply = opt_replyp = opt_replyend = NULL; | |
1544 | return; | |
1545 | } | |
1546 | opt_replyp = opt_reply + len - (opt_replyend - opt_replyp); | |
1547 | opt_replyend = opt_reply + len; | |
1548 | } | |
1549 | if (opt_welldefined(ep)) | |
1550 | #ifdef OLD_ENVIRON | |
1551 | if (telopt_environ == TELOPT_OLD_ENVIRON) | |
1552 | *opt_replyp++ = old_env_var; | |
1553 | else | |
1554 | #endif | |
1555 | *opt_replyp++ = NEW_ENV_VAR; | |
1556 | else | |
1557 | *opt_replyp++ = ENV_USERVAR; | |
1558 | for (;;) { | |
7ba0088d | 1559 | while ((c = *ep++)) { |
b7080c8e A |
1560 | switch(c&0xff) { |
1561 | case IAC: | |
1562 | *opt_replyp++ = IAC; | |
1563 | break; | |
1564 | case NEW_ENV_VAR: | |
1565 | case NEW_ENV_VALUE: | |
1566 | case ENV_ESC: | |
1567 | case ENV_USERVAR: | |
1568 | *opt_replyp++ = ENV_ESC; | |
1569 | break; | |
1570 | } | |
1571 | *opt_replyp++ = c; | |
1572 | } | |
7ba0088d | 1573 | if ((ep = vp)) { |
b7080c8e A |
1574 | #ifdef OLD_ENVIRON |
1575 | if (telopt_environ == TELOPT_OLD_ENVIRON) | |
1576 | *opt_replyp++ = old_env_value; | |
1577 | else | |
1578 | #endif | |
1579 | *opt_replyp++ = NEW_ENV_VALUE; | |
1580 | vp = NULL; | |
1581 | } else | |
1582 | break; | |
1583 | } | |
1584 | } | |
1585 | ||
7ba0088d A |
1586 | int |
1587 | opt_welldefined(const char *ep) | |
b7080c8e A |
1588 | { |
1589 | if ((strcmp(ep, "USER") == 0) || | |
1590 | (strcmp(ep, "DISPLAY") == 0) || | |
1591 | (strcmp(ep, "PRINTER") == 0) || | |
1592 | (strcmp(ep, "SYSTEMTYPE") == 0) || | |
1593 | (strcmp(ep, "JOB") == 0) || | |
1594 | (strcmp(ep, "ACCT") == 0)) | |
1595 | return(1); | |
1596 | return(0); | |
1597 | } | |
7ba0088d A |
1598 | |
1599 | void | |
1600 | env_opt_end(int emptyok) | |
b7080c8e | 1601 | { |
7ba0088d | 1602 | int len; |
b7080c8e A |
1603 | |
1604 | len = opt_replyp - opt_reply + 2; | |
1605 | if (emptyok || len > 6) { | |
1606 | *opt_replyp++ = IAC; | |
1607 | *opt_replyp++ = SE; | |
1608 | if (NETROOM() > len) { | |
1609 | ring_supply_data(&netoring, opt_reply, len); | |
1610 | printsub('>', &opt_reply[2], len - 2); | |
1611 | } | |
1612 | /*@*/ else printf("slc_end_reply: not enough room\n"); | |
1613 | } | |
1614 | if (opt_reply) { | |
1615 | free(opt_reply); | |
1616 | opt_reply = opt_replyp = opt_replyend = NULL; | |
1617 | } | |
1618 | } | |
1619 | ||
1620 | \f | |
1621 | ||
7ba0088d A |
1622 | int |
1623 | telrcv(void) | |
b7080c8e | 1624 | { |
7ba0088d A |
1625 | int c; |
1626 | int scc; | |
1627 | unsigned char *sbp; | |
b7080c8e A |
1628 | int count; |
1629 | int returnValue = 0; | |
1630 | ||
1631 | scc = 0; | |
1632 | count = 0; | |
1633 | while (TTYROOM() > 2) { | |
1634 | if (scc == 0) { | |
1635 | if (count) { | |
1636 | ring_consumed(&netiring, count); | |
1637 | returnValue = 1; | |
1638 | count = 0; | |
1639 | } | |
1640 | sbp = netiring.consume; | |
1641 | scc = ring_full_consecutive(&netiring); | |
1642 | if (scc == 0) { | |
1643 | /* No more data coming in */ | |
1644 | break; | |
1645 | } | |
1646 | } | |
1647 | ||
1648 | c = *sbp++ & 0xff, scc--; count++; | |
1649 | #ifdef ENCRYPTION | |
1650 | if (decrypt_input) | |
1651 | c = (*decrypt_input)(c); | |
1652 | #endif /* ENCRYPTION */ | |
1653 | ||
1654 | switch (telrcv_state) { | |
1655 | ||
1656 | case TS_CR: | |
1657 | telrcv_state = TS_DATA; | |
1658 | if (c == '\0') { | |
1659 | break; /* Ignore \0 after CR */ | |
1660 | } | |
1661 | else if ((c == '\n') && my_want_state_is_dont(TELOPT_ECHO) && !crmod) { | |
1662 | TTYADD(c); | |
1663 | break; | |
1664 | } | |
1665 | /* Else, fall through */ | |
1666 | ||
1667 | case TS_DATA: | |
1668 | if (c == IAC) { | |
1669 | telrcv_state = TS_IAC; | |
1670 | break; | |
1671 | } | |
b7080c8e A |
1672 | /* |
1673 | * The 'crmod' hack (see following) is needed | |
1674 | * since we can't * set CRMOD on output only. | |
1675 | * Machines like MULTICS like to send \r without | |
1676 | * \n; since we must turn off CRMOD to get proper | |
1677 | * input, the mapping is done here (sigh). | |
1678 | */ | |
1679 | if ((c == '\r') && my_want_state_is_dont(TELOPT_BINARY)) { | |
1680 | if (scc > 0) { | |
1681 | c = *sbp&0xff; | |
1682 | #ifdef ENCRYPTION | |
1683 | if (decrypt_input) | |
1684 | c = (*decrypt_input)(c); | |
1685 | #endif /* ENCRYPTION */ | |
1686 | if (c == 0) { | |
1687 | sbp++, scc--; count++; | |
1688 | /* a "true" CR */ | |
1689 | TTYADD('\r'); | |
1690 | } else if (my_want_state_is_dont(TELOPT_ECHO) && | |
1691 | (c == '\n')) { | |
1692 | sbp++, scc--; count++; | |
1693 | TTYADD('\n'); | |
1694 | } else { | |
1695 | #ifdef ENCRYPTION | |
1696 | if (decrypt_input) | |
1697 | (*decrypt_input)(-1); | |
1698 | #endif /* ENCRYPTION */ | |
1699 | ||
1700 | TTYADD('\r'); | |
1701 | if (crmod) { | |
1702 | TTYADD('\n'); | |
1703 | } | |
1704 | } | |
1705 | } else { | |
1706 | telrcv_state = TS_CR; | |
1707 | TTYADD('\r'); | |
1708 | if (crmod) { | |
1709 | TTYADD('\n'); | |
1710 | } | |
1711 | } | |
1712 | } else { | |
1713 | TTYADD(c); | |
1714 | } | |
1715 | continue; | |
1716 | ||
1717 | case TS_IAC: | |
1718 | process_iac: | |
1719 | switch (c) { | |
1720 | ||
1721 | case WILL: | |
1722 | telrcv_state = TS_WILL; | |
1723 | continue; | |
1724 | ||
1725 | case WONT: | |
1726 | telrcv_state = TS_WONT; | |
1727 | continue; | |
1728 | ||
1729 | case DO: | |
1730 | telrcv_state = TS_DO; | |
1731 | continue; | |
1732 | ||
1733 | case DONT: | |
1734 | telrcv_state = TS_DONT; | |
1735 | continue; | |
1736 | ||
1737 | case DM: | |
1738 | /* | |
1739 | * We may have missed an urgent notification, | |
1740 | * so make sure we flush whatever is in the | |
1741 | * buffer currently. | |
1742 | */ | |
1743 | printoption("RCVD", IAC, DM); | |
1744 | SYNCHing = 1; | |
1745 | (void) ttyflush(1); | |
1746 | SYNCHing = stilloob(); | |
1747 | settimer(gotDM); | |
1748 | break; | |
1749 | ||
1750 | case SB: | |
1751 | SB_CLEAR(); | |
1752 | telrcv_state = TS_SB; | |
1753 | continue; | |
1754 | ||
b7080c8e | 1755 | case IAC: |
b7080c8e | 1756 | TTYADD(IAC); |
b7080c8e A |
1757 | break; |
1758 | ||
1759 | case NOP: | |
1760 | case GA: | |
1761 | default: | |
1762 | printoption("RCVD", IAC, c); | |
1763 | break; | |
1764 | } | |
1765 | telrcv_state = TS_DATA; | |
1766 | continue; | |
1767 | ||
1768 | case TS_WILL: | |
1769 | printoption("RCVD", WILL, c); | |
1770 | willoption(c); | |
b7080c8e A |
1771 | telrcv_state = TS_DATA; |
1772 | continue; | |
1773 | ||
1774 | case TS_WONT: | |
1775 | printoption("RCVD", WONT, c); | |
1776 | wontoption(c); | |
b7080c8e A |
1777 | telrcv_state = TS_DATA; |
1778 | continue; | |
1779 | ||
1780 | case TS_DO: | |
1781 | printoption("RCVD", DO, c); | |
1782 | dooption(c); | |
b7080c8e A |
1783 | if (c == TELOPT_NAWS) { |
1784 | sendnaws(); | |
1785 | } else if (c == TELOPT_LFLOW) { | |
1786 | localflow = 1; | |
1787 | setcommandmode(); | |
1788 | setconnmode(0); | |
1789 | } | |
1790 | telrcv_state = TS_DATA; | |
1791 | continue; | |
1792 | ||
1793 | case TS_DONT: | |
1794 | printoption("RCVD", DONT, c); | |
1795 | dontoption(c); | |
1796 | flushline = 1; | |
1797 | setconnmode(0); /* set new tty mode (maybe) */ | |
b7080c8e A |
1798 | telrcv_state = TS_DATA; |
1799 | continue; | |
1800 | ||
1801 | case TS_SB: | |
1802 | if (c == IAC) { | |
1803 | telrcv_state = TS_SE; | |
1804 | } else { | |
1805 | SB_ACCUM(c); | |
1806 | } | |
1807 | continue; | |
1808 | ||
1809 | case TS_SE: | |
1810 | if (c != SE) { | |
1811 | if (c != IAC) { | |
1812 | /* | |
1813 | * This is an error. We only expect to get | |
1814 | * "IAC IAC" or "IAC SE". Several things may | |
1815 | * have happend. An IAC was not doubled, the | |
1816 | * IAC SE was left off, or another option got | |
1817 | * inserted into the suboption are all possibilities. | |
1818 | * If we assume that the IAC was not doubled, | |
1819 | * and really the IAC SE was left off, we could | |
1820 | * get into an infinate loop here. So, instead, | |
1821 | * we terminate the suboption, and process the | |
1822 | * partial suboption if we can. | |
1823 | */ | |
1824 | SB_ACCUM(IAC); | |
1825 | SB_ACCUM(c); | |
1826 | subpointer -= 2; | |
1827 | SB_TERM(); | |
1828 | ||
1829 | printoption("In SUBOPTION processing, RCVD", IAC, c); | |
1830 | suboption(); /* handle sub-option */ | |
b7080c8e A |
1831 | telrcv_state = TS_IAC; |
1832 | goto process_iac; | |
1833 | } | |
1834 | SB_ACCUM(c); | |
1835 | telrcv_state = TS_SB; | |
1836 | } else { | |
1837 | SB_ACCUM(IAC); | |
1838 | SB_ACCUM(SE); | |
1839 | subpointer -= 2; | |
1840 | SB_TERM(); | |
1841 | suboption(); /* handle sub-option */ | |
b7080c8e A |
1842 | telrcv_state = TS_DATA; |
1843 | } | |
1844 | } | |
1845 | } | |
1846 | if (count) | |
1847 | ring_consumed(&netiring, count); | |
1848 | return returnValue||count; | |
1849 | } | |
1850 | ||
1851 | static int bol = 1, local = 0; | |
1852 | ||
7ba0088d A |
1853 | int |
1854 | rlogin_susp(void) | |
b7080c8e A |
1855 | { |
1856 | if (local) { | |
1857 | local = 0; | |
1858 | bol = 1; | |
1859 | command(0, "z\n", 2); | |
1860 | return(1); | |
1861 | } | |
1862 | return(0); | |
1863 | } | |
1864 | ||
7ba0088d A |
1865 | static int |
1866 | telsnd(void) | |
b7080c8e A |
1867 | { |
1868 | int tcc; | |
1869 | int count; | |
1870 | int returnValue = 0; | |
1871 | unsigned char *tbp; | |
1872 | ||
1873 | tcc = 0; | |
1874 | count = 0; | |
1875 | while (NETROOM() > 2) { | |
7ba0088d A |
1876 | int sc; |
1877 | int c; | |
b7080c8e A |
1878 | |
1879 | if (tcc == 0) { | |
1880 | if (count) { | |
1881 | ring_consumed(&ttyiring, count); | |
1882 | returnValue = 1; | |
1883 | count = 0; | |
1884 | } | |
1885 | tbp = ttyiring.consume; | |
1886 | tcc = ring_full_consecutive(&ttyiring); | |
1887 | if (tcc == 0) { | |
1888 | break; | |
1889 | } | |
1890 | } | |
1891 | c = *tbp++ & 0xff, sc = strip(c), tcc--; count++; | |
1892 | if (rlogin != _POSIX_VDISABLE) { | |
1893 | if (bol) { | |
1894 | bol = 0; | |
1895 | if (sc == rlogin) { | |
1896 | local = 1; | |
1897 | continue; | |
1898 | } | |
1899 | } else if (local) { | |
1900 | local = 0; | |
1901 | if (sc == '.' || c == termEofChar) { | |
1902 | bol = 1; | |
1903 | command(0, "close\n", 6); | |
1904 | continue; | |
1905 | } | |
1906 | if (sc == termSuspChar) { | |
1907 | bol = 1; | |
1908 | command(0, "z\n", 2); | |
1909 | continue; | |
1910 | } | |
1911 | if (sc == escape) { | |
7ba0088d | 1912 | command(0, tbp, tcc); |
b7080c8e A |
1913 | bol = 1; |
1914 | count += tcc; | |
1915 | tcc = 0; | |
1916 | flushline = 1; | |
1917 | break; | |
1918 | } | |
1919 | if (sc != rlogin) { | |
1920 | ++tcc; | |
1921 | --tbp; | |
1922 | --count; | |
1923 | c = sc = rlogin; | |
1924 | } | |
1925 | } | |
1926 | if ((sc == '\n') || (sc == '\r')) | |
1927 | bol = 1; | |
7ba0088d | 1928 | } else if (escape != _POSIX_VDISABLE && sc == escape) { |
b7080c8e A |
1929 | /* |
1930 | * Double escape is a pass through of a single escape character. | |
1931 | */ | |
1932 | if (tcc && strip(*tbp) == escape) { | |
1933 | tbp++; | |
1934 | tcc--; | |
1935 | count++; | |
1936 | bol = 0; | |
1937 | } else { | |
1938 | command(0, (char *)tbp, tcc); | |
1939 | bol = 1; | |
1940 | count += tcc; | |
1941 | tcc = 0; | |
1942 | flushline = 1; | |
1943 | break; | |
1944 | } | |
1945 | } else | |
1946 | bol = 0; | |
1947 | #ifdef KLUDGELINEMODE | |
1948 | if (kludgelinemode && (globalmode&MODE_EDIT) && (sc == echoc)) { | |
1949 | if (tcc > 0 && strip(*tbp) == echoc) { | |
1950 | tcc--; tbp++; count++; | |
1951 | } else { | |
1952 | dontlecho = !dontlecho; | |
1953 | settimer(echotoggle); | |
1954 | setconnmode(0); | |
1955 | flushline = 1; | |
1956 | break; | |
1957 | } | |
1958 | } | |
1959 | #endif | |
1960 | if (MODE_LOCAL_CHARS(globalmode)) { | |
1961 | if (TerminalSpecialChars(sc) == 0) { | |
1962 | bol = 1; | |
1963 | break; | |
1964 | } | |
1965 | } | |
1966 | if (my_want_state_is_wont(TELOPT_BINARY)) { | |
1967 | switch (c) { | |
1968 | case '\n': | |
1969 | /* | |
1970 | * If we are in CRMOD mode (\r ==> \n) | |
1971 | * on our local machine, then probably | |
1972 | * a newline (unix) is CRLF (TELNET). | |
1973 | */ | |
1974 | if (MODE_LOCAL_CHARS(globalmode)) { | |
1975 | NETADD('\r'); | |
1976 | } | |
1977 | NETADD('\n'); | |
1978 | bol = flushline = 1; | |
1979 | break; | |
1980 | case '\r': | |
1981 | if (!crlf) { | |
1982 | NET2ADD('\r', '\0'); | |
1983 | } else { | |
1984 | NET2ADD('\r', '\n'); | |
1985 | } | |
1986 | bol = flushline = 1; | |
1987 | break; | |
1988 | case IAC: | |
1989 | NET2ADD(IAC, IAC); | |
1990 | break; | |
1991 | default: | |
1992 | NETADD(c); | |
1993 | break; | |
1994 | } | |
1995 | } else if (c == IAC) { | |
1996 | NET2ADD(IAC, IAC); | |
1997 | } else { | |
1998 | NETADD(c); | |
1999 | } | |
2000 | } | |
2001 | if (count) | |
2002 | ring_consumed(&ttyiring, count); | |
2003 | return returnValue||count; /* Non-zero if we did anything */ | |
2004 | } | |
2005 | \f | |
2006 | /* | |
2007 | * Scheduler() | |
2008 | * | |
2009 | * Try to do something. | |
2010 | * | |
2011 | * If we do something useful, return 1; else return 0. | |
2012 | * | |
2013 | */ | |
2014 | ||
7ba0088d A |
2015 | static int |
2016 | Scheduler(int block) | |
b7080c8e A |
2017 | { |
2018 | /* One wants to be a bit careful about setting returnValue | |
2019 | * to one, since a one implies we did some useful work, | |
2020 | * and therefore probably won't be called to block next | |
b7080c8e A |
2021 | */ |
2022 | int returnValue; | |
2023 | int netin, netout, netex, ttyin, ttyout; | |
2024 | ||
2025 | /* Decide which rings should be processed */ | |
2026 | ||
2027 | netout = ring_full_count(&netoring) && | |
2028 | (flushline || | |
2029 | (my_want_state_is_wont(TELOPT_LINEMODE) | |
2030 | #ifdef KLUDGELINEMODE | |
2031 | && (!kludgelinemode || my_want_state_is_do(TELOPT_SGA)) | |
2032 | #endif | |
2033 | ) || | |
2034 | my_want_state_is_will(TELOPT_BINARY)); | |
2035 | ttyout = ring_full_count(&ttyoring); | |
2036 | ||
7ba0088d | 2037 | ttyin = ring_empty_count(&ttyiring) && (clienteof == 0); |
b7080c8e | 2038 | |
b7080c8e | 2039 | netin = !ISend && ring_empty_count(&netiring); |
b7080c8e A |
2040 | |
2041 | netex = !SYNCHing; | |
2042 | ||
b7080c8e A |
2043 | /* Call to system code to process rings */ |
2044 | ||
2045 | returnValue = process_rings(netin, netout, netex, ttyin, ttyout, !block); | |
2046 | ||
2047 | /* Now, look at the input rings, looking for work to do. */ | |
2048 | ||
2049 | if (ring_full_count(&ttyiring)) { | |
b7080c8e | 2050 | returnValue |= telsnd(); |
b7080c8e A |
2051 | } |
2052 | ||
2053 | if (ring_full_count(&netiring)) { | |
b7080c8e | 2054 | returnValue |= telrcv(); |
b7080c8e A |
2055 | } |
2056 | return returnValue; | |
2057 | } | |
2058 | \f | |
7ba0088d A |
2059 | #ifdef AUTHENTICATION |
2060 | #define __unusedhere | |
2061 | #else | |
2062 | #define __unusedhere __unused | |
2063 | #endif | |
b7080c8e A |
2064 | /* |
2065 | * Select from tty and network... | |
2066 | */ | |
7ba0088d A |
2067 | void |
2068 | telnet(char *user __unusedhere) | |
b7080c8e A |
2069 | { |
2070 | sys_telnet_init(); | |
2071 | ||
7ba0088d A |
2072 | #ifdef AUTHENTICATION |
2073 | #ifdef ENCRYPTION | |
b7080c8e A |
2074 | { |
2075 | static char local_host[256] = { 0 }; | |
2076 | ||
2077 | if (!local_host[0]) { | |
2078 | gethostname(local_host, sizeof(local_host)); | |
2079 | local_host[sizeof(local_host)-1] = 0; | |
2080 | } | |
2081 | auth_encrypt_init(local_host, hostname, "TELNET", 0); | |
2082 | auth_encrypt_user(user); | |
2083 | } | |
7ba0088d A |
2084 | #endif |
2085 | #endif | |
b7080c8e | 2086 | if (telnetport) { |
7ba0088d | 2087 | #ifdef AUTHENTICATION |
b7080c8e A |
2088 | if (autologin) |
2089 | send_will(TELOPT_AUTHENTICATION, 1); | |
2090 | #endif | |
2091 | #ifdef ENCRYPTION | |
2092 | send_do(TELOPT_ENCRYPT, 1); | |
2093 | send_will(TELOPT_ENCRYPT, 1); | |
2094 | #endif /* ENCRYPTION */ | |
2095 | send_do(TELOPT_SGA, 1); | |
2096 | send_will(TELOPT_TTYPE, 1); | |
2097 | send_will(TELOPT_NAWS, 1); | |
2098 | send_will(TELOPT_TSPEED, 1); | |
2099 | send_will(TELOPT_LFLOW, 1); | |
2100 | send_will(TELOPT_LINEMODE, 1); | |
2101 | send_will(TELOPT_NEW_ENVIRON, 1); | |
2102 | send_do(TELOPT_STATUS, 1); | |
7ba0088d | 2103 | if (env_getvalue("DISPLAY")) |
b7080c8e A |
2104 | send_will(TELOPT_XDISPLOC, 1); |
2105 | if (eight) | |
2106 | tel_enter_binary(eight); | |
2107 | } | |
b7080c8e | 2108 | |
b7080c8e A |
2109 | for (;;) { |
2110 | int schedValue; | |
2111 | ||
2112 | while ((schedValue = Scheduler(0)) != 0) { | |
2113 | if (schedValue == -1) { | |
2114 | setcommandmode(); | |
2115 | return; | |
2116 | } | |
2117 | } | |
2118 | ||
2119 | if (Scheduler(1) == -1) { | |
2120 | setcommandmode(); | |
2121 | return; | |
2122 | } | |
2123 | } | |
b7080c8e A |
2124 | } |
2125 | \f | |
2126 | #if 0 /* XXX - this not being in is a bug */ | |
2127 | /* | |
2128 | * nextitem() | |
2129 | * | |
2130 | * Return the address of the next "item" in the TELNET data | |
2131 | * stream. This will be the address of the next character if | |
2132 | * the current address is a user data character, or it will | |
2133 | * be the address of the character following the TELNET command | |
2134 | * if the current address is a TELNET IAC ("I Am a Command") | |
2135 | * character. | |
2136 | */ | |
2137 | ||
7ba0088d A |
2138 | static char * |
2139 | nextitem(char *current) | |
b7080c8e A |
2140 | { |
2141 | if ((*current&0xff) != IAC) { | |
2142 | return current+1; | |
2143 | } | |
2144 | switch (*(current+1)&0xff) { | |
2145 | case DO: | |
2146 | case DONT: | |
2147 | case WILL: | |
2148 | case WONT: | |
2149 | return current+3; | |
2150 | case SB: /* loop forever looking for the SE */ | |
2151 | { | |
7ba0088d | 2152 | char *look = current+2; |
b7080c8e A |
2153 | |
2154 | for (;;) { | |
2155 | if ((*look++&0xff) == IAC) { | |
2156 | if ((*look++&0xff) == SE) { | |
2157 | return look; | |
2158 | } | |
2159 | } | |
2160 | } | |
2161 | } | |
2162 | default: | |
2163 | return current+2; | |
2164 | } | |
2165 | } | |
2166 | #endif /* 0 */ | |
2167 | ||
2168 | /* | |
2169 | * netclear() | |
2170 | * | |
2171 | * We are about to do a TELNET SYNCH operation. Clear | |
2172 | * the path to the network. | |
2173 | * | |
2174 | * Things are a bit tricky since we may have sent the first | |
2175 | * byte or so of a previous TELNET command into the network. | |
2176 | * So, we have to scan the network buffer from the beginning | |
2177 | * until we are up to where we want to be. | |
2178 | * | |
2179 | * A side effect of what we do, just to keep things | |
2180 | * simple, is to clear the urgent data pointer. The principal | |
2181 | * caller should be setting the urgent data pointer AFTER calling | |
2182 | * us in any case. | |
2183 | */ | |
2184 | ||
7ba0088d A |
2185 | static void |
2186 | netclear(void) | |
b7080c8e | 2187 | { |
7ba0088d | 2188 | /* Deleted */ |
b7080c8e A |
2189 | } |
2190 | \f | |
2191 | /* | |
2192 | * These routines add various telnet commands to the data stream. | |
2193 | */ | |
2194 | ||
7ba0088d A |
2195 | static void |
2196 | doflush(void) | |
b7080c8e A |
2197 | { |
2198 | NET2ADD(IAC, DO); | |
2199 | NETADD(TELOPT_TM); | |
2200 | flushline = 1; | |
2201 | flushout = 1; | |
2202 | (void) ttyflush(1); /* Flush/drop output */ | |
2203 | /* do printoption AFTER flush, otherwise the output gets tossed... */ | |
2204 | printoption("SENT", DO, TELOPT_TM); | |
2205 | } | |
2206 | ||
7ba0088d A |
2207 | void |
2208 | xmitAO(void) | |
b7080c8e A |
2209 | { |
2210 | NET2ADD(IAC, AO); | |
2211 | printoption("SENT", IAC, AO); | |
2212 | if (autoflush) { | |
2213 | doflush(); | |
2214 | } | |
2215 | } | |
2216 | ||
7ba0088d A |
2217 | void |
2218 | xmitEL(void) | |
b7080c8e A |
2219 | { |
2220 | NET2ADD(IAC, EL); | |
2221 | printoption("SENT", IAC, EL); | |
2222 | } | |
2223 | ||
7ba0088d A |
2224 | void |
2225 | xmitEC(void) | |
b7080c8e A |
2226 | { |
2227 | NET2ADD(IAC, EC); | |
2228 | printoption("SENT", IAC, EC); | |
2229 | } | |
2230 | ||
7ba0088d A |
2231 | int |
2232 | dosynch(char *ch __unused) | |
b7080c8e A |
2233 | { |
2234 | netclear(); /* clear the path to the network */ | |
2235 | NETADD(IAC); | |
2236 | setneturg(); | |
2237 | NETADD(DM); | |
2238 | printoption("SENT", IAC, DM); | |
2239 | return 1; | |
2240 | } | |
2241 | ||
2242 | int want_status_response = 0; | |
2243 | ||
7ba0088d A |
2244 | int |
2245 | get_status(char *ch __unused) | |
b7080c8e A |
2246 | { |
2247 | unsigned char tmp[16]; | |
7ba0088d | 2248 | unsigned char *cp; |
b7080c8e A |
2249 | |
2250 | if (my_want_state_is_dont(TELOPT_STATUS)) { | |
2251 | printf("Remote side does not support STATUS option\n"); | |
2252 | return 0; | |
2253 | } | |
2254 | cp = tmp; | |
2255 | ||
2256 | *cp++ = IAC; | |
2257 | *cp++ = SB; | |
2258 | *cp++ = TELOPT_STATUS; | |
2259 | *cp++ = TELQUAL_SEND; | |
2260 | *cp++ = IAC; | |
2261 | *cp++ = SE; | |
2262 | if (NETROOM() >= cp - tmp) { | |
2263 | ring_supply_data(&netoring, tmp, cp-tmp); | |
2264 | printsub('>', tmp+2, cp - tmp - 2); | |
2265 | } | |
2266 | ++want_status_response; | |
2267 | return 1; | |
2268 | } | |
2269 | ||
7ba0088d A |
2270 | void |
2271 | intp(void) | |
b7080c8e A |
2272 | { |
2273 | NET2ADD(IAC, IP); | |
2274 | printoption("SENT", IAC, IP); | |
2275 | flushline = 1; | |
2276 | if (autoflush) { | |
2277 | doflush(); | |
2278 | } | |
2279 | if (autosynch) { | |
7ba0088d | 2280 | dosynch(NULL); |
b7080c8e A |
2281 | } |
2282 | } | |
2283 | ||
7ba0088d A |
2284 | void |
2285 | sendbrk(void) | |
b7080c8e A |
2286 | { |
2287 | NET2ADD(IAC, BREAK); | |
2288 | printoption("SENT", IAC, BREAK); | |
2289 | flushline = 1; | |
2290 | if (autoflush) { | |
2291 | doflush(); | |
2292 | } | |
2293 | if (autosynch) { | |
7ba0088d | 2294 | dosynch(NULL); |
b7080c8e A |
2295 | } |
2296 | } | |
2297 | ||
7ba0088d A |
2298 | void |
2299 | sendabort(void) | |
b7080c8e A |
2300 | { |
2301 | NET2ADD(IAC, ABORT); | |
2302 | printoption("SENT", IAC, ABORT); | |
2303 | flushline = 1; | |
2304 | if (autoflush) { | |
2305 | doflush(); | |
2306 | } | |
2307 | if (autosynch) { | |
7ba0088d | 2308 | dosynch(NULL); |
b7080c8e A |
2309 | } |
2310 | } | |
2311 | ||
7ba0088d A |
2312 | void |
2313 | sendsusp(void) | |
b7080c8e A |
2314 | { |
2315 | NET2ADD(IAC, SUSP); | |
2316 | printoption("SENT", IAC, SUSP); | |
2317 | flushline = 1; | |
2318 | if (autoflush) { | |
2319 | doflush(); | |
2320 | } | |
2321 | if (autosynch) { | |
7ba0088d | 2322 | dosynch(NULL); |
b7080c8e A |
2323 | } |
2324 | } | |
2325 | ||
7ba0088d A |
2326 | void |
2327 | sendeof(void) | |
b7080c8e A |
2328 | { |
2329 | NET2ADD(IAC, xEOF); | |
2330 | printoption("SENT", IAC, xEOF); | |
2331 | } | |
2332 | ||
7ba0088d A |
2333 | void |
2334 | sendayt(void) | |
b7080c8e A |
2335 | { |
2336 | NET2ADD(IAC, AYT); | |
2337 | printoption("SENT", IAC, AYT); | |
2338 | } | |
2339 | ||
2340 | /* | |
2341 | * Send a window size update to the remote system. | |
2342 | */ | |
2343 | ||
7ba0088d A |
2344 | void |
2345 | sendnaws(void) | |
b7080c8e A |
2346 | { |
2347 | long rows, cols; | |
2348 | unsigned char tmp[16]; | |
7ba0088d | 2349 | unsigned char *cp; |
b7080c8e A |
2350 | |
2351 | if (my_state_is_wont(TELOPT_NAWS)) | |
2352 | return; | |
2353 | ||
2354 | #define PUTSHORT(cp, x) { if ((*cp++ = ((x)>>8)&0xff) == IAC) *cp++ = IAC; \ | |
2355 | if ((*cp++ = ((x))&0xff) == IAC) *cp++ = IAC; } | |
2356 | ||
2357 | if (TerminalWindowSize(&rows, &cols) == 0) { /* Failed */ | |
2358 | return; | |
2359 | } | |
2360 | ||
2361 | cp = tmp; | |
2362 | ||
2363 | *cp++ = IAC; | |
2364 | *cp++ = SB; | |
2365 | *cp++ = TELOPT_NAWS; | |
2366 | PUTSHORT(cp, cols); | |
2367 | PUTSHORT(cp, rows); | |
2368 | *cp++ = IAC; | |
2369 | *cp++ = SE; | |
2370 | if (NETROOM() >= cp - tmp) { | |
2371 | ring_supply_data(&netoring, tmp, cp-tmp); | |
2372 | printsub('>', tmp+2, cp - tmp - 2); | |
2373 | } | |
2374 | } | |
2375 | ||
7ba0088d A |
2376 | void |
2377 | tel_enter_binary(int rw) | |
b7080c8e A |
2378 | { |
2379 | if (rw&1) | |
2380 | send_do(TELOPT_BINARY, 1); | |
2381 | if (rw&2) | |
2382 | send_will(TELOPT_BINARY, 1); | |
2383 | } | |
2384 | ||
7ba0088d A |
2385 | void |
2386 | tel_leave_binary(int rw) | |
b7080c8e A |
2387 | { |
2388 | if (rw&1) | |
2389 | send_dont(TELOPT_BINARY, 1); | |
2390 | if (rw&2) | |
2391 | send_wont(TELOPT_BINARY, 1); | |
2392 | } |