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