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