374330e2 |
1 | #include <windows.h> |
2 | #include <stdio.h> |
3 | #include <stdlib.h> |
374330e2 |
4 | |
5 | #include "putty.h" |
6 | |
7 | #ifndef FALSE |
8 | #define FALSE 0 |
9 | #endif |
10 | #ifndef TRUE |
11 | #define TRUE 1 |
12 | #endif |
13 | |
32874aea |
14 | #define IAC 255 /* interpret as command: */ |
15 | #define DONT 254 /* you are not to use option */ |
16 | #define DO 253 /* please, you use option */ |
17 | #define WONT 252 /* I won't use option */ |
18 | #define WILL 251 /* I will use option */ |
19 | #define SB 250 /* interpret as subnegotiation */ |
20 | #define SE 240 /* end sub negotiation */ |
21 | |
22 | #define GA 249 /* you may reverse the line */ |
23 | #define EL 248 /* erase the current line */ |
24 | #define EC 247 /* erase the current character */ |
25 | #define AYT 246 /* are you there */ |
26 | #define AO 245 /* abort output--but let prog finish */ |
27 | #define IP 244 /* interrupt process--permanently */ |
28 | #define BREAK 243 /* break */ |
29 | #define DM 242 /* data mark--for connect. cleaning */ |
30 | #define NOP 241 /* nop */ |
31 | #define EOR 239 /* end of record (transparent mode) */ |
32 | #define ABORT 238 /* Abort process */ |
33 | #define SUSP 237 /* Suspend process */ |
34 | #define xEOF 236 /* End of file: EOF is already used... */ |
35 | |
36 | #define TELOPT_BINARY 0 /* 8-bit data path */ |
37 | #define TELOPT_ECHO 1 /* echo */ |
38 | #define TELOPT_RCP 2 /* prepare to reconnect */ |
39 | #define TELOPT_SGA 3 /* suppress go ahead */ |
40 | #define TELOPT_NAMS 4 /* approximate message size */ |
41 | #define TELOPT_STATUS 5 /* give status */ |
42 | #define TELOPT_TM 6 /* timing mark */ |
43 | #define TELOPT_RCTE 7 /* remote controlled transmission and echo */ |
44 | #define TELOPT_NAOL 8 /* negotiate about output line width */ |
45 | #define TELOPT_NAOP 9 /* negotiate about output page size */ |
46 | #define TELOPT_NAOCRD 10 /* negotiate about CR disposition */ |
47 | #define TELOPT_NAOHTS 11 /* negotiate about horizontal tabstops */ |
48 | #define TELOPT_NAOHTD 12 /* negotiate about horizontal tab disposition */ |
49 | #define TELOPT_NAOFFD 13 /* negotiate about formfeed disposition */ |
50 | #define TELOPT_NAOVTS 14 /* negotiate about vertical tab stops */ |
51 | #define TELOPT_NAOVTD 15 /* negotiate about vertical tab disposition */ |
52 | #define TELOPT_NAOLFD 16 /* negotiate about output LF disposition */ |
53 | #define TELOPT_XASCII 17 /* extended ascic character set */ |
54 | #define TELOPT_LOGOUT 18 /* force logout */ |
55 | #define TELOPT_BM 19 /* byte macro */ |
56 | #define TELOPT_DET 20 /* data entry terminal */ |
57 | #define TELOPT_SUPDUP 21 /* supdup protocol */ |
58 | #define TELOPT_SUPDUPOUTPUT 22 /* supdup output */ |
59 | #define TELOPT_SNDLOC 23 /* send location */ |
60 | #define TELOPT_TTYPE 24 /* terminal type */ |
61 | #define TELOPT_EOR 25 /* end or record */ |
62 | #define TELOPT_TUID 26 /* TACACS user identification */ |
63 | #define TELOPT_OUTMRK 27 /* output marking */ |
64 | #define TELOPT_TTYLOC 28 /* terminal location number */ |
65 | #define TELOPT_3270REGIME 29 /* 3270 regime */ |
66 | #define TELOPT_X3PAD 30 /* X.3 PAD */ |
67 | #define TELOPT_NAWS 31 /* window size */ |
68 | #define TELOPT_TSPEED 32 /* terminal speed */ |
69 | #define TELOPT_LFLOW 33 /* remote flow control */ |
70 | #define TELOPT_LINEMODE 34 /* Linemode option */ |
71 | #define TELOPT_XDISPLOC 35 /* X Display Location */ |
72 | #define TELOPT_OLD_ENVIRON 36 /* Old - Environment variables */ |
73 | #define TELOPT_AUTHENTICATION 37 /* Authenticate */ |
74 | #define TELOPT_ENCRYPT 38 /* Encryption option */ |
75 | #define TELOPT_NEW_ENVIRON 39 /* New - Environment variables */ |
76 | #define TELOPT_EXOPL 255 /* extended-options-list */ |
77 | |
78 | #define TELQUAL_IS 0 /* option is... */ |
79 | #define TELQUAL_SEND 1 /* send option */ |
80 | #define TELQUAL_INFO 2 /* ENVIRON: informational version of IS */ |
374330e2 |
81 | #define BSD_VAR 1 |
82 | #define BSD_VALUE 0 |
83 | #define RFC_VAR 0 |
84 | #define RFC_VALUE 1 |
85 | |
86 | #define CR 13 |
87 | #define LF 10 |
88 | #define NUL 0 |
89 | |
90 | #define iswritable(x) ( (x) != IAC && (x) != CR ) |
91 | |
32874aea |
92 | static char *telopt(int opt) |
93 | { |
374330e2 |
94 | #define i(x) if (opt == TELOPT_ ## x) return #x; |
32874aea |
95 | i(BINARY); |
96 | i(ECHO); |
97 | i(RCP); |
98 | i(SGA); |
99 | i(NAMS); |
100 | i(STATUS); |
101 | i(TM); |
102 | i(RCTE); |
103 | i(NAOL); |
104 | i(NAOP); |
105 | i(NAOCRD); |
106 | i(NAOHTS); |
107 | i(NAOHTD); |
108 | i(NAOFFD); |
109 | i(NAOVTS); |
110 | i(NAOVTD); |
111 | i(NAOLFD); |
112 | i(XASCII); |
113 | i(LOGOUT); |
114 | i(BM); |
115 | i(DET); |
116 | i(SUPDUP); |
117 | i(SUPDUPOUTPUT); |
118 | i(SNDLOC); |
119 | i(TTYPE); |
120 | i(EOR); |
121 | i(TUID); |
122 | i(OUTMRK); |
123 | i(TTYLOC); |
124 | i(X3PAD); |
125 | i(NAWS); |
126 | i(TSPEED); |
127 | i(LFLOW); |
128 | i(LINEMODE); |
129 | i(XDISPLOC); |
130 | i(OLD_ENVIRON); |
131 | i(AUTHENTICATION); |
132 | i(ENCRYPT); |
133 | i(NEW_ENVIRON); |
134 | i(EXOPL); |
374330e2 |
135 | #undef i |
136 | return "<unknown>"; |
137 | } |
138 | |
51470298 |
139 | static void telnet_size(void *handle, int width, int height); |
374330e2 |
140 | |
141 | struct Opt { |
142 | int send; /* what we initially send */ |
143 | int nsend; /* -ve send if requested to stop it */ |
144 | int ack, nak; /* +ve and -ve acknowledgements */ |
145 | int option; /* the option code */ |
51470298 |
146 | int index; /* index into telnet->opt_states[] */ |
374330e2 |
147 | enum { |
148 | REQUESTED, ACTIVE, INACTIVE, REALLY_INACTIVE |
51470298 |
149 | } initial_state; |
150 | }; |
151 | |
152 | enum { |
153 | OPTINDEX_NAWS, |
154 | OPTINDEX_TSPEED, |
155 | OPTINDEX_TTYPE, |
156 | OPTINDEX_OENV, |
157 | OPTINDEX_NENV, |
158 | OPTINDEX_ECHO, |
159 | OPTINDEX_WE_SGA, |
160 | OPTINDEX_THEY_SGA, |
161 | NUM_OPTS |
374330e2 |
162 | }; |
163 | |
51470298 |
164 | static const struct Opt o_naws = |
165 | { WILL, WONT, DO, DONT, TELOPT_NAWS, OPTINDEX_NAWS, REQUESTED }; |
166 | static const struct Opt o_tspeed = |
167 | { WILL, WONT, DO, DONT, TELOPT_TSPEED, OPTINDEX_TSPEED, REQUESTED }; |
168 | static const struct Opt o_ttype = |
169 | { WILL, WONT, DO, DONT, TELOPT_TTYPE, OPTINDEX_TTYPE, REQUESTED }; |
170 | static const struct Opt o_oenv = { WILL, WONT, DO, DONT, TELOPT_OLD_ENVIRON, |
171 | OPTINDEX_OENV, INACTIVE |
32874aea |
172 | }; |
51470298 |
173 | static const struct Opt o_nenv = { WILL, WONT, DO, DONT, TELOPT_NEW_ENVIRON, |
174 | OPTINDEX_NENV, REQUESTED |
32874aea |
175 | }; |
51470298 |
176 | static const struct Opt o_echo = |
177 | { DO, DONT, WILL, WONT, TELOPT_ECHO, OPTINDEX_ECHO, REQUESTED }; |
178 | static const struct Opt o_we_sga = |
179 | { WILL, WONT, DO, DONT, TELOPT_SGA, OPTINDEX_WE_SGA, REQUESTED }; |
180 | static const struct Opt o_they_sga = |
181 | { DO, DONT, WILL, WONT, TELOPT_SGA, OPTINDEX_THEY_SGA, REQUESTED }; |
182 | |
183 | static const struct Opt *const opts[] = { |
374330e2 |
184 | &o_naws, &o_tspeed, &o_ttype, &o_oenv, &o_nenv, &o_echo, |
185 | &o_we_sga, &o_they_sga, NULL |
186 | }; |
187 | |
51470298 |
188 | typedef struct telnet_tag { |
189 | const struct plug_function_table *fn; |
190 | /* the above field _must_ be first in the structure */ |
191 | |
192 | Socket s; |
193 | |
194 | void *frontend; |
195 | int term_width, term_height; |
196 | |
197 | int opt_states[NUM_OPTS]; |
198 | |
199 | int echoing, editing; |
200 | int activated; |
201 | int bufsize; |
202 | int in_synch; |
203 | int sb_opt, sb_len; |
204 | char *sb_buf; |
205 | int sb_size; |
206 | |
207 | enum { |
208 | TOP_LEVEL, SEENIAC, SEENWILL, SEENWONT, SEENDO, SEENDONT, |
209 | SEENSB, SUBNEGOT, SUBNEG_IAC, SEENCR |
210 | } state; |
211 | |
212 | } *Telnet; |
213 | |
5471d09a |
214 | #define TELNET_MAX_BACKLOG 4096 |
215 | |
374330e2 |
216 | #define SB_DELTA 1024 |
217 | |
51470298 |
218 | static void c_write1(Telnet telnet, int c) |
32874aea |
219 | { |
5471d09a |
220 | int backlog; |
32874aea |
221 | char cc = (char) c; |
51470298 |
222 | backlog = from_backend(telnet->frontend, 0, &cc, 1); |
223 | sk_set_frozen(telnet->s, backlog > TELNET_MAX_BACKLOG); |
fe50e814 |
224 | } |
225 | |
32874aea |
226 | static void log_option(char *sender, int cmd, int option) |
227 | { |
374330e2 |
228 | char buf[50]; |
229 | sprintf(buf, "%s:\t%s %s", sender, |
230 | (cmd == WILL ? "WILL" : cmd == WONT ? "WONT" : |
231 | cmd == DO ? "DO" : cmd == DONT ? "DONT" : "<??>"), |
232 | telopt(option)); |
c5e9c988 |
233 | logevent(buf); |
374330e2 |
234 | } |
235 | |
51470298 |
236 | static void send_opt(Telnet telnet, int cmd, int option) |
32874aea |
237 | { |
374330e2 |
238 | unsigned char b[3]; |
239 | |
32874aea |
240 | b[0] = IAC; |
241 | b[1] = cmd; |
242 | b[2] = option; |
51470298 |
243 | telnet->bufsize = sk_write(telnet->s, b, 3); |
374330e2 |
244 | log_option("client", cmd, option); |
245 | } |
246 | |
51470298 |
247 | static void deactivate_option(Telnet telnet, const struct Opt *o) |
32874aea |
248 | { |
51470298 |
249 | if (telnet->opt_states[o->index] == REQUESTED || |
250 | telnet->opt_states[o->index] == ACTIVE) |
251 | send_opt(telnet, o->nsend, o->option); |
252 | telnet->opt_states[o->index] = REALLY_INACTIVE; |
374330e2 |
253 | } |
254 | |
708bbbbe |
255 | /* |
256 | * Generate side effects of enabling or disabling an option. |
257 | */ |
51470298 |
258 | static void option_side_effects(Telnet telnet, const struct Opt *o, int enabled) |
32874aea |
259 | { |
0965bee0 |
260 | if (o->option == TELOPT_ECHO && o->send == DO) |
51470298 |
261 | telnet->echoing = !enabled; |
b6c680d4 |
262 | else if (o->option == TELOPT_SGA && o->send == DO) |
51470298 |
263 | telnet->editing = !enabled; |
760e88b2 |
264 | ldisc_send(NULL, 0, 0); /* cause ldisc to notice the change */ |
8faa456c |
265 | |
266 | /* Ensure we get the minimum options */ |
51470298 |
267 | if (!telnet->activated) { |
268 | if (telnet->opt_states[o_echo.index] == INACTIVE) { |
269 | telnet->opt_states[o_echo.index] = REQUESTED; |
270 | send_opt(telnet, o_echo.send, o_echo.option); |
8faa456c |
271 | } |
51470298 |
272 | if (telnet->opt_states[o_we_sga.index] == INACTIVE) { |
273 | telnet->opt_states[o_we_sga.index] = REQUESTED; |
274 | send_opt(telnet, o_we_sga.send, o_we_sga.option); |
8faa456c |
275 | } |
51470298 |
276 | if (telnet->opt_states[o_they_sga.index] == INACTIVE) { |
277 | telnet->opt_states[o_they_sga.index] = REQUESTED; |
278 | send_opt(telnet, o_they_sga.send, o_they_sga.option); |
8faa456c |
279 | } |
51470298 |
280 | telnet->activated = TRUE; |
8faa456c |
281 | } |
708bbbbe |
282 | } |
283 | |
51470298 |
284 | static void activate_option(Telnet telnet, const struct Opt *o) |
32874aea |
285 | { |
374330e2 |
286 | if (o->send == WILL && o->option == TELOPT_NAWS) |
51470298 |
287 | telnet_size(telnet, telnet->term_width, telnet->term_height); |
374330e2 |
288 | if (o->send == WILL && |
289 | (o->option == TELOPT_NEW_ENVIRON || |
290 | o->option == TELOPT_OLD_ENVIRON)) { |
291 | /* |
292 | * We may only have one kind of ENVIRON going at a time. |
293 | * This is a hack, but who cares. |
294 | */ |
51470298 |
295 | deactivate_option(telnet, o->option == |
32874aea |
296 | TELOPT_NEW_ENVIRON ? &o_oenv : &o_nenv); |
374330e2 |
297 | } |
51470298 |
298 | option_side_effects(telnet, o, 1); |
374330e2 |
299 | } |
300 | |
51470298 |
301 | static void refused_option(Telnet telnet, const struct Opt *o) |
32874aea |
302 | { |
374330e2 |
303 | if (o->send == WILL && o->option == TELOPT_NEW_ENVIRON && |
51470298 |
304 | telnet->opt_states[o_oenv.index] == INACTIVE) { |
305 | send_opt(telnet, WILL, TELOPT_OLD_ENVIRON); |
306 | telnet->opt_states[o_oenv.index] = REQUESTED; |
374330e2 |
307 | } |
51470298 |
308 | option_side_effects(telnet, o, 0); |
374330e2 |
309 | } |
310 | |
51470298 |
311 | static void proc_rec_opt(Telnet telnet, int cmd, int option) |
32874aea |
312 | { |
51470298 |
313 | const struct Opt *const *o; |
374330e2 |
314 | |
32874aea |
315 | log_option("server", cmd, option); |
374330e2 |
316 | for (o = opts; *o; o++) { |
317 | if ((*o)->option == option && (*o)->ack == cmd) { |
51470298 |
318 | switch (telnet->opt_states[(*o)->index]) { |
374330e2 |
319 | case REQUESTED: |
51470298 |
320 | telnet->opt_states[(*o)->index] = ACTIVE; |
321 | activate_option(telnet, *o); |
374330e2 |
322 | break; |
323 | case ACTIVE: |
324 | break; |
325 | case INACTIVE: |
51470298 |
326 | telnet->opt_states[(*o)->index] = ACTIVE; |
327 | send_opt(telnet, (*o)->send, option); |
328 | activate_option(telnet, *o); |
374330e2 |
329 | break; |
330 | case REALLY_INACTIVE: |
51470298 |
331 | send_opt(telnet, (*o)->nsend, option); |
374330e2 |
332 | break; |
333 | } |
334 | return; |
335 | } else if ((*o)->option == option && (*o)->nak == cmd) { |
51470298 |
336 | switch (telnet->opt_states[(*o)->index]) { |
374330e2 |
337 | case REQUESTED: |
51470298 |
338 | telnet->opt_states[(*o)->index] = INACTIVE; |
339 | refused_option(telnet, *o); |
374330e2 |
340 | break; |
341 | case ACTIVE: |
51470298 |
342 | telnet->opt_states[(*o)->index] = INACTIVE; |
343 | send_opt(telnet, (*o)->nsend, option); |
344 | option_side_effects(telnet, *o, 0); |
374330e2 |
345 | break; |
346 | case INACTIVE: |
347 | case REALLY_INACTIVE: |
348 | break; |
349 | } |
350 | return; |
351 | } |
352 | } |
353 | /* |
354 | * If we reach here, the option was one we weren't prepared to |
355 | * cope with. So send a negative ack. |
356 | */ |
51470298 |
357 | send_opt(telnet, (cmd == WILL ? DONT : WONT), option); |
374330e2 |
358 | } |
359 | |
51470298 |
360 | static void process_subneg(Telnet telnet) |
32874aea |
361 | { |
374330e2 |
362 | unsigned char b[2048], *p, *q; |
363 | int var, value, n; |
364 | char *e; |
365 | |
51470298 |
366 | switch (telnet->sb_opt) { |
374330e2 |
367 | case TELOPT_TSPEED: |
51470298 |
368 | if (telnet->sb_len == 1 && telnet->sb_buf[0] == TELQUAL_SEND) { |
32874aea |
369 | char logbuf[sizeof(cfg.termspeed) + 80]; |
370 | b[0] = IAC; |
371 | b[1] = SB; |
372 | b[2] = TELOPT_TSPEED; |
374330e2 |
373 | b[3] = TELQUAL_IS; |
32874aea |
374 | strcpy(b + 4, cfg.termspeed); |
374330e2 |
375 | n = 4 + strlen(cfg.termspeed); |
32874aea |
376 | b[n] = IAC; |
377 | b[n + 1] = SE; |
51470298 |
378 | telnet->bufsize = sk_write(telnet->s, b, n + 2); |
c5e9c988 |
379 | logevent("server:\tSB TSPEED SEND"); |
374330e2 |
380 | sprintf(logbuf, "client:\tSB TSPEED IS %s", cfg.termspeed); |
32874aea |
381 | logevent(logbuf); |
374330e2 |
382 | } else |
32874aea |
383 | logevent("server:\tSB TSPEED <something weird>"); |
374330e2 |
384 | break; |
385 | case TELOPT_TTYPE: |
51470298 |
386 | if (telnet->sb_len == 1 && telnet->sb_buf[0] == TELQUAL_SEND) { |
32874aea |
387 | char logbuf[sizeof(cfg.termtype) + 80]; |
388 | b[0] = IAC; |
389 | b[1] = SB; |
390 | b[2] = TELOPT_TTYPE; |
374330e2 |
391 | b[3] = TELQUAL_IS; |
392 | for (n = 0; cfg.termtype[n]; n++) |
32874aea |
393 | b[n + 4] = (cfg.termtype[n] >= 'a' |
394 | && cfg.termtype[n] <= |
395 | 'z' ? cfg.termtype[n] + 'A' - |
396 | 'a' : cfg.termtype[n]); |
397 | b[n + 4] = IAC; |
398 | b[n + 5] = SE; |
51470298 |
399 | telnet->bufsize = sk_write(telnet->s, b, n + 6); |
32874aea |
400 | b[n + 4] = 0; |
c5e9c988 |
401 | logevent("server:\tSB TTYPE SEND"); |
32874aea |
402 | sprintf(logbuf, "client:\tSB TTYPE IS %s", b + 4); |
c5e9c988 |
403 | logevent(logbuf); |
374330e2 |
404 | } else |
c5e9c988 |
405 | logevent("server:\tSB TTYPE <something weird>\r\n"); |
374330e2 |
406 | break; |
407 | case TELOPT_OLD_ENVIRON: |
32874aea |
408 | case TELOPT_NEW_ENVIRON: |
51470298 |
409 | p = telnet->sb_buf; |
410 | q = p + telnet->sb_len; |
374330e2 |
411 | if (p < q && *p == TELQUAL_SEND) { |
412 | char logbuf[50]; |
413 | p++; |
51470298 |
414 | sprintf(logbuf, "server:\tSB %s SEND", telopt(telnet->sb_opt)); |
32874aea |
415 | logevent(logbuf); |
51470298 |
416 | if (telnet->sb_opt == TELOPT_OLD_ENVIRON) { |
374330e2 |
417 | if (cfg.rfc_environ) { |
418 | value = RFC_VALUE; |
419 | var = RFC_VAR; |
420 | } else { |
421 | value = BSD_VALUE; |
422 | var = BSD_VAR; |
423 | } |
424 | /* |
425 | * Try to guess the sense of VAR and VALUE. |
426 | */ |
427 | while (p < q) { |
428 | if (*p == RFC_VAR) { |
429 | value = RFC_VALUE; |
430 | var = RFC_VAR; |
431 | } else if (*p == BSD_VAR) { |
432 | value = BSD_VALUE; |
433 | var = BSD_VAR; |
434 | } |
435 | p++; |
436 | } |
437 | } else { |
438 | /* |
439 | * With NEW_ENVIRON, the sense of VAR and VALUE |
440 | * isn't in doubt. |
441 | */ |
442 | value = RFC_VALUE; |
443 | var = RFC_VAR; |
444 | } |
32874aea |
445 | b[0] = IAC; |
446 | b[1] = SB; |
51470298 |
447 | b[2] = telnet->sb_opt; |
374330e2 |
448 | b[3] = TELQUAL_IS; |
449 | n = 4; |
32874aea |
450 | e = cfg.environmt; |
374330e2 |
451 | while (*e) { |
452 | b[n++] = var; |
32874aea |
453 | while (*e && *e != '\t') |
454 | b[n++] = *e++; |
455 | if (*e == '\t') |
456 | e++; |
374330e2 |
457 | b[n++] = value; |
32874aea |
458 | while (*e) |
459 | b[n++] = *e++; |
374330e2 |
460 | e++; |
461 | } |
462 | if (*cfg.username) { |
32874aea |
463 | b[n++] = var; |
464 | b[n++] = 'U'; |
465 | b[n++] = 'S'; |
466 | b[n++] = 'E'; |
467 | b[n++] = 'R'; |
468 | b[n++] = value; |
374330e2 |
469 | e = cfg.username; |
32874aea |
470 | while (*e) |
471 | b[n++] = *e++; |
374330e2 |
472 | } |
32874aea |
473 | b[n++] = IAC; |
474 | b[n++] = SE; |
51470298 |
475 | telnet->bufsize = sk_write(telnet->s, b, n); |
476 | sprintf(logbuf, "client:\tSB %s IS %s", telopt(telnet->sb_opt), |
32874aea |
477 | n == 6 ? "<nothing>" : "<stuff>"); |
478 | logevent(logbuf); |
374330e2 |
479 | } |
480 | break; |
481 | } |
482 | } |
483 | |
51470298 |
484 | static void do_telnet_read(Telnet telnet, char *buf, int len) |
32874aea |
485 | { |
374330e2 |
486 | |
487 | while (len--) { |
488 | int c = (unsigned char) *buf++; |
489 | |
51470298 |
490 | switch (telnet->state) { |
887035a5 |
491 | case TOP_LEVEL: |
374330e2 |
492 | case SEENCR: |
51470298 |
493 | if (c == NUL && telnet->state == SEENCR) |
494 | telnet->state = TOP_LEVEL; |
374330e2 |
495 | else if (c == IAC) |
51470298 |
496 | telnet->state = SEENIAC; |
374330e2 |
497 | else { |
51470298 |
498 | if (!telnet->in_synch) |
499 | c_write1(telnet, c); |
2f938b83 |
500 | |
501 | #if 1 |
502 | /* I can't get the F***ing winsock to insert the urgent IAC |
503 | * into the right position! Even with SO_OOBINLINE it gives |
504 | * it to recv too soon. And of course the DM byte (that |
505 | * arrives in the same packet!) appears several K later!! |
506 | * |
507 | * Oh well, we do get the DM in the right place so I'll |
508 | * just stop hiding on the next 0xf2 and hope for the best. |
509 | */ |
32874aea |
510 | else if (c == DM) |
51470298 |
511 | telnet->in_synch = 0; |
2f938b83 |
512 | #endif |
374330e2 |
513 | if (c == CR) |
51470298 |
514 | telnet->state = SEENCR; |
374330e2 |
515 | else |
51470298 |
516 | telnet->state = TOP_LEVEL; |
374330e2 |
517 | } |
518 | break; |
519 | case SEENIAC: |
32874aea |
520 | if (c == DO) |
51470298 |
521 | telnet->state = SEENDO; |
32874aea |
522 | else if (c == DONT) |
51470298 |
523 | telnet->state = SEENDONT; |
32874aea |
524 | else if (c == WILL) |
51470298 |
525 | telnet->state = SEENWILL; |
32874aea |
526 | else if (c == WONT) |
51470298 |
527 | telnet->state = SEENWONT; |
32874aea |
528 | else if (c == SB) |
51470298 |
529 | telnet->state = SEENSB; |
2f938b83 |
530 | else if (c == DM) { |
51470298 |
531 | telnet->in_synch = 0; |
532 | telnet->state = TOP_LEVEL; |
32874aea |
533 | } else { |
ded38628 |
534 | /* ignore everything else; print it if it's IAC */ |
535 | if (c == IAC) { |
51470298 |
536 | c_write1(telnet, c); |
ded38628 |
537 | } |
51470298 |
538 | telnet->state = TOP_LEVEL; |
300d41b0 |
539 | } |
374330e2 |
540 | break; |
541 | case SEENWILL: |
51470298 |
542 | proc_rec_opt(telnet, WILL, c); |
543 | telnet->state = TOP_LEVEL; |
374330e2 |
544 | break; |
545 | case SEENWONT: |
51470298 |
546 | proc_rec_opt(telnet, WONT, c); |
547 | telnet->state = TOP_LEVEL; |
374330e2 |
548 | break; |
549 | case SEENDO: |
51470298 |
550 | proc_rec_opt(telnet, DO, c); |
551 | telnet->state = TOP_LEVEL; |
374330e2 |
552 | break; |
553 | case SEENDONT: |
51470298 |
554 | proc_rec_opt(telnet, DONT, c); |
555 | telnet->state = TOP_LEVEL; |
374330e2 |
556 | break; |
557 | case SEENSB: |
51470298 |
558 | telnet->sb_opt = c; |
559 | telnet->sb_len = 0; |
560 | telnet->state = SUBNEGOT; |
374330e2 |
561 | break; |
562 | case SUBNEGOT: |
563 | if (c == IAC) |
51470298 |
564 | telnet->state = SUBNEG_IAC; |
374330e2 |
565 | else { |
32874aea |
566 | subneg_addchar: |
51470298 |
567 | if (telnet->sb_len >= telnet->sb_size) { |
374330e2 |
568 | char *newbuf; |
51470298 |
569 | telnet->sb_size += SB_DELTA; |
570 | newbuf = (telnet->sb_buf ? |
571 | srealloc(telnet->sb_buf, telnet->sb_size) : |
572 | smalloc(telnet->sb_size)); |
374330e2 |
573 | if (newbuf) |
51470298 |
574 | telnet->sb_buf = newbuf; |
374330e2 |
575 | else |
51470298 |
576 | telnet->sb_size -= SB_DELTA; |
374330e2 |
577 | } |
51470298 |
578 | if (telnet->sb_len < telnet->sb_size) |
579 | telnet->sb_buf[telnet->sb_len++] = c; |
580 | telnet->state = SUBNEGOT; /* in case we came here by goto */ |
374330e2 |
581 | } |
582 | break; |
583 | case SUBNEG_IAC: |
584 | if (c != SE) |
585 | goto subneg_addchar; /* yes, it's a hack, I know, but... */ |
586 | else { |
51470298 |
587 | process_subneg(telnet); |
588 | telnet->state = TOP_LEVEL; |
374330e2 |
589 | } |
590 | break; |
591 | } |
592 | } |
593 | } |
594 | |
32874aea |
595 | static int telnet_closing(Plug plug, char *error_msg, int error_code, |
596 | int calling_back) |
597 | { |
51470298 |
598 | Telnet telnet = (Telnet) plug; |
599 | |
600 | if (telnet->s) { |
601 | sk_close(telnet->s); |
602 | telnet->s = NULL; |
f3ab576e |
603 | } |
7e78000d |
604 | if (error_msg) { |
32874aea |
605 | /* A socket error has occurred. */ |
247308b5 |
606 | logevent(error_msg); |
607 | connection_fatal("%s", error_msg); |
32874aea |
608 | } /* Otherwise, the remote side closed the connection normally. */ |
7e78000d |
609 | return 0; |
610 | } |
611 | |
32874aea |
612 | static int telnet_receive(Plug plug, int urgent, char *data, int len) |
613 | { |
51470298 |
614 | Telnet telnet = (Telnet) plug; |
32874aea |
615 | if (urgent) |
51470298 |
616 | telnet->in_synch = TRUE; |
617 | do_telnet_read(telnet, data, len); |
8df7a775 |
618 | return 1; |
619 | } |
620 | |
3ad9d396 |
621 | static void telnet_sent(Plug plug, int bufsize) |
622 | { |
51470298 |
623 | Telnet telnet = (Telnet) plug; |
624 | telnet->bufsize = bufsize; |
3ad9d396 |
625 | } |
626 | |
374330e2 |
627 | /* |
8df7a775 |
628 | * Called to set up the Telnet connection. |
374330e2 |
629 | * |
630 | * Returns an error message, or NULL on success. |
631 | * |
6e1ebb76 |
632 | * Also places the canonical host name into `realhost'. It must be |
633 | * freed by the caller. |
374330e2 |
634 | */ |
51470298 |
635 | static char *telnet_init(void *frontend_handle, void **backend_handle, |
887035a5 |
636 | char *host, int port, char **realhost, int nodelay) |
32874aea |
637 | { |
51470298 |
638 | static const struct plug_function_table fn_table = { |
7e78000d |
639 | telnet_closing, |
3ad9d396 |
640 | telnet_receive, |
641 | telnet_sent |
51470298 |
642 | }; |
8df7a775 |
643 | SockAddr addr; |
644 | char *err; |
51470298 |
645 | Telnet telnet; |
646 | |
647 | telnet = smalloc(sizeof(*telnet)); |
648 | telnet->fn = &fn_table; |
649 | telnet->s = NULL; |
650 | telnet->echoing = TRUE; |
651 | telnet->editing = TRUE; |
652 | telnet->activated = FALSE; |
653 | telnet->sb_buf = NULL; |
654 | telnet->sb_size = 0; |
655 | telnet->frontend = frontend_handle; |
656 | telnet->term_width = cfg.width; |
657 | telnet->term_height = cfg.height; |
658 | telnet->state = TOP_LEVEL; |
659 | *backend_handle = telnet; |
887035a5 |
660 | |
374330e2 |
661 | /* |
662 | * Try to find host. |
663 | */ |
3ad9d396 |
664 | { |
665 | char buf[200]; |
666 | sprintf(buf, "Looking up host \"%.170s\"", host); |
667 | logevent(buf); |
668 | } |
8df7a775 |
669 | addr = sk_namelookup(host, realhost); |
32874aea |
670 | if ((err = sk_addr_error(addr))) |
8df7a775 |
671 | return err; |
374330e2 |
672 | |
673 | if (port < 0) |
674 | port = 23; /* default telnet port */ |
675 | |
676 | /* |
677 | * Open socket. |
678 | */ |
3ad9d396 |
679 | { |
680 | char buf[200], addrbuf[100]; |
681 | sk_getaddr(addr, addrbuf, 100); |
682 | sprintf(buf, "Connecting to %.100s port %d", addrbuf, port); |
683 | logevent(buf); |
684 | } |
51470298 |
685 | telnet->s = new_connection(addr, *realhost, port, 0, 1, |
686 | nodelay, (Plug) telnet); |
687 | if ((err = sk_socket_error(telnet->s))) |
8df7a775 |
688 | return err; |
374330e2 |
689 | |
8df7a775 |
690 | sk_addr_free(addr); |
374330e2 |
691 | |
692 | /* |
693 | * Initialise option states. |
694 | */ |
8faa456c |
695 | if (cfg.passive_telnet) { |
51470298 |
696 | const struct Opt *const *o; |
8faa456c |
697 | |
698 | for (o = opts; *o; o++) |
51470298 |
699 | telnet->opt_states[(*o)->index] = INACTIVE; |
8faa456c |
700 | } else { |
51470298 |
701 | const struct Opt *const *o; |
374330e2 |
702 | |
51470298 |
703 | for (o = opts; *o; o++) { |
704 | telnet->opt_states[(*o)->index] = (*o)->initial_state; |
705 | if (telnet->opt_states[(*o)->index] == REQUESTED) |
706 | send_opt(telnet, (*o)->send, (*o)->option); |
707 | } |
708 | telnet->activated = TRUE; |
374330e2 |
709 | } |
710 | |
374330e2 |
711 | /* |
712 | * Set up SYNCH state. |
713 | */ |
51470298 |
714 | telnet->in_synch = FALSE; |
6f34e365 |
715 | |
374330e2 |
716 | return NULL; |
717 | } |
718 | |
719 | /* |
374330e2 |
720 | * Called to send data down the Telnet connection. |
721 | */ |
51470298 |
722 | static int telnet_send(void *handle, char *buf, int len) |
32874aea |
723 | { |
51470298 |
724 | Telnet telnet = (Telnet) handle; |
374330e2 |
725 | char *p; |
726 | static unsigned char iac[2] = { IAC, IAC }; |
727 | static unsigned char cr[2] = { CR, NUL }; |
a5f3e637 |
728 | #if 0 |
eb5e1db9 |
729 | static unsigned char nl[2] = { CR, LF }; |
a5f3e637 |
730 | #endif |
374330e2 |
731 | |
51470298 |
732 | if (telnet->s == NULL) |
5471d09a |
733 | return 0; |
374330e2 |
734 | |
735 | p = buf; |
32874aea |
736 | while (p < buf + len) { |
374330e2 |
737 | char *q = p; |
738 | |
32874aea |
739 | while (iswritable((unsigned char) *p) && p < buf + len) |
740 | p++; |
51470298 |
741 | telnet->bufsize = sk_write(telnet->s, q, p - q); |
374330e2 |
742 | |
32874aea |
743 | while (p < buf + len && !iswritable((unsigned char) *p)) { |
51470298 |
744 | telnet->bufsize = |
745 | sk_write(telnet->s, (unsigned char) *p == IAC ? iac : cr, 2); |
374330e2 |
746 | p++; |
747 | } |
748 | } |
5471d09a |
749 | |
51470298 |
750 | return telnet->bufsize; |
5471d09a |
751 | } |
752 | |
753 | /* |
754 | * Called to query the current socket sendability status. |
755 | */ |
51470298 |
756 | static int telnet_sendbuffer(void *handle) |
5471d09a |
757 | { |
51470298 |
758 | Telnet telnet = (Telnet) handle; |
759 | return telnet->bufsize; |
374330e2 |
760 | } |
761 | |
762 | /* |
763 | * Called to set the size of the window from Telnet's POV. |
764 | */ |
51470298 |
765 | static void telnet_size(void *handle, int width, int height) |
32874aea |
766 | { |
51470298 |
767 | Telnet telnet = (Telnet) handle; |
374330e2 |
768 | unsigned char b[16]; |
769 | char logbuf[50]; |
770 | |
51470298 |
771 | telnet->term_width = width; |
772 | telnet->term_height = height; |
f278d6f8 |
773 | |
51470298 |
774 | if (telnet->s == NULL || telnet->opt_states[o_naws.index] != ACTIVE) |
374330e2 |
775 | return; |
32874aea |
776 | b[0] = IAC; |
777 | b[1] = SB; |
778 | b[2] = TELOPT_NAWS; |
51470298 |
779 | b[3] = telnet->term_width >> 8; |
780 | b[4] = telnet->term_width & 0xFF; |
781 | b[5] = telnet->term_height >> 8; |
782 | b[6] = telnet->term_height & 0xFF; |
32874aea |
783 | b[7] = IAC; |
784 | b[8] = SE; |
51470298 |
785 | telnet->bufsize = sk_write(telnet->s, b, 9); |
374330e2 |
786 | sprintf(logbuf, "client:\tSB NAWS %d,%d", |
32874aea |
787 | ((unsigned char) b[3] << 8) + (unsigned char) b[4], |
788 | ((unsigned char) b[5] << 8) + (unsigned char) b[6]); |
789 | logevent(logbuf); |
374330e2 |
790 | } |
791 | |
792 | /* |
793 | * Send Telnet special codes. |
794 | */ |
51470298 |
795 | static void telnet_special(void *handle, Telnet_Special code) |
32874aea |
796 | { |
51470298 |
797 | Telnet telnet = (Telnet) handle; |
374330e2 |
798 | unsigned char b[2]; |
799 | |
51470298 |
800 | if (telnet->s == NULL) |
374330e2 |
801 | return; |
802 | |
803 | b[0] = IAC; |
804 | switch (code) { |
32874aea |
805 | case TS_AYT: |
806 | b[1] = AYT; |
51470298 |
807 | telnet->bufsize = sk_write(telnet->s, b, 2); |
32874aea |
808 | break; |
809 | case TS_BRK: |
810 | b[1] = BREAK; |
51470298 |
811 | telnet->bufsize = sk_write(telnet->s, b, 2); |
32874aea |
812 | break; |
813 | case TS_EC: |
814 | b[1] = EC; |
51470298 |
815 | telnet->bufsize = sk_write(telnet->s, b, 2); |
32874aea |
816 | break; |
817 | case TS_EL: |
818 | b[1] = EL; |
51470298 |
819 | telnet->bufsize = sk_write(telnet->s, b, 2); |
32874aea |
820 | break; |
821 | case TS_GA: |
822 | b[1] = GA; |
51470298 |
823 | telnet->bufsize = sk_write(telnet->s, b, 2); |
32874aea |
824 | break; |
825 | case TS_NOP: |
826 | b[1] = NOP; |
51470298 |
827 | telnet->bufsize = sk_write(telnet->s, b, 2); |
32874aea |
828 | break; |
829 | case TS_ABORT: |
830 | b[1] = ABORT; |
51470298 |
831 | telnet->bufsize = sk_write(telnet->s, b, 2); |
32874aea |
832 | break; |
833 | case TS_AO: |
834 | b[1] = AO; |
51470298 |
835 | telnet->bufsize = sk_write(telnet->s, b, 2); |
32874aea |
836 | break; |
837 | case TS_IP: |
838 | b[1] = IP; |
51470298 |
839 | telnet->bufsize = sk_write(telnet->s, b, 2); |
32874aea |
840 | break; |
841 | case TS_SUSP: |
842 | b[1] = SUSP; |
51470298 |
843 | telnet->bufsize = sk_write(telnet->s, b, 2); |
32874aea |
844 | break; |
845 | case TS_EOR: |
846 | b[1] = EOR; |
51470298 |
847 | telnet->bufsize = sk_write(telnet->s, b, 2); |
32874aea |
848 | break; |
849 | case TS_EOF: |
850 | b[1] = xEOF; |
51470298 |
851 | telnet->bufsize = sk_write(telnet->s, b, 2); |
32874aea |
852 | break; |
a5f3e637 |
853 | case TS_EOL: |
51470298 |
854 | telnet->bufsize = sk_write(telnet->s, "\r\n", 2); |
a5f3e637 |
855 | break; |
374330e2 |
856 | case TS_SYNCH: |
32874aea |
857 | b[1] = DM; |
51470298 |
858 | telnet->bufsize = sk_write(telnet->s, b, 1); |
859 | telnet->bufsize = sk_write_oob(telnet->s, b + 1, 1); |
32874aea |
860 | break; |
684d367c |
861 | case TS_RECHO: |
51470298 |
862 | if (telnet->opt_states[o_echo.index] == INACTIVE || |
863 | telnet->opt_states[o_echo.index] == REALLY_INACTIVE) { |
864 | telnet->opt_states[o_echo.index] = REQUESTED; |
865 | send_opt(telnet, o_echo.send, o_echo.option); |
684d367c |
866 | } |
867 | break; |
868 | case TS_LECHO: |
51470298 |
869 | if (telnet->opt_states[o_echo.index] == ACTIVE) { |
870 | telnet->opt_states[o_echo.index] = REQUESTED; |
871 | send_opt(telnet, o_echo.nsend, o_echo.option); |
684d367c |
872 | } |
374330e2 |
873 | break; |
32874aea |
874 | case TS_PING: |
51470298 |
875 | if (telnet->opt_states[o_they_sga.index] == ACTIVE) { |
32874aea |
876 | b[1] = NOP; |
51470298 |
877 | telnet->bufsize = sk_write(telnet->s, b, 2); |
ec55b220 |
878 | } |
32874aea |
879 | break; |
374330e2 |
880 | } |
881 | } |
882 | |
51470298 |
883 | static Socket telnet_socket(void *handle) |
32874aea |
884 | { |
51470298 |
885 | Telnet telnet = (Telnet) handle; |
886 | return telnet->s; |
32874aea |
887 | } |
8ccc75b0 |
888 | |
51470298 |
889 | static int telnet_sendok(void *handle) |
32874aea |
890 | { |
51470298 |
891 | Telnet telnet = (Telnet) handle; |
32874aea |
892 | return 1; |
893 | } |
4017be6d |
894 | |
51470298 |
895 | static void telnet_unthrottle(void *handle, int backlog) |
5471d09a |
896 | { |
51470298 |
897 | Telnet telnet = (Telnet) handle; |
898 | sk_set_frozen(telnet->s, backlog > TELNET_MAX_BACKLOG); |
5471d09a |
899 | } |
900 | |
51470298 |
901 | static int telnet_ldisc(void *handle, int option) |
32874aea |
902 | { |
51470298 |
903 | Telnet telnet = (Telnet) handle; |
32874aea |
904 | if (option == LD_ECHO) |
51470298 |
905 | return telnet->echoing; |
32874aea |
906 | if (option == LD_EDIT) |
51470298 |
907 | return telnet->editing; |
0965bee0 |
908 | return FALSE; |
909 | } |
910 | |
51470298 |
911 | static int telnet_exitcode(void *handle) |
d8d6c7e5 |
912 | { |
51470298 |
913 | Telnet telnet = (Telnet) handle; |
d8d6c7e5 |
914 | /* Telnet doesn't transmit exit codes back to the client */ |
915 | return 0; |
916 | } |
917 | |
374330e2 |
918 | Backend telnet_backend = { |
919 | telnet_init, |
374330e2 |
920 | telnet_send, |
5471d09a |
921 | telnet_sendbuffer, |
374330e2 |
922 | telnet_size, |
4017be6d |
923 | telnet_special, |
8ccc75b0 |
924 | telnet_socket, |
d8d6c7e5 |
925 | telnet_exitcode, |
97db3be4 |
926 | telnet_sendok, |
0965bee0 |
927 | telnet_ldisc, |
5471d09a |
928 | telnet_unthrottle, |
97db3be4 |
929 | 23 |
374330e2 |
930 | }; |