]>
Commit | Line | Data |
---|---|---|
1c1af145 | 1 | /* |
2 | * Telnet backend. | |
3 | */ | |
4 | ||
5 | #include <stdio.h> | |
6 | #include <stdlib.h> | |
7 | ||
8 | #include "putty.h" | |
9 | ||
10 | #ifndef FALSE | |
11 | #define FALSE 0 | |
12 | #endif | |
13 | #ifndef TRUE | |
14 | #define TRUE 1 | |
15 | #endif | |
16 | ||
17 | #define IAC 255 /* interpret as command: */ | |
18 | #define DONT 254 /* you are not to use option */ | |
19 | #define DO 253 /* please, you use option */ | |
20 | #define WONT 252 /* I won't use option */ | |
21 | #define WILL 251 /* I will use option */ | |
22 | #define SB 250 /* interpret as subnegotiation */ | |
23 | #define SE 240 /* end sub negotiation */ | |
24 | ||
25 | #define GA 249 /* you may reverse the line */ | |
26 | #define EL 248 /* erase the current line */ | |
27 | #define EC 247 /* erase the current character */ | |
28 | #define AYT 246 /* are you there */ | |
29 | #define AO 245 /* abort output--but let prog finish */ | |
30 | #define IP 244 /* interrupt process--permanently */ | |
31 | #define BREAK 243 /* break */ | |
32 | #define DM 242 /* data mark--for connect. cleaning */ | |
33 | #define NOP 241 /* nop */ | |
34 | #define EOR 239 /* end of record (transparent mode) */ | |
35 | #define ABORT 238 /* Abort process */ | |
36 | #define SUSP 237 /* Suspend process */ | |
37 | #define xEOF 236 /* End of file: EOF is already used... */ | |
38 | ||
39 | #define TELOPTS(X) \ | |
40 | X(BINARY, 0) /* 8-bit data path */ \ | |
41 | X(ECHO, 1) /* echo */ \ | |
42 | X(RCP, 2) /* prepare to reconnect */ \ | |
43 | X(SGA, 3) /* suppress go ahead */ \ | |
44 | X(NAMS, 4) /* approximate message size */ \ | |
45 | X(STATUS, 5) /* give status */ \ | |
46 | X(TM, 6) /* timing mark */ \ | |
47 | X(RCTE, 7) /* remote controlled transmission and echo */ \ | |
48 | X(NAOL, 8) /* negotiate about output line width */ \ | |
49 | X(NAOP, 9) /* negotiate about output page size */ \ | |
50 | X(NAOCRD, 10) /* negotiate about CR disposition */ \ | |
51 | X(NAOHTS, 11) /* negotiate about horizontal tabstops */ \ | |
52 | X(NAOHTD, 12) /* negotiate about horizontal tab disposition */ \ | |
53 | X(NAOFFD, 13) /* negotiate about formfeed disposition */ \ | |
54 | X(NAOVTS, 14) /* negotiate about vertical tab stops */ \ | |
55 | X(NAOVTD, 15) /* negotiate about vertical tab disposition */ \ | |
56 | X(NAOLFD, 16) /* negotiate about output LF disposition */ \ | |
57 | X(XASCII, 17) /* extended ascic character set */ \ | |
58 | X(LOGOUT, 18) /* force logout */ \ | |
59 | X(BM, 19) /* byte macro */ \ | |
60 | X(DET, 20) /* data entry terminal */ \ | |
61 | X(SUPDUP, 21) /* supdup protocol */ \ | |
62 | X(SUPDUPOUTPUT, 22) /* supdup output */ \ | |
63 | X(SNDLOC, 23) /* send location */ \ | |
64 | X(TTYPE, 24) /* terminal type */ \ | |
65 | X(EOR, 25) /* end or record */ \ | |
66 | X(TUID, 26) /* TACACS user identification */ \ | |
67 | X(OUTMRK, 27) /* output marking */ \ | |
68 | X(TTYLOC, 28) /* terminal location number */ \ | |
69 | X(3270REGIME, 29) /* 3270 regime */ \ | |
70 | X(X3PAD, 30) /* X.3 PAD */ \ | |
71 | X(NAWS, 31) /* window size */ \ | |
72 | X(TSPEED, 32) /* terminal speed */ \ | |
73 | X(LFLOW, 33) /* remote flow control */ \ | |
74 | X(LINEMODE, 34) /* Linemode option */ \ | |
75 | X(XDISPLOC, 35) /* X Display Location */ \ | |
76 | X(OLD_ENVIRON, 36) /* Old - Environment variables */ \ | |
77 | X(AUTHENTICATION, 37) /* Authenticate */ \ | |
78 | X(ENCRYPT, 38) /* Encryption option */ \ | |
79 | X(NEW_ENVIRON, 39) /* New - Environment variables */ \ | |
80 | X(TN3270E, 40) /* TN3270 enhancements */ \ | |
81 | X(XAUTH, 41) \ | |
82 | X(CHARSET, 42) /* Character set */ \ | |
83 | X(RSP, 43) /* Remote serial port */ \ | |
84 | X(COM_PORT_OPTION, 44) /* Com port control */ \ | |
85 | X(SLE, 45) /* Suppress local echo */ \ | |
86 | X(STARTTLS, 46) /* Start TLS */ \ | |
87 | X(KERMIT, 47) /* Automatic Kermit file transfer */ \ | |
88 | X(SEND_URL, 48) \ | |
89 | X(FORWARD_X, 49) \ | |
90 | X(PRAGMA_LOGON, 138) \ | |
91 | X(SSPI_LOGON, 139) \ | |
92 | X(PRAGMA_HEARTBEAT, 140) \ | |
93 | X(EXOPL, 255) /* extended-options-list */ | |
94 | ||
95 | #define telnet_enum(x,y) TELOPT_##x = y, | |
96 | enum { TELOPTS(telnet_enum) dummy=0 }; | |
97 | #undef telnet_enum | |
98 | ||
99 | #define TELQUAL_IS 0 /* option is... */ | |
100 | #define TELQUAL_SEND 1 /* send option */ | |
101 | #define TELQUAL_INFO 2 /* ENVIRON: informational version of IS */ | |
102 | #define BSD_VAR 1 | |
103 | #define BSD_VALUE 0 | |
104 | #define RFC_VAR 0 | |
105 | #define RFC_VALUE 1 | |
106 | ||
107 | #define CR 13 | |
108 | #define LF 10 | |
109 | #define NUL 0 | |
110 | ||
111 | #define iswritable(x) \ | |
112 | ( (x) != IAC && \ | |
113 | (telnet->opt_states[o_we_bin.index] == ACTIVE || (x) != CR)) | |
114 | ||
115 | static char *telopt(int opt) | |
116 | { | |
117 | #define telnet_str(x,y) case TELOPT_##x: return #x; | |
118 | switch (opt) { | |
119 | TELOPTS(telnet_str) | |
120 | default: | |
121 | return "<unknown>"; | |
122 | } | |
123 | #undef telnet_str | |
124 | } | |
125 | ||
126 | static void telnet_size(void *handle, int width, int height); | |
127 | ||
128 | struct Opt { | |
129 | int send; /* what we initially send */ | |
130 | int nsend; /* -ve send if requested to stop it */ | |
131 | int ack, nak; /* +ve and -ve acknowledgements */ | |
132 | int option; /* the option code */ | |
133 | int index; /* index into telnet->opt_states[] */ | |
134 | enum { | |
135 | REQUESTED, ACTIVE, INACTIVE, REALLY_INACTIVE | |
136 | } initial_state; | |
137 | }; | |
138 | ||
139 | enum { | |
140 | OPTINDEX_NAWS, | |
141 | OPTINDEX_TSPEED, | |
142 | OPTINDEX_TTYPE, | |
143 | OPTINDEX_OENV, | |
144 | OPTINDEX_NENV, | |
145 | OPTINDEX_ECHO, | |
146 | OPTINDEX_WE_SGA, | |
147 | OPTINDEX_THEY_SGA, | |
148 | OPTINDEX_WE_BIN, | |
149 | OPTINDEX_THEY_BIN, | |
150 | NUM_OPTS | |
151 | }; | |
152 | ||
153 | static const struct Opt o_naws = | |
154 | { WILL, WONT, DO, DONT, TELOPT_NAWS, OPTINDEX_NAWS, REQUESTED }; | |
155 | static const struct Opt o_tspeed = | |
156 | { WILL, WONT, DO, DONT, TELOPT_TSPEED, OPTINDEX_TSPEED, REQUESTED }; | |
157 | static const struct Opt o_ttype = | |
158 | { WILL, WONT, DO, DONT, TELOPT_TTYPE, OPTINDEX_TTYPE, REQUESTED }; | |
159 | static const struct Opt o_oenv = | |
160 | { WILL, WONT, DO, DONT, TELOPT_OLD_ENVIRON, OPTINDEX_OENV, INACTIVE }; | |
161 | static const struct Opt o_nenv = | |
162 | { WILL, WONT, DO, DONT, TELOPT_NEW_ENVIRON, OPTINDEX_NENV, REQUESTED }; | |
163 | static const struct Opt o_echo = | |
164 | { DO, DONT, WILL, WONT, TELOPT_ECHO, OPTINDEX_ECHO, REQUESTED }; | |
165 | static const struct Opt o_we_sga = | |
166 | { WILL, WONT, DO, DONT, TELOPT_SGA, OPTINDEX_WE_SGA, REQUESTED }; | |
167 | static const struct Opt o_they_sga = | |
168 | { DO, DONT, WILL, WONT, TELOPT_SGA, OPTINDEX_THEY_SGA, REQUESTED }; | |
169 | static const struct Opt o_we_bin = | |
170 | { WILL, WONT, DO, DONT, TELOPT_BINARY, OPTINDEX_WE_BIN, INACTIVE }; | |
171 | static const struct Opt o_they_bin = | |
172 | { DO, DONT, WILL, WONT, TELOPT_BINARY, OPTINDEX_THEY_BIN, INACTIVE }; | |
173 | ||
174 | static const struct Opt *const opts[] = { | |
175 | &o_naws, &o_tspeed, &o_ttype, &o_oenv, &o_nenv, &o_echo, | |
176 | &o_we_sga, &o_they_sga, &o_we_bin, &o_they_bin, NULL | |
177 | }; | |
178 | ||
179 | typedef struct telnet_tag { | |
180 | const struct plug_function_table *fn; | |
181 | /* the above field _must_ be first in the structure */ | |
182 | ||
183 | Socket s; | |
184 | ||
185 | void *frontend; | |
186 | void *ldisc; | |
187 | int term_width, term_height; | |
188 | ||
189 | int opt_states[NUM_OPTS]; | |
190 | ||
191 | int echoing, editing; | |
192 | int activated; | |
193 | int bufsize; | |
194 | int in_synch; | |
195 | int sb_opt, sb_len; | |
196 | unsigned char *sb_buf; | |
197 | int sb_size; | |
198 | ||
199 | enum { | |
200 | TOP_LEVEL, SEENIAC, SEENWILL, SEENWONT, SEENDO, SEENDONT, | |
201 | SEENSB, SUBNEGOT, SUBNEG_IAC, SEENCR | |
202 | } state; | |
203 | ||
204 | Config cfg; | |
205 | ||
206 | Pinger pinger; | |
207 | } *Telnet; | |
208 | ||
209 | #define TELNET_MAX_BACKLOG 4096 | |
210 | ||
211 | #define SB_DELTA 1024 | |
212 | ||
213 | static void c_write(Telnet telnet, char *buf, int len) | |
214 | { | |
215 | int backlog; | |
216 | backlog = from_backend(telnet->frontend, 0, buf, len); | |
217 | sk_set_frozen(telnet->s, backlog > TELNET_MAX_BACKLOG); | |
218 | } | |
219 | ||
220 | static void log_option(Telnet telnet, char *sender, int cmd, int option) | |
221 | { | |
222 | char *buf; | |
223 | /* | |
224 | * The strange-looking "<?""?>" below is there to avoid a | |
225 | * trigraph - a double question mark followed by > maps to a | |
226 | * closing brace character! | |
227 | */ | |
228 | buf = dupprintf("%s:\t%s %s", sender, | |
229 | (cmd == WILL ? "WILL" : cmd == WONT ? "WONT" : | |
230 | cmd == DO ? "DO" : cmd == DONT ? "DONT" : "<?""?>"), | |
231 | telopt(option)); | |
232 | logevent(telnet->frontend, buf); | |
233 | sfree(buf); | |
234 | } | |
235 | ||
236 | static void send_opt(Telnet telnet, int cmd, int option) | |
237 | { | |
238 | unsigned char b[3]; | |
239 | ||
240 | b[0] = IAC; | |
241 | b[1] = cmd; | |
242 | b[2] = option; | |
243 | telnet->bufsize = sk_write(telnet->s, (char *)b, 3); | |
244 | log_option(telnet, "client", cmd, option); | |
245 | } | |
246 | ||
247 | static void deactivate_option(Telnet telnet, const struct Opt *o) | |
248 | { | |
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; | |
253 | } | |
254 | ||
255 | /* | |
256 | * Generate side effects of enabling or disabling an option. | |
257 | */ | |
258 | static void option_side_effects(Telnet telnet, const struct Opt *o, int enabled) | |
259 | { | |
260 | if (o->option == TELOPT_ECHO && o->send == DO) | |
261 | telnet->echoing = !enabled; | |
262 | else if (o->option == TELOPT_SGA && o->send == DO) | |
263 | telnet->editing = !enabled; | |
264 | if (telnet->ldisc) /* cause ldisc to notice the change */ | |
265 | ldisc_send(telnet->ldisc, NULL, 0, 0); | |
266 | ||
267 | /* Ensure we get the minimum options */ | |
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); | |
272 | } | |
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); | |
276 | } | |
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); | |
280 | } | |
281 | telnet->activated = TRUE; | |
282 | } | |
283 | } | |
284 | ||
285 | static void activate_option(Telnet telnet, const struct Opt *o) | |
286 | { | |
287 | if (o->send == WILL && o->option == TELOPT_NAWS) | |
288 | telnet_size(telnet, telnet->term_width, telnet->term_height); | |
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 | */ | |
296 | deactivate_option(telnet, o->option == | |
297 | TELOPT_NEW_ENVIRON ? &o_oenv : &o_nenv); | |
298 | } | |
299 | option_side_effects(telnet, o, 1); | |
300 | } | |
301 | ||
302 | static void refused_option(Telnet telnet, const struct Opt *o) | |
303 | { | |
304 | if (o->send == WILL && o->option == TELOPT_NEW_ENVIRON && | |
305 | telnet->opt_states[o_oenv.index] == INACTIVE) { | |
306 | send_opt(telnet, WILL, TELOPT_OLD_ENVIRON); | |
307 | telnet->opt_states[o_oenv.index] = REQUESTED; | |
308 | } | |
309 | option_side_effects(telnet, o, 0); | |
310 | } | |
311 | ||
312 | static void proc_rec_opt(Telnet telnet, int cmd, int option) | |
313 | { | |
314 | const struct Opt *const *o; | |
315 | ||
316 | log_option(telnet, "server", cmd, option); | |
317 | for (o = opts; *o; o++) { | |
318 | if ((*o)->option == option && (*o)->ack == cmd) { | |
319 | switch (telnet->opt_states[(*o)->index]) { | |
320 | case REQUESTED: | |
321 | telnet->opt_states[(*o)->index] = ACTIVE; | |
322 | activate_option(telnet, *o); | |
323 | break; | |
324 | case ACTIVE: | |
325 | break; | |
326 | case INACTIVE: | |
327 | telnet->opt_states[(*o)->index] = ACTIVE; | |
328 | send_opt(telnet, (*o)->send, option); | |
329 | activate_option(telnet, *o); | |
330 | break; | |
331 | case REALLY_INACTIVE: | |
332 | send_opt(telnet, (*o)->nsend, option); | |
333 | break; | |
334 | } | |
335 | return; | |
336 | } else if ((*o)->option == option && (*o)->nak == cmd) { | |
337 | switch (telnet->opt_states[(*o)->index]) { | |
338 | case REQUESTED: | |
339 | telnet->opt_states[(*o)->index] = INACTIVE; | |
340 | refused_option(telnet, *o); | |
341 | break; | |
342 | case ACTIVE: | |
343 | telnet->opt_states[(*o)->index] = INACTIVE; | |
344 | send_opt(telnet, (*o)->nsend, option); | |
345 | option_side_effects(telnet, *o, 0); | |
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. If the request was positive (WILL or DO), we send | |
357 | * a negative ack to indicate refusal. If the request was | |
358 | * negative (WONT / DONT), we must do nothing. | |
359 | */ | |
360 | if (cmd == WILL || cmd == DO) | |
361 | send_opt(telnet, (cmd == WILL ? DONT : WONT), option); | |
362 | } | |
363 | ||
364 | static void process_subneg(Telnet telnet) | |
365 | { | |
366 | unsigned char b[2048], *p, *q; | |
367 | int var, value, n; | |
368 | char *e; | |
369 | ||
370 | switch (telnet->sb_opt) { | |
371 | case TELOPT_TSPEED: | |
372 | if (telnet->sb_len == 1 && telnet->sb_buf[0] == TELQUAL_SEND) { | |
373 | char *logbuf; | |
374 | b[0] = IAC; | |
375 | b[1] = SB; | |
376 | b[2] = TELOPT_TSPEED; | |
377 | b[3] = TELQUAL_IS; | |
378 | strcpy((char *)(b + 4), telnet->cfg.termspeed); | |
379 | n = 4 + strlen(telnet->cfg.termspeed); | |
380 | b[n] = IAC; | |
381 | b[n + 1] = SE; | |
382 | telnet->bufsize = sk_write(telnet->s, (char *)b, n + 2); | |
383 | logevent(telnet->frontend, "server:\tSB TSPEED SEND"); | |
384 | logbuf = dupprintf("client:\tSB TSPEED IS %s", telnet->cfg.termspeed); | |
385 | logevent(telnet->frontend, logbuf); | |
386 | sfree(logbuf); | |
387 | } else | |
388 | logevent(telnet->frontend, "server:\tSB TSPEED <something weird>"); | |
389 | break; | |
390 | case TELOPT_TTYPE: | |
391 | if (telnet->sb_len == 1 && telnet->sb_buf[0] == TELQUAL_SEND) { | |
392 | char *logbuf; | |
393 | b[0] = IAC; | |
394 | b[1] = SB; | |
395 | b[2] = TELOPT_TTYPE; | |
396 | b[3] = TELQUAL_IS; | |
397 | for (n = 0; telnet->cfg.termtype[n]; n++) | |
398 | b[n + 4] = (telnet->cfg.termtype[n] >= 'a' | |
399 | && telnet->cfg.termtype[n] <= | |
400 | 'z' ? telnet->cfg.termtype[n] + 'A' - | |
401 | 'a' : telnet->cfg.termtype[n]); | |
402 | b[n + 4] = IAC; | |
403 | b[n + 5] = SE; | |
404 | telnet->bufsize = sk_write(telnet->s, (char *)b, n + 6); | |
405 | b[n + 4] = 0; | |
406 | logevent(telnet->frontend, "server:\tSB TTYPE SEND"); | |
407 | logbuf = dupprintf("client:\tSB TTYPE IS %s", b + 4); | |
408 | logevent(telnet->frontend, logbuf); | |
409 | sfree(logbuf); | |
410 | } else | |
411 | logevent(telnet->frontend, "server:\tSB TTYPE <something weird>\r\n"); | |
412 | break; | |
413 | case TELOPT_OLD_ENVIRON: | |
414 | case TELOPT_NEW_ENVIRON: | |
415 | p = telnet->sb_buf; | |
416 | q = p + telnet->sb_len; | |
417 | if (p < q && *p == TELQUAL_SEND) { | |
418 | char *logbuf; | |
419 | p++; | |
420 | logbuf = dupprintf("server:\tSB %s SEND", telopt(telnet->sb_opt)); | |
421 | logevent(telnet->frontend, logbuf); | |
422 | sfree(logbuf); | |
423 | if (telnet->sb_opt == TELOPT_OLD_ENVIRON) { | |
424 | if (telnet->cfg.rfc_environ) { | |
425 | value = RFC_VALUE; | |
426 | var = RFC_VAR; | |
427 | } else { | |
428 | value = BSD_VALUE; | |
429 | var = BSD_VAR; | |
430 | } | |
431 | /* | |
432 | * Try to guess the sense of VAR and VALUE. | |
433 | */ | |
434 | while (p < q) { | |
435 | if (*p == RFC_VAR) { | |
436 | value = RFC_VALUE; | |
437 | var = RFC_VAR; | |
438 | } else if (*p == BSD_VAR) { | |
439 | value = BSD_VALUE; | |
440 | var = BSD_VAR; | |
441 | } | |
442 | p++; | |
443 | } | |
444 | } else { | |
445 | /* | |
446 | * With NEW_ENVIRON, the sense of VAR and VALUE | |
447 | * isn't in doubt. | |
448 | */ | |
449 | value = RFC_VALUE; | |
450 | var = RFC_VAR; | |
451 | } | |
452 | b[0] = IAC; | |
453 | b[1] = SB; | |
454 | b[2] = telnet->sb_opt; | |
455 | b[3] = TELQUAL_IS; | |
456 | n = 4; | |
457 | e = telnet->cfg.environmt; | |
458 | while (*e) { | |
459 | b[n++] = var; | |
460 | while (*e && *e != '\t') | |
461 | b[n++] = *e++; | |
462 | if (*e == '\t') | |
463 | e++; | |
464 | b[n++] = value; | |
465 | while (*e) | |
466 | b[n++] = *e++; | |
467 | e++; | |
468 | } | |
469 | { | |
470 | char user[sizeof(telnet->cfg.username)]; | |
471 | (void) get_remote_username(&telnet->cfg, user, sizeof(user)); | |
472 | if (*user) { | |
473 | b[n++] = var; | |
474 | b[n++] = 'U'; | |
475 | b[n++] = 'S'; | |
476 | b[n++] = 'E'; | |
477 | b[n++] = 'R'; | |
478 | b[n++] = value; | |
479 | e = user; | |
480 | while (*e) | |
481 | b[n++] = *e++; | |
482 | } | |
483 | b[n++] = IAC; | |
484 | b[n++] = SE; | |
485 | telnet->bufsize = sk_write(telnet->s, (char *)b, n); | |
486 | logbuf = dupprintf("client:\tSB %s IS %s%s%s%s", | |
487 | telopt(telnet->sb_opt), | |
488 | *user ? "USER=" : "", | |
489 | user, | |
490 | *user ? " " : "", | |
491 | n == 6 ? "<nothing>" : | |
492 | (*telnet->cfg.environmt ? "<stuff>" : "")); | |
493 | logevent(telnet->frontend, logbuf); | |
494 | sfree(logbuf); | |
495 | } | |
496 | } | |
497 | break; | |
498 | } | |
499 | } | |
500 | ||
501 | static void do_telnet_read(Telnet telnet, char *buf, int len) | |
502 | { | |
503 | char *outbuf = NULL; | |
504 | int outbuflen = 0, outbufsize = 0; | |
505 | ||
506 | #define ADDTOBUF(c) do { \ | |
507 | if (outbuflen >= outbufsize) { \ | |
508 | outbufsize = outbuflen + 256; \ | |
509 | outbuf = sresize(outbuf, outbufsize, char); \ | |
510 | } \ | |
511 | outbuf[outbuflen++] = (c); \ | |
512 | } while (0) | |
513 | ||
514 | while (len--) { | |
515 | int c = (unsigned char) *buf++; | |
516 | ||
517 | switch (telnet->state) { | |
518 | case TOP_LEVEL: | |
519 | case SEENCR: | |
520 | if (c == NUL && telnet->state == SEENCR) | |
521 | telnet->state = TOP_LEVEL; | |
522 | else if (c == IAC) | |
523 | telnet->state = SEENIAC; | |
524 | else { | |
525 | if (!telnet->in_synch) | |
526 | ADDTOBUF(c); | |
527 | ||
528 | #if 1 | |
529 | /* I can't get the F***ing winsock to insert the urgent IAC | |
530 | * into the right position! Even with SO_OOBINLINE it gives | |
531 | * it to recv too soon. And of course the DM byte (that | |
532 | * arrives in the same packet!) appears several K later!! | |
533 | * | |
534 | * Oh well, we do get the DM in the right place so I'll | |
535 | * just stop hiding on the next 0xf2 and hope for the best. | |
536 | */ | |
537 | else if (c == DM) | |
538 | telnet->in_synch = 0; | |
539 | #endif | |
540 | if (c == CR && telnet->opt_states[o_they_bin.index] != ACTIVE) | |
541 | telnet->state = SEENCR; | |
542 | else | |
543 | telnet->state = TOP_LEVEL; | |
544 | } | |
545 | break; | |
546 | case SEENIAC: | |
547 | if (c == DO) | |
548 | telnet->state = SEENDO; | |
549 | else if (c == DONT) | |
550 | telnet->state = SEENDONT; | |
551 | else if (c == WILL) | |
552 | telnet->state = SEENWILL; | |
553 | else if (c == WONT) | |
554 | telnet->state = SEENWONT; | |
555 | else if (c == SB) | |
556 | telnet->state = SEENSB; | |
557 | else if (c == DM) { | |
558 | telnet->in_synch = 0; | |
559 | telnet->state = TOP_LEVEL; | |
560 | } else { | |
561 | /* ignore everything else; print it if it's IAC */ | |
562 | if (c == IAC) { | |
563 | ADDTOBUF(c); | |
564 | } | |
565 | telnet->state = TOP_LEVEL; | |
566 | } | |
567 | break; | |
568 | case SEENWILL: | |
569 | proc_rec_opt(telnet, WILL, c); | |
570 | telnet->state = TOP_LEVEL; | |
571 | break; | |
572 | case SEENWONT: | |
573 | proc_rec_opt(telnet, WONT, c); | |
574 | telnet->state = TOP_LEVEL; | |
575 | break; | |
576 | case SEENDO: | |
577 | proc_rec_opt(telnet, DO, c); | |
578 | telnet->state = TOP_LEVEL; | |
579 | break; | |
580 | case SEENDONT: | |
581 | proc_rec_opt(telnet, DONT, c); | |
582 | telnet->state = TOP_LEVEL; | |
583 | break; | |
584 | case SEENSB: | |
585 | telnet->sb_opt = c; | |
586 | telnet->sb_len = 0; | |
587 | telnet->state = SUBNEGOT; | |
588 | break; | |
589 | case SUBNEGOT: | |
590 | if (c == IAC) | |
591 | telnet->state = SUBNEG_IAC; | |
592 | else { | |
593 | subneg_addchar: | |
594 | if (telnet->sb_len >= telnet->sb_size) { | |
595 | telnet->sb_size += SB_DELTA; | |
596 | telnet->sb_buf = sresize(telnet->sb_buf, telnet->sb_size, | |
597 | unsigned char); | |
598 | } | |
599 | telnet->sb_buf[telnet->sb_len++] = c; | |
600 | telnet->state = SUBNEGOT; /* in case we came here by goto */ | |
601 | } | |
602 | break; | |
603 | case SUBNEG_IAC: | |
604 | if (c != SE) | |
605 | goto subneg_addchar; /* yes, it's a hack, I know, but... */ | |
606 | else { | |
607 | process_subneg(telnet); | |
608 | telnet->state = TOP_LEVEL; | |
609 | } | |
610 | break; | |
611 | } | |
612 | } | |
613 | ||
614 | if (outbuflen) | |
615 | c_write(telnet, outbuf, outbuflen); | |
616 | sfree(outbuf); | |
617 | } | |
618 | ||
619 | static void telnet_log(Plug plug, int type, SockAddr addr, int port, | |
620 | const char *error_msg, int error_code) | |
621 | { | |
622 | Telnet telnet = (Telnet) plug; | |
623 | char addrbuf[256], *msg; | |
624 | ||
625 | sk_getaddr(addr, addrbuf, lenof(addrbuf)); | |
626 | ||
627 | if (type == 0) | |
628 | msg = dupprintf("Connecting to %s port %d", addrbuf, port); | |
629 | else | |
630 | msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg); | |
631 | ||
632 | logevent(telnet->frontend, msg); | |
633 | } | |
634 | ||
635 | static int telnet_closing(Plug plug, const char *error_msg, int error_code, | |
636 | int calling_back) | |
637 | { | |
638 | Telnet telnet = (Telnet) plug; | |
639 | ||
640 | if (telnet->s) { | |
641 | sk_close(telnet->s); | |
642 | telnet->s = NULL; | |
643 | notify_remote_exit(telnet->frontend); | |
644 | } | |
645 | if (error_msg) { | |
646 | logevent(telnet->frontend, error_msg); | |
647 | connection_fatal(telnet->frontend, "%s", error_msg); | |
648 | } | |
649 | /* Otherwise, the remote side closed the connection normally. */ | |
650 | return 0; | |
651 | } | |
652 | ||
653 | static int telnet_receive(Plug plug, int urgent, char *data, int len) | |
654 | { | |
655 | Telnet telnet = (Telnet) plug; | |
656 | if (urgent) | |
657 | telnet->in_synch = TRUE; | |
658 | do_telnet_read(telnet, data, len); | |
659 | return 1; | |
660 | } | |
661 | ||
662 | static void telnet_sent(Plug plug, int bufsize) | |
663 | { | |
664 | Telnet telnet = (Telnet) plug; | |
665 | telnet->bufsize = bufsize; | |
666 | } | |
667 | ||
668 | /* | |
669 | * Called to set up the Telnet connection. | |
670 | * | |
671 | * Returns an error message, or NULL on success. | |
672 | * | |
673 | * Also places the canonical host name into `realhost'. It must be | |
674 | * freed by the caller. | |
675 | */ | |
676 | static const char *telnet_init(void *frontend_handle, void **backend_handle, | |
677 | Config *cfg, | |
678 | char *host, int port, char **realhost, | |
679 | int nodelay, int keepalive) | |
680 | { | |
681 | static const struct plug_function_table fn_table = { | |
682 | telnet_log, | |
683 | telnet_closing, | |
684 | telnet_receive, | |
685 | telnet_sent | |
686 | }; | |
687 | SockAddr addr; | |
688 | const char *err; | |
689 | Telnet telnet; | |
690 | ||
691 | telnet = snew(struct telnet_tag); | |
692 | telnet->fn = &fn_table; | |
693 | telnet->cfg = *cfg; /* STRUCTURE COPY */ | |
694 | telnet->s = NULL; | |
695 | telnet->echoing = TRUE; | |
696 | telnet->editing = TRUE; | |
697 | telnet->activated = FALSE; | |
698 | telnet->sb_buf = NULL; | |
699 | telnet->sb_size = 0; | |
700 | telnet->frontend = frontend_handle; | |
701 | telnet->term_width = telnet->cfg.width; | |
702 | telnet->term_height = telnet->cfg.height; | |
703 | telnet->state = TOP_LEVEL; | |
704 | telnet->ldisc = NULL; | |
705 | telnet->pinger = NULL; | |
706 | *backend_handle = telnet; | |
707 | ||
708 | /* | |
709 | * Try to find host. | |
710 | */ | |
711 | { | |
712 | char *buf; | |
713 | buf = dupprintf("Looking up host \"%s\"%s", host, | |
714 | (cfg->addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" : | |
715 | (cfg->addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" : | |
716 | ""))); | |
717 | logevent(telnet->frontend, buf); | |
718 | sfree(buf); | |
719 | } | |
720 | addr = name_lookup(host, port, realhost, &telnet->cfg, cfg->addressfamily); | |
721 | if ((err = sk_addr_error(addr)) != NULL) { | |
722 | sk_addr_free(addr); | |
723 | return err; | |
724 | } | |
725 | ||
726 | if (port < 0) | |
727 | port = 23; /* default telnet port */ | |
728 | ||
729 | /* | |
730 | * Open socket. | |
731 | */ | |
732 | telnet->s = new_connection(addr, *realhost, port, 0, 1, | |
733 | nodelay, keepalive, (Plug) telnet, &telnet->cfg); | |
734 | if ((err = sk_socket_error(telnet->s)) != NULL) | |
735 | return err; | |
736 | ||
737 | telnet->pinger = pinger_new(&telnet->cfg, &telnet_backend, telnet); | |
738 | ||
739 | /* | |
740 | * Initialise option states. | |
741 | */ | |
742 | if (telnet->cfg.passive_telnet) { | |
743 | const struct Opt *const *o; | |
744 | ||
745 | for (o = opts; *o; o++) | |
746 | telnet->opt_states[(*o)->index] = INACTIVE; | |
747 | } else { | |
748 | const struct Opt *const *o; | |
749 | ||
750 | for (o = opts; *o; o++) { | |
751 | telnet->opt_states[(*o)->index] = (*o)->initial_state; | |
752 | if (telnet->opt_states[(*o)->index] == REQUESTED) | |
753 | send_opt(telnet, (*o)->send, (*o)->option); | |
754 | } | |
755 | telnet->activated = TRUE; | |
756 | } | |
757 | ||
758 | /* | |
759 | * Set up SYNCH state. | |
760 | */ | |
761 | telnet->in_synch = FALSE; | |
762 | ||
763 | /* | |
764 | * We can send special commands from the start. | |
765 | */ | |
766 | update_specials_menu(telnet->frontend); | |
767 | ||
768 | /* | |
769 | * loghost overrides realhost, if specified. | |
770 | */ | |
771 | if (*telnet->cfg.loghost) { | |
772 | char *colon; | |
773 | ||
774 | sfree(*realhost); | |
775 | *realhost = dupstr(telnet->cfg.loghost); | |
776 | colon = strrchr(*realhost, ':'); | |
777 | if (colon) { | |
778 | /* | |
779 | * FIXME: if we ever update this aspect of ssh.c for | |
780 | * IPv6 literal management, this should change in line | |
781 | * with it. | |
782 | */ | |
783 | *colon++ = '\0'; | |
784 | } | |
785 | } | |
786 | ||
787 | return NULL; | |
788 | } | |
789 | ||
790 | static void telnet_free(void *handle) | |
791 | { | |
792 | Telnet telnet = (Telnet) handle; | |
793 | ||
794 | sfree(telnet->sb_buf); | |
795 | if (telnet->s) | |
796 | sk_close(telnet->s); | |
797 | if (telnet->pinger) | |
798 | pinger_free(telnet->pinger); | |
799 | sfree(telnet); | |
800 | } | |
801 | /* | |
802 | * Reconfigure the Telnet backend. There's no immediate action | |
803 | * necessary, in this backend: we just save the fresh config for | |
804 | * any subsequent negotiations. | |
805 | */ | |
806 | static void telnet_reconfig(void *handle, Config *cfg) | |
807 | { | |
808 | Telnet telnet = (Telnet) handle; | |
809 | pinger_reconfig(telnet->pinger, &telnet->cfg, cfg); | |
810 | telnet->cfg = *cfg; /* STRUCTURE COPY */ | |
811 | } | |
812 | ||
813 | /* | |
814 | * Called to send data down the Telnet connection. | |
815 | */ | |
816 | static int telnet_send(void *handle, char *buf, int len) | |
817 | { | |
818 | Telnet telnet = (Telnet) handle; | |
819 | unsigned char *p, *end; | |
820 | static const unsigned char iac[2] = { IAC, IAC }; | |
821 | static const unsigned char cr[2] = { CR, NUL }; | |
822 | #if 0 | |
823 | static const unsigned char nl[2] = { CR, LF }; | |
824 | #endif | |
825 | ||
826 | if (telnet->s == NULL) | |
827 | return 0; | |
828 | ||
829 | p = (unsigned char *)buf; | |
830 | end = (unsigned char *)(buf + len); | |
831 | while (p < end) { | |
832 | unsigned char *q = p; | |
833 | ||
834 | while (p < end && iswritable(*p)) | |
835 | p++; | |
836 | telnet->bufsize = sk_write(telnet->s, (char *)q, p - q); | |
837 | ||
838 | while (p < end && !iswritable(*p)) { | |
839 | telnet->bufsize = | |
840 | sk_write(telnet->s, (char *)(*p == IAC ? iac : cr), 2); | |
841 | p++; | |
842 | } | |
843 | } | |
844 | ||
845 | return telnet->bufsize; | |
846 | } | |
847 | ||
848 | /* | |
849 | * Called to query the current socket sendability status. | |
850 | */ | |
851 | static int telnet_sendbuffer(void *handle) | |
852 | { | |
853 | Telnet telnet = (Telnet) handle; | |
854 | return telnet->bufsize; | |
855 | } | |
856 | ||
857 | /* | |
858 | * Called to set the size of the window from Telnet's POV. | |
859 | */ | |
860 | static void telnet_size(void *handle, int width, int height) | |
861 | { | |
862 | Telnet telnet = (Telnet) handle; | |
863 | unsigned char b[24]; | |
864 | int n; | |
865 | char *logbuf; | |
866 | ||
867 | telnet->term_width = width; | |
868 | telnet->term_height = height; | |
869 | ||
870 | if (telnet->s == NULL || telnet->opt_states[o_naws.index] != ACTIVE) | |
871 | return; | |
872 | n = 0; | |
873 | b[n++] = IAC; | |
874 | b[n++] = SB; | |
875 | b[n++] = TELOPT_NAWS; | |
876 | b[n++] = telnet->term_width >> 8; | |
877 | if (b[n-1] == IAC) b[n++] = IAC; /* duplicate any IAC byte occurs */ | |
878 | b[n++] = telnet->term_width & 0xFF; | |
879 | if (b[n-1] == IAC) b[n++] = IAC; /* duplicate any IAC byte occurs */ | |
880 | b[n++] = telnet->term_height >> 8; | |
881 | if (b[n-1] == IAC) b[n++] = IAC; /* duplicate any IAC byte occurs */ | |
882 | b[n++] = telnet->term_height & 0xFF; | |
883 | if (b[n-1] == IAC) b[n++] = IAC; /* duplicate any IAC byte occurs */ | |
884 | b[n++] = IAC; | |
885 | b[n++] = SE; | |
886 | telnet->bufsize = sk_write(telnet->s, (char *)b, n); | |
887 | logbuf = dupprintf("client:\tSB NAWS %d,%d", | |
888 | telnet->term_width, telnet->term_height); | |
889 | logevent(telnet->frontend, logbuf); | |
890 | sfree(logbuf); | |
891 | } | |
892 | ||
893 | /* | |
894 | * Send Telnet special codes. | |
895 | */ | |
896 | static void telnet_special(void *handle, Telnet_Special code) | |
897 | { | |
898 | Telnet telnet = (Telnet) handle; | |
899 | unsigned char b[2]; | |
900 | ||
901 | if (telnet->s == NULL) | |
902 | return; | |
903 | ||
904 | b[0] = IAC; | |
905 | switch (code) { | |
906 | case TS_AYT: | |
907 | b[1] = AYT; | |
908 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); | |
909 | break; | |
910 | case TS_BRK: | |
911 | b[1] = BREAK; | |
912 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); | |
913 | break; | |
914 | case TS_EC: | |
915 | b[1] = EC; | |
916 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); | |
917 | break; | |
918 | case TS_EL: | |
919 | b[1] = EL; | |
920 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); | |
921 | break; | |
922 | case TS_GA: | |
923 | b[1] = GA; | |
924 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); | |
925 | break; | |
926 | case TS_NOP: | |
927 | b[1] = NOP; | |
928 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); | |
929 | break; | |
930 | case TS_ABORT: | |
931 | b[1] = ABORT; | |
932 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); | |
933 | break; | |
934 | case TS_AO: | |
935 | b[1] = AO; | |
936 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); | |
937 | break; | |
938 | case TS_IP: | |
939 | b[1] = IP; | |
940 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); | |
941 | break; | |
942 | case TS_SUSP: | |
943 | b[1] = SUSP; | |
944 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); | |
945 | break; | |
946 | case TS_EOR: | |
947 | b[1] = EOR; | |
948 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); | |
949 | break; | |
950 | case TS_EOF: | |
951 | b[1] = xEOF; | |
952 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); | |
953 | break; | |
954 | case TS_EOL: | |
955 | /* In BINARY mode, CR-LF becomes just CR - | |
956 | * and without the NUL suffix too. */ | |
957 | if (telnet->opt_states[o_we_bin.index] == ACTIVE) | |
958 | telnet->bufsize = sk_write(telnet->s, "\r", 1); | |
959 | else | |
960 | telnet->bufsize = sk_write(telnet->s, "\r\n", 2); | |
961 | break; | |
962 | case TS_SYNCH: | |
963 | b[1] = DM; | |
964 | telnet->bufsize = sk_write(telnet->s, (char *)b, 1); | |
965 | telnet->bufsize = sk_write_oob(telnet->s, (char *)(b + 1), 1); | |
966 | break; | |
967 | case TS_RECHO: | |
968 | if (telnet->opt_states[o_echo.index] == INACTIVE || | |
969 | telnet->opt_states[o_echo.index] == REALLY_INACTIVE) { | |
970 | telnet->opt_states[o_echo.index] = REQUESTED; | |
971 | send_opt(telnet, o_echo.send, o_echo.option); | |
972 | } | |
973 | break; | |
974 | case TS_LECHO: | |
975 | if (telnet->opt_states[o_echo.index] == ACTIVE) { | |
976 | telnet->opt_states[o_echo.index] = REQUESTED; | |
977 | send_opt(telnet, o_echo.nsend, o_echo.option); | |
978 | } | |
979 | break; | |
980 | case TS_PING: | |
981 | if (telnet->opt_states[o_they_sga.index] == ACTIVE) { | |
982 | b[1] = NOP; | |
983 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); | |
984 | } | |
985 | break; | |
986 | default: | |
987 | break; /* never heard of it */ | |
988 | } | |
989 | } | |
990 | ||
991 | static const struct telnet_special *telnet_get_specials(void *handle) | |
992 | { | |
993 | static const struct telnet_special specials[] = { | |
994 | {"Are You There", TS_AYT}, | |
995 | {"Break", TS_BRK}, | |
996 | {"Synch", TS_SYNCH}, | |
997 | {"Erase Character", TS_EC}, | |
998 | {"Erase Line", TS_EL}, | |
999 | {"Go Ahead", TS_GA}, | |
1000 | {"No Operation", TS_NOP}, | |
1001 | {NULL, TS_SEP}, | |
1002 | {"Abort Process", TS_ABORT}, | |
1003 | {"Abort Output", TS_AO}, | |
1004 | {"Interrupt Process", TS_IP}, | |
1005 | {"Suspend Process", TS_SUSP}, | |
1006 | {NULL, TS_SEP}, | |
1007 | {"End Of Record", TS_EOR}, | |
1008 | {"End Of File", TS_EOF}, | |
1009 | {NULL, TS_EXITMENU} | |
1010 | }; | |
1011 | return specials; | |
1012 | } | |
1013 | ||
1014 | static int telnet_connected(void *handle) | |
1015 | { | |
1016 | Telnet telnet = (Telnet) handle; | |
1017 | return telnet->s != NULL; | |
1018 | } | |
1019 | ||
1020 | static int telnet_sendok(void *handle) | |
1021 | { | |
1022 | /* Telnet telnet = (Telnet) handle; */ | |
1023 | return 1; | |
1024 | } | |
1025 | ||
1026 | static void telnet_unthrottle(void *handle, int backlog) | |
1027 | { | |
1028 | Telnet telnet = (Telnet) handle; | |
1029 | sk_set_frozen(telnet->s, backlog > TELNET_MAX_BACKLOG); | |
1030 | } | |
1031 | ||
1032 | static int telnet_ldisc(void *handle, int option) | |
1033 | { | |
1034 | Telnet telnet = (Telnet) handle; | |
1035 | if (option == LD_ECHO) | |
1036 | return telnet->echoing; | |
1037 | if (option == LD_EDIT) | |
1038 | return telnet->editing; | |
1039 | return FALSE; | |
1040 | } | |
1041 | ||
1042 | static void telnet_provide_ldisc(void *handle, void *ldisc) | |
1043 | { | |
1044 | Telnet telnet = (Telnet) handle; | |
1045 | telnet->ldisc = ldisc; | |
1046 | } | |
1047 | ||
1048 | static void telnet_provide_logctx(void *handle, void *logctx) | |
1049 | { | |
1050 | /* This is a stub. */ | |
1051 | } | |
1052 | ||
1053 | static int telnet_exitcode(void *handle) | |
1054 | { | |
1055 | Telnet telnet = (Telnet) handle; | |
1056 | if (telnet->s != NULL) | |
1057 | return -1; /* still connected */ | |
1058 | else | |
1059 | /* Telnet doesn't transmit exit codes back to the client */ | |
1060 | return 0; | |
1061 | } | |
1062 | ||
1063 | /* | |
1064 | * cfg_info for Telnet does nothing at all. | |
1065 | */ | |
1066 | static int telnet_cfg_info(void *handle) | |
1067 | { | |
1068 | return 0; | |
1069 | } | |
1070 | ||
1071 | Backend telnet_backend = { | |
1072 | telnet_init, | |
1073 | telnet_free, | |
1074 | telnet_reconfig, | |
1075 | telnet_send, | |
1076 | telnet_sendbuffer, | |
1077 | telnet_size, | |
1078 | telnet_special, | |
1079 | telnet_get_specials, | |
1080 | telnet_connected, | |
1081 | telnet_exitcode, | |
1082 | telnet_sendok, | |
1083 | telnet_ldisc, | |
1084 | telnet_provide_ldisc, | |
1085 | telnet_provide_logctx, | |
1086 | telnet_unthrottle, | |
1087 | telnet_cfg_info, | |
1088 | "telnet", | |
1089 | PROT_TELNET, | |
1090 | 23 | |
1091 | }; |