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; |
b9d7bcad |
195 | void *ldisc; |
51470298 |
196 | int term_width, term_height; |
197 | |
198 | int opt_states[NUM_OPTS]; |
199 | |
200 | int echoing, editing; |
201 | int activated; |
202 | int bufsize; |
203 | int in_synch; |
204 | int sb_opt, sb_len; |
205 | char *sb_buf; |
206 | int sb_size; |
207 | |
208 | enum { |
209 | TOP_LEVEL, SEENIAC, SEENWILL, SEENWONT, SEENDO, SEENDONT, |
210 | SEENSB, SUBNEGOT, SUBNEG_IAC, SEENCR |
211 | } state; |
212 | |
213 | } *Telnet; |
214 | |
5471d09a |
215 | #define TELNET_MAX_BACKLOG 4096 |
216 | |
374330e2 |
217 | #define SB_DELTA 1024 |
218 | |
51470298 |
219 | static void c_write1(Telnet telnet, int c) |
32874aea |
220 | { |
5471d09a |
221 | int backlog; |
32874aea |
222 | char cc = (char) c; |
51470298 |
223 | backlog = from_backend(telnet->frontend, 0, &cc, 1); |
224 | sk_set_frozen(telnet->s, backlog > TELNET_MAX_BACKLOG); |
fe50e814 |
225 | } |
226 | |
a8327734 |
227 | static void log_option(Telnet telnet, char *sender, int cmd, int option) |
32874aea |
228 | { |
374330e2 |
229 | char buf[50]; |
230 | sprintf(buf, "%s:\t%s %s", sender, |
231 | (cmd == WILL ? "WILL" : cmd == WONT ? "WONT" : |
232 | cmd == DO ? "DO" : cmd == DONT ? "DONT" : "<??>"), |
233 | telopt(option)); |
a8327734 |
234 | logevent(telnet->frontend, buf); |
374330e2 |
235 | } |
236 | |
51470298 |
237 | static void send_opt(Telnet telnet, int cmd, int option) |
32874aea |
238 | { |
374330e2 |
239 | unsigned char b[3]; |
240 | |
32874aea |
241 | b[0] = IAC; |
242 | b[1] = cmd; |
243 | b[2] = option; |
51470298 |
244 | telnet->bufsize = sk_write(telnet->s, b, 3); |
a8327734 |
245 | log_option(telnet, "client", cmd, option); |
374330e2 |
246 | } |
247 | |
51470298 |
248 | static void deactivate_option(Telnet telnet, const struct Opt *o) |
32874aea |
249 | { |
51470298 |
250 | if (telnet->opt_states[o->index] == REQUESTED || |
251 | telnet->opt_states[o->index] == ACTIVE) |
252 | send_opt(telnet, o->nsend, o->option); |
253 | telnet->opt_states[o->index] = REALLY_INACTIVE; |
374330e2 |
254 | } |
255 | |
708bbbbe |
256 | /* |
257 | * Generate side effects of enabling or disabling an option. |
258 | */ |
51470298 |
259 | static void option_side_effects(Telnet telnet, const struct Opt *o, int enabled) |
32874aea |
260 | { |
0965bee0 |
261 | if (o->option == TELOPT_ECHO && o->send == DO) |
51470298 |
262 | telnet->echoing = !enabled; |
b6c680d4 |
263 | else if (o->option == TELOPT_SGA && o->send == DO) |
51470298 |
264 | telnet->editing = !enabled; |
b9d7bcad |
265 | if (telnet->ldisc) /* cause ldisc to notice the change */ |
266 | ldisc_send(telnet->ldisc, NULL, 0, 0); |
8faa456c |
267 | |
268 | /* Ensure we get the minimum options */ |
51470298 |
269 | if (!telnet->activated) { |
270 | if (telnet->opt_states[o_echo.index] == INACTIVE) { |
271 | telnet->opt_states[o_echo.index] = REQUESTED; |
272 | send_opt(telnet, o_echo.send, o_echo.option); |
8faa456c |
273 | } |
51470298 |
274 | if (telnet->opt_states[o_we_sga.index] == INACTIVE) { |
275 | telnet->opt_states[o_we_sga.index] = REQUESTED; |
276 | send_opt(telnet, o_we_sga.send, o_we_sga.option); |
8faa456c |
277 | } |
51470298 |
278 | if (telnet->opt_states[o_they_sga.index] == INACTIVE) { |
279 | telnet->opt_states[o_they_sga.index] = REQUESTED; |
280 | send_opt(telnet, o_they_sga.send, o_they_sga.option); |
8faa456c |
281 | } |
51470298 |
282 | telnet->activated = TRUE; |
8faa456c |
283 | } |
708bbbbe |
284 | } |
285 | |
51470298 |
286 | static void activate_option(Telnet telnet, const struct Opt *o) |
32874aea |
287 | { |
374330e2 |
288 | if (o->send == WILL && o->option == TELOPT_NAWS) |
51470298 |
289 | telnet_size(telnet, telnet->term_width, telnet->term_height); |
374330e2 |
290 | if (o->send == WILL && |
291 | (o->option == TELOPT_NEW_ENVIRON || |
292 | o->option == TELOPT_OLD_ENVIRON)) { |
293 | /* |
294 | * We may only have one kind of ENVIRON going at a time. |
295 | * This is a hack, but who cares. |
296 | */ |
51470298 |
297 | deactivate_option(telnet, o->option == |
32874aea |
298 | TELOPT_NEW_ENVIRON ? &o_oenv : &o_nenv); |
374330e2 |
299 | } |
51470298 |
300 | option_side_effects(telnet, o, 1); |
374330e2 |
301 | } |
302 | |
51470298 |
303 | static void refused_option(Telnet telnet, const struct Opt *o) |
32874aea |
304 | { |
374330e2 |
305 | if (o->send == WILL && o->option == TELOPT_NEW_ENVIRON && |
51470298 |
306 | telnet->opt_states[o_oenv.index] == INACTIVE) { |
307 | send_opt(telnet, WILL, TELOPT_OLD_ENVIRON); |
308 | telnet->opt_states[o_oenv.index] = REQUESTED; |
374330e2 |
309 | } |
51470298 |
310 | option_side_effects(telnet, o, 0); |
374330e2 |
311 | } |
312 | |
51470298 |
313 | static void proc_rec_opt(Telnet telnet, int cmd, int option) |
32874aea |
314 | { |
51470298 |
315 | const struct Opt *const *o; |
374330e2 |
316 | |
a8327734 |
317 | log_option(telnet, "server", cmd, option); |
374330e2 |
318 | for (o = opts; *o; o++) { |
319 | if ((*o)->option == option && (*o)->ack == cmd) { |
51470298 |
320 | switch (telnet->opt_states[(*o)->index]) { |
374330e2 |
321 | case REQUESTED: |
51470298 |
322 | telnet->opt_states[(*o)->index] = ACTIVE; |
323 | activate_option(telnet, *o); |
374330e2 |
324 | break; |
325 | case ACTIVE: |
326 | break; |
327 | case INACTIVE: |
51470298 |
328 | telnet->opt_states[(*o)->index] = ACTIVE; |
329 | send_opt(telnet, (*o)->send, option); |
330 | activate_option(telnet, *o); |
374330e2 |
331 | break; |
332 | case REALLY_INACTIVE: |
51470298 |
333 | send_opt(telnet, (*o)->nsend, option); |
374330e2 |
334 | break; |
335 | } |
336 | return; |
337 | } else if ((*o)->option == option && (*o)->nak == cmd) { |
51470298 |
338 | switch (telnet->opt_states[(*o)->index]) { |
374330e2 |
339 | case REQUESTED: |
51470298 |
340 | telnet->opt_states[(*o)->index] = INACTIVE; |
341 | refused_option(telnet, *o); |
374330e2 |
342 | break; |
343 | case ACTIVE: |
51470298 |
344 | telnet->opt_states[(*o)->index] = INACTIVE; |
345 | send_opt(telnet, (*o)->nsend, option); |
346 | option_side_effects(telnet, *o, 0); |
374330e2 |
347 | break; |
348 | case INACTIVE: |
349 | case REALLY_INACTIVE: |
350 | break; |
351 | } |
352 | return; |
353 | } |
354 | } |
355 | /* |
356 | * If we reach here, the option was one we weren't prepared to |
357 | * cope with. So send a negative ack. |
358 | */ |
51470298 |
359 | send_opt(telnet, (cmd == WILL ? DONT : WONT), option); |
374330e2 |
360 | } |
361 | |
51470298 |
362 | static void process_subneg(Telnet telnet) |
32874aea |
363 | { |
374330e2 |
364 | unsigned char b[2048], *p, *q; |
365 | int var, value, n; |
366 | char *e; |
367 | |
51470298 |
368 | switch (telnet->sb_opt) { |
374330e2 |
369 | case TELOPT_TSPEED: |
51470298 |
370 | if (telnet->sb_len == 1 && telnet->sb_buf[0] == TELQUAL_SEND) { |
32874aea |
371 | char logbuf[sizeof(cfg.termspeed) + 80]; |
372 | b[0] = IAC; |
373 | b[1] = SB; |
374 | b[2] = TELOPT_TSPEED; |
374330e2 |
375 | b[3] = TELQUAL_IS; |
32874aea |
376 | strcpy(b + 4, cfg.termspeed); |
374330e2 |
377 | n = 4 + strlen(cfg.termspeed); |
32874aea |
378 | b[n] = IAC; |
379 | b[n + 1] = SE; |
51470298 |
380 | telnet->bufsize = sk_write(telnet->s, b, n + 2); |
a8327734 |
381 | logevent(telnet->frontend, "server:\tSB TSPEED SEND"); |
374330e2 |
382 | sprintf(logbuf, "client:\tSB TSPEED IS %s", cfg.termspeed); |
a8327734 |
383 | logevent(telnet->frontend, logbuf); |
374330e2 |
384 | } else |
a8327734 |
385 | logevent(telnet->frontend, "server:\tSB TSPEED <something weird>"); |
374330e2 |
386 | break; |
387 | case TELOPT_TTYPE: |
51470298 |
388 | if (telnet->sb_len == 1 && telnet->sb_buf[0] == TELQUAL_SEND) { |
32874aea |
389 | char logbuf[sizeof(cfg.termtype) + 80]; |
390 | b[0] = IAC; |
391 | b[1] = SB; |
392 | b[2] = TELOPT_TTYPE; |
374330e2 |
393 | b[3] = TELQUAL_IS; |
394 | for (n = 0; cfg.termtype[n]; n++) |
32874aea |
395 | b[n + 4] = (cfg.termtype[n] >= 'a' |
396 | && cfg.termtype[n] <= |
397 | 'z' ? cfg.termtype[n] + 'A' - |
398 | 'a' : cfg.termtype[n]); |
399 | b[n + 4] = IAC; |
400 | b[n + 5] = SE; |
51470298 |
401 | telnet->bufsize = sk_write(telnet->s, b, n + 6); |
32874aea |
402 | b[n + 4] = 0; |
a8327734 |
403 | logevent(telnet->frontend, "server:\tSB TTYPE SEND"); |
32874aea |
404 | sprintf(logbuf, "client:\tSB TTYPE IS %s", b + 4); |
a8327734 |
405 | logevent(telnet->frontend, logbuf); |
374330e2 |
406 | } else |
a8327734 |
407 | logevent(telnet->frontend, "server:\tSB TTYPE <something weird>\r\n"); |
374330e2 |
408 | break; |
409 | case TELOPT_OLD_ENVIRON: |
32874aea |
410 | case TELOPT_NEW_ENVIRON: |
51470298 |
411 | p = telnet->sb_buf; |
412 | q = p + telnet->sb_len; |
374330e2 |
413 | if (p < q && *p == TELQUAL_SEND) { |
414 | char logbuf[50]; |
415 | p++; |
51470298 |
416 | sprintf(logbuf, "server:\tSB %s SEND", telopt(telnet->sb_opt)); |
a8327734 |
417 | logevent(telnet->frontend, logbuf); |
51470298 |
418 | if (telnet->sb_opt == TELOPT_OLD_ENVIRON) { |
374330e2 |
419 | if (cfg.rfc_environ) { |
420 | value = RFC_VALUE; |
421 | var = RFC_VAR; |
422 | } else { |
423 | value = BSD_VALUE; |
424 | var = BSD_VAR; |
425 | } |
426 | /* |
427 | * Try to guess the sense of VAR and VALUE. |
428 | */ |
429 | while (p < q) { |
430 | if (*p == RFC_VAR) { |
431 | value = RFC_VALUE; |
432 | var = RFC_VAR; |
433 | } else if (*p == BSD_VAR) { |
434 | value = BSD_VALUE; |
435 | var = BSD_VAR; |
436 | } |
437 | p++; |
438 | } |
439 | } else { |
440 | /* |
441 | * With NEW_ENVIRON, the sense of VAR and VALUE |
442 | * isn't in doubt. |
443 | */ |
444 | value = RFC_VALUE; |
445 | var = RFC_VAR; |
446 | } |
32874aea |
447 | b[0] = IAC; |
448 | b[1] = SB; |
51470298 |
449 | b[2] = telnet->sb_opt; |
374330e2 |
450 | b[3] = TELQUAL_IS; |
451 | n = 4; |
32874aea |
452 | e = cfg.environmt; |
374330e2 |
453 | while (*e) { |
454 | b[n++] = var; |
32874aea |
455 | while (*e && *e != '\t') |
456 | b[n++] = *e++; |
457 | if (*e == '\t') |
458 | e++; |
374330e2 |
459 | b[n++] = value; |
32874aea |
460 | while (*e) |
461 | b[n++] = *e++; |
374330e2 |
462 | e++; |
463 | } |
464 | if (*cfg.username) { |
32874aea |
465 | b[n++] = var; |
466 | b[n++] = 'U'; |
467 | b[n++] = 'S'; |
468 | b[n++] = 'E'; |
469 | b[n++] = 'R'; |
470 | b[n++] = value; |
374330e2 |
471 | e = cfg.username; |
32874aea |
472 | while (*e) |
473 | b[n++] = *e++; |
374330e2 |
474 | } |
32874aea |
475 | b[n++] = IAC; |
476 | b[n++] = SE; |
51470298 |
477 | telnet->bufsize = sk_write(telnet->s, b, n); |
478 | sprintf(logbuf, "client:\tSB %s IS %s", telopt(telnet->sb_opt), |
32874aea |
479 | n == 6 ? "<nothing>" : "<stuff>"); |
a8327734 |
480 | logevent(telnet->frontend, logbuf); |
374330e2 |
481 | } |
482 | break; |
483 | } |
484 | } |
485 | |
51470298 |
486 | static void do_telnet_read(Telnet telnet, char *buf, int len) |
32874aea |
487 | { |
374330e2 |
488 | |
489 | while (len--) { |
490 | int c = (unsigned char) *buf++; |
491 | |
51470298 |
492 | switch (telnet->state) { |
887035a5 |
493 | case TOP_LEVEL: |
374330e2 |
494 | case SEENCR: |
51470298 |
495 | if (c == NUL && telnet->state == SEENCR) |
496 | telnet->state = TOP_LEVEL; |
374330e2 |
497 | else if (c == IAC) |
51470298 |
498 | telnet->state = SEENIAC; |
374330e2 |
499 | else { |
51470298 |
500 | if (!telnet->in_synch) |
501 | c_write1(telnet, c); |
2f938b83 |
502 | |
503 | #if 1 |
504 | /* I can't get the F***ing winsock to insert the urgent IAC |
505 | * into the right position! Even with SO_OOBINLINE it gives |
506 | * it to recv too soon. And of course the DM byte (that |
507 | * arrives in the same packet!) appears several K later!! |
508 | * |
509 | * Oh well, we do get the DM in the right place so I'll |
510 | * just stop hiding on the next 0xf2 and hope for the best. |
511 | */ |
32874aea |
512 | else if (c == DM) |
51470298 |
513 | telnet->in_synch = 0; |
2f938b83 |
514 | #endif |
374330e2 |
515 | if (c == CR) |
51470298 |
516 | telnet->state = SEENCR; |
374330e2 |
517 | else |
51470298 |
518 | telnet->state = TOP_LEVEL; |
374330e2 |
519 | } |
520 | break; |
521 | case SEENIAC: |
32874aea |
522 | if (c == DO) |
51470298 |
523 | telnet->state = SEENDO; |
32874aea |
524 | else if (c == DONT) |
51470298 |
525 | telnet->state = SEENDONT; |
32874aea |
526 | else if (c == WILL) |
51470298 |
527 | telnet->state = SEENWILL; |
32874aea |
528 | else if (c == WONT) |
51470298 |
529 | telnet->state = SEENWONT; |
32874aea |
530 | else if (c == SB) |
51470298 |
531 | telnet->state = SEENSB; |
2f938b83 |
532 | else if (c == DM) { |
51470298 |
533 | telnet->in_synch = 0; |
534 | telnet->state = TOP_LEVEL; |
32874aea |
535 | } else { |
ded38628 |
536 | /* ignore everything else; print it if it's IAC */ |
537 | if (c == IAC) { |
51470298 |
538 | c_write1(telnet, c); |
ded38628 |
539 | } |
51470298 |
540 | telnet->state = TOP_LEVEL; |
300d41b0 |
541 | } |
374330e2 |
542 | break; |
543 | case SEENWILL: |
51470298 |
544 | proc_rec_opt(telnet, WILL, c); |
545 | telnet->state = TOP_LEVEL; |
374330e2 |
546 | break; |
547 | case SEENWONT: |
51470298 |
548 | proc_rec_opt(telnet, WONT, c); |
549 | telnet->state = TOP_LEVEL; |
374330e2 |
550 | break; |
551 | case SEENDO: |
51470298 |
552 | proc_rec_opt(telnet, DO, c); |
553 | telnet->state = TOP_LEVEL; |
374330e2 |
554 | break; |
555 | case SEENDONT: |
51470298 |
556 | proc_rec_opt(telnet, DONT, c); |
557 | telnet->state = TOP_LEVEL; |
374330e2 |
558 | break; |
559 | case SEENSB: |
51470298 |
560 | telnet->sb_opt = c; |
561 | telnet->sb_len = 0; |
562 | telnet->state = SUBNEGOT; |
374330e2 |
563 | break; |
564 | case SUBNEGOT: |
565 | if (c == IAC) |
51470298 |
566 | telnet->state = SUBNEG_IAC; |
374330e2 |
567 | else { |
32874aea |
568 | subneg_addchar: |
51470298 |
569 | if (telnet->sb_len >= telnet->sb_size) { |
374330e2 |
570 | char *newbuf; |
51470298 |
571 | telnet->sb_size += SB_DELTA; |
572 | newbuf = (telnet->sb_buf ? |
573 | srealloc(telnet->sb_buf, telnet->sb_size) : |
574 | smalloc(telnet->sb_size)); |
374330e2 |
575 | if (newbuf) |
51470298 |
576 | telnet->sb_buf = newbuf; |
374330e2 |
577 | else |
51470298 |
578 | telnet->sb_size -= SB_DELTA; |
374330e2 |
579 | } |
51470298 |
580 | if (telnet->sb_len < telnet->sb_size) |
581 | telnet->sb_buf[telnet->sb_len++] = c; |
582 | telnet->state = SUBNEGOT; /* in case we came here by goto */ |
374330e2 |
583 | } |
584 | break; |
585 | case SUBNEG_IAC: |
586 | if (c != SE) |
587 | goto subneg_addchar; /* yes, it's a hack, I know, but... */ |
588 | else { |
51470298 |
589 | process_subneg(telnet); |
590 | telnet->state = TOP_LEVEL; |
374330e2 |
591 | } |
592 | break; |
593 | } |
594 | } |
595 | } |
596 | |
32874aea |
597 | static int telnet_closing(Plug plug, char *error_msg, int error_code, |
598 | int calling_back) |
599 | { |
51470298 |
600 | Telnet telnet = (Telnet) plug; |
601 | |
602 | if (telnet->s) { |
603 | sk_close(telnet->s); |
604 | telnet->s = NULL; |
f3ab576e |
605 | } |
7e78000d |
606 | if (error_msg) { |
32874aea |
607 | /* A socket error has occurred. */ |
a8327734 |
608 | logevent(telnet->frontend, error_msg); |
247308b5 |
609 | connection_fatal("%s", error_msg); |
32874aea |
610 | } /* Otherwise, the remote side closed the connection normally. */ |
7e78000d |
611 | return 0; |
612 | } |
613 | |
32874aea |
614 | static int telnet_receive(Plug plug, int urgent, char *data, int len) |
615 | { |
51470298 |
616 | Telnet telnet = (Telnet) plug; |
32874aea |
617 | if (urgent) |
51470298 |
618 | telnet->in_synch = TRUE; |
619 | do_telnet_read(telnet, data, len); |
8df7a775 |
620 | return 1; |
621 | } |
622 | |
3ad9d396 |
623 | static void telnet_sent(Plug plug, int bufsize) |
624 | { |
51470298 |
625 | Telnet telnet = (Telnet) plug; |
626 | telnet->bufsize = bufsize; |
3ad9d396 |
627 | } |
628 | |
374330e2 |
629 | /* |
8df7a775 |
630 | * Called to set up the Telnet connection. |
374330e2 |
631 | * |
632 | * Returns an error message, or NULL on success. |
633 | * |
6e1ebb76 |
634 | * Also places the canonical host name into `realhost'. It must be |
635 | * freed by the caller. |
374330e2 |
636 | */ |
51470298 |
637 | static char *telnet_init(void *frontend_handle, void **backend_handle, |
887035a5 |
638 | char *host, int port, char **realhost, int nodelay) |
32874aea |
639 | { |
51470298 |
640 | static const struct plug_function_table fn_table = { |
7e78000d |
641 | telnet_closing, |
3ad9d396 |
642 | telnet_receive, |
643 | telnet_sent |
51470298 |
644 | }; |
8df7a775 |
645 | SockAddr addr; |
646 | char *err; |
51470298 |
647 | Telnet telnet; |
648 | |
649 | telnet = smalloc(sizeof(*telnet)); |
650 | telnet->fn = &fn_table; |
651 | telnet->s = NULL; |
652 | telnet->echoing = TRUE; |
653 | telnet->editing = TRUE; |
654 | telnet->activated = FALSE; |
655 | telnet->sb_buf = NULL; |
656 | telnet->sb_size = 0; |
657 | telnet->frontend = frontend_handle; |
658 | telnet->term_width = cfg.width; |
659 | telnet->term_height = cfg.height; |
660 | telnet->state = TOP_LEVEL; |
661 | *backend_handle = telnet; |
887035a5 |
662 | |
374330e2 |
663 | /* |
664 | * Try to find host. |
665 | */ |
3ad9d396 |
666 | { |
667 | char buf[200]; |
668 | sprintf(buf, "Looking up host \"%.170s\"", host); |
a8327734 |
669 | logevent(telnet->frontend, buf); |
3ad9d396 |
670 | } |
8df7a775 |
671 | addr = sk_namelookup(host, realhost); |
32874aea |
672 | if ((err = sk_addr_error(addr))) |
8df7a775 |
673 | return err; |
374330e2 |
674 | |
675 | if (port < 0) |
676 | port = 23; /* default telnet port */ |
677 | |
678 | /* |
679 | * Open socket. |
680 | */ |
3ad9d396 |
681 | { |
682 | char buf[200], addrbuf[100]; |
683 | sk_getaddr(addr, addrbuf, 100); |
684 | sprintf(buf, "Connecting to %.100s port %d", addrbuf, port); |
a8327734 |
685 | logevent(telnet->frontend, buf); |
3ad9d396 |
686 | } |
51470298 |
687 | telnet->s = new_connection(addr, *realhost, port, 0, 1, |
688 | nodelay, (Plug) telnet); |
689 | if ((err = sk_socket_error(telnet->s))) |
8df7a775 |
690 | return err; |
374330e2 |
691 | |
8df7a775 |
692 | sk_addr_free(addr); |
374330e2 |
693 | |
694 | /* |
695 | * Initialise option states. |
696 | */ |
8faa456c |
697 | if (cfg.passive_telnet) { |
51470298 |
698 | const struct Opt *const *o; |
8faa456c |
699 | |
700 | for (o = opts; *o; o++) |
51470298 |
701 | telnet->opt_states[(*o)->index] = INACTIVE; |
8faa456c |
702 | } else { |
51470298 |
703 | const struct Opt *const *o; |
374330e2 |
704 | |
51470298 |
705 | for (o = opts; *o; o++) { |
706 | telnet->opt_states[(*o)->index] = (*o)->initial_state; |
707 | if (telnet->opt_states[(*o)->index] == REQUESTED) |
708 | send_opt(telnet, (*o)->send, (*o)->option); |
709 | } |
710 | telnet->activated = TRUE; |
374330e2 |
711 | } |
712 | |
374330e2 |
713 | /* |
714 | * Set up SYNCH state. |
715 | */ |
51470298 |
716 | telnet->in_synch = FALSE; |
6f34e365 |
717 | |
374330e2 |
718 | return NULL; |
719 | } |
720 | |
721 | /* |
374330e2 |
722 | * Called to send data down the Telnet connection. |
723 | */ |
51470298 |
724 | static int telnet_send(void *handle, char *buf, int len) |
32874aea |
725 | { |
51470298 |
726 | Telnet telnet = (Telnet) handle; |
374330e2 |
727 | char *p; |
728 | static unsigned char iac[2] = { IAC, IAC }; |
729 | static unsigned char cr[2] = { CR, NUL }; |
a5f3e637 |
730 | #if 0 |
eb5e1db9 |
731 | static unsigned char nl[2] = { CR, LF }; |
a5f3e637 |
732 | #endif |
374330e2 |
733 | |
51470298 |
734 | if (telnet->s == NULL) |
5471d09a |
735 | return 0; |
374330e2 |
736 | |
737 | p = buf; |
32874aea |
738 | while (p < buf + len) { |
374330e2 |
739 | char *q = p; |
740 | |
32874aea |
741 | while (iswritable((unsigned char) *p) && p < buf + len) |
742 | p++; |
51470298 |
743 | telnet->bufsize = sk_write(telnet->s, q, p - q); |
374330e2 |
744 | |
32874aea |
745 | while (p < buf + len && !iswritable((unsigned char) *p)) { |
51470298 |
746 | telnet->bufsize = |
747 | sk_write(telnet->s, (unsigned char) *p == IAC ? iac : cr, 2); |
374330e2 |
748 | p++; |
749 | } |
750 | } |
5471d09a |
751 | |
51470298 |
752 | return telnet->bufsize; |
5471d09a |
753 | } |
754 | |
755 | /* |
756 | * Called to query the current socket sendability status. |
757 | */ |
51470298 |
758 | static int telnet_sendbuffer(void *handle) |
5471d09a |
759 | { |
51470298 |
760 | Telnet telnet = (Telnet) handle; |
761 | return telnet->bufsize; |
374330e2 |
762 | } |
763 | |
764 | /* |
765 | * Called to set the size of the window from Telnet's POV. |
766 | */ |
51470298 |
767 | static void telnet_size(void *handle, int width, int height) |
32874aea |
768 | { |
51470298 |
769 | Telnet telnet = (Telnet) handle; |
374330e2 |
770 | unsigned char b[16]; |
771 | char logbuf[50]; |
772 | |
51470298 |
773 | telnet->term_width = width; |
774 | telnet->term_height = height; |
f278d6f8 |
775 | |
51470298 |
776 | if (telnet->s == NULL || telnet->opt_states[o_naws.index] != ACTIVE) |
374330e2 |
777 | return; |
32874aea |
778 | b[0] = IAC; |
779 | b[1] = SB; |
780 | b[2] = TELOPT_NAWS; |
51470298 |
781 | b[3] = telnet->term_width >> 8; |
782 | b[4] = telnet->term_width & 0xFF; |
783 | b[5] = telnet->term_height >> 8; |
784 | b[6] = telnet->term_height & 0xFF; |
32874aea |
785 | b[7] = IAC; |
786 | b[8] = SE; |
51470298 |
787 | telnet->bufsize = sk_write(telnet->s, b, 9); |
374330e2 |
788 | sprintf(logbuf, "client:\tSB NAWS %d,%d", |
32874aea |
789 | ((unsigned char) b[3] << 8) + (unsigned char) b[4], |
790 | ((unsigned char) b[5] << 8) + (unsigned char) b[6]); |
a8327734 |
791 | logevent(telnet->frontend, logbuf); |
374330e2 |
792 | } |
793 | |
794 | /* |
795 | * Send Telnet special codes. |
796 | */ |
51470298 |
797 | static void telnet_special(void *handle, Telnet_Special code) |
32874aea |
798 | { |
51470298 |
799 | Telnet telnet = (Telnet) handle; |
374330e2 |
800 | unsigned char b[2]; |
801 | |
51470298 |
802 | if (telnet->s == NULL) |
374330e2 |
803 | return; |
804 | |
805 | b[0] = IAC; |
806 | switch (code) { |
32874aea |
807 | case TS_AYT: |
808 | b[1] = AYT; |
51470298 |
809 | telnet->bufsize = sk_write(telnet->s, b, 2); |
32874aea |
810 | break; |
811 | case TS_BRK: |
812 | b[1] = BREAK; |
51470298 |
813 | telnet->bufsize = sk_write(telnet->s, b, 2); |
32874aea |
814 | break; |
815 | case TS_EC: |
816 | b[1] = EC; |
51470298 |
817 | telnet->bufsize = sk_write(telnet->s, b, 2); |
32874aea |
818 | break; |
819 | case TS_EL: |
820 | b[1] = EL; |
51470298 |
821 | telnet->bufsize = sk_write(telnet->s, b, 2); |
32874aea |
822 | break; |
823 | case TS_GA: |
824 | b[1] = GA; |
51470298 |
825 | telnet->bufsize = sk_write(telnet->s, b, 2); |
32874aea |
826 | break; |
827 | case TS_NOP: |
828 | b[1] = NOP; |
51470298 |
829 | telnet->bufsize = sk_write(telnet->s, b, 2); |
32874aea |
830 | break; |
831 | case TS_ABORT: |
832 | b[1] = ABORT; |
51470298 |
833 | telnet->bufsize = sk_write(telnet->s, b, 2); |
32874aea |
834 | break; |
835 | case TS_AO: |
836 | b[1] = AO; |
51470298 |
837 | telnet->bufsize = sk_write(telnet->s, b, 2); |
32874aea |
838 | break; |
839 | case TS_IP: |
840 | b[1] = IP; |
51470298 |
841 | telnet->bufsize = sk_write(telnet->s, b, 2); |
32874aea |
842 | break; |
843 | case TS_SUSP: |
844 | b[1] = SUSP; |
51470298 |
845 | telnet->bufsize = sk_write(telnet->s, b, 2); |
32874aea |
846 | break; |
847 | case TS_EOR: |
848 | b[1] = EOR; |
51470298 |
849 | telnet->bufsize = sk_write(telnet->s, b, 2); |
32874aea |
850 | break; |
851 | case TS_EOF: |
852 | b[1] = xEOF; |
51470298 |
853 | telnet->bufsize = sk_write(telnet->s, b, 2); |
32874aea |
854 | break; |
a5f3e637 |
855 | case TS_EOL: |
51470298 |
856 | telnet->bufsize = sk_write(telnet->s, "\r\n", 2); |
a5f3e637 |
857 | break; |
374330e2 |
858 | case TS_SYNCH: |
32874aea |
859 | b[1] = DM; |
51470298 |
860 | telnet->bufsize = sk_write(telnet->s, b, 1); |
861 | telnet->bufsize = sk_write_oob(telnet->s, b + 1, 1); |
32874aea |
862 | break; |
684d367c |
863 | case TS_RECHO: |
51470298 |
864 | if (telnet->opt_states[o_echo.index] == INACTIVE || |
865 | telnet->opt_states[o_echo.index] == REALLY_INACTIVE) { |
866 | telnet->opt_states[o_echo.index] = REQUESTED; |
867 | send_opt(telnet, o_echo.send, o_echo.option); |
684d367c |
868 | } |
869 | break; |
870 | case TS_LECHO: |
51470298 |
871 | if (telnet->opt_states[o_echo.index] == ACTIVE) { |
872 | telnet->opt_states[o_echo.index] = REQUESTED; |
873 | send_opt(telnet, o_echo.nsend, o_echo.option); |
684d367c |
874 | } |
374330e2 |
875 | break; |
32874aea |
876 | case TS_PING: |
51470298 |
877 | if (telnet->opt_states[o_they_sga.index] == ACTIVE) { |
32874aea |
878 | b[1] = NOP; |
51470298 |
879 | telnet->bufsize = sk_write(telnet->s, b, 2); |
ec55b220 |
880 | } |
32874aea |
881 | break; |
374330e2 |
882 | } |
883 | } |
884 | |
51470298 |
885 | static Socket telnet_socket(void *handle) |
32874aea |
886 | { |
51470298 |
887 | Telnet telnet = (Telnet) handle; |
888 | return telnet->s; |
32874aea |
889 | } |
8ccc75b0 |
890 | |
51470298 |
891 | static int telnet_sendok(void *handle) |
32874aea |
892 | { |
51470298 |
893 | Telnet telnet = (Telnet) handle; |
32874aea |
894 | return 1; |
895 | } |
4017be6d |
896 | |
51470298 |
897 | static void telnet_unthrottle(void *handle, int backlog) |
5471d09a |
898 | { |
51470298 |
899 | Telnet telnet = (Telnet) handle; |
900 | sk_set_frozen(telnet->s, backlog > TELNET_MAX_BACKLOG); |
5471d09a |
901 | } |
902 | |
51470298 |
903 | static int telnet_ldisc(void *handle, int option) |
32874aea |
904 | { |
51470298 |
905 | Telnet telnet = (Telnet) handle; |
32874aea |
906 | if (option == LD_ECHO) |
51470298 |
907 | return telnet->echoing; |
32874aea |
908 | if (option == LD_EDIT) |
51470298 |
909 | return telnet->editing; |
0965bee0 |
910 | return FALSE; |
911 | } |
912 | |
b9d7bcad |
913 | static void telnet_provide_ldisc(void *handle, void *ldisc) |
914 | { |
915 | Telnet telnet = (Telnet) handle; |
916 | telnet->ldisc = ldisc; |
917 | } |
918 | |
a8327734 |
919 | static void telnet_provide_logctx(void *handle, void *logctx) |
920 | { |
921 | /* This is a stub. */ |
922 | } |
923 | |
51470298 |
924 | static int telnet_exitcode(void *handle) |
d8d6c7e5 |
925 | { |
51470298 |
926 | Telnet telnet = (Telnet) handle; |
d8d6c7e5 |
927 | /* Telnet doesn't transmit exit codes back to the client */ |
928 | return 0; |
929 | } |
930 | |
374330e2 |
931 | Backend telnet_backend = { |
932 | telnet_init, |
374330e2 |
933 | telnet_send, |
5471d09a |
934 | telnet_sendbuffer, |
374330e2 |
935 | telnet_size, |
4017be6d |
936 | telnet_special, |
8ccc75b0 |
937 | telnet_socket, |
d8d6c7e5 |
938 | telnet_exitcode, |
97db3be4 |
939 | telnet_sendok, |
0965bee0 |
940 | telnet_ldisc, |
b9d7bcad |
941 | telnet_provide_ldisc, |
a8327734 |
942 | telnet_provide_logctx, |
5471d09a |
943 | telnet_unthrottle, |
97db3be4 |
944 | 23 |
374330e2 |
945 | }; |