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