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