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