| 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 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 | |
| 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 */ |
| 80 | #define BSD_VAR 1 |
| 81 | #define BSD_VALUE 0 |
| 82 | #define RFC_VAR 0 |
| 83 | #define RFC_VALUE 1 |
| 84 | |
| 85 | #define CR 13 |
| 86 | #define LF 10 |
| 87 | #define NUL 0 |
| 88 | |
| 89 | #define iswritable(x) \ |
| 90 | ( (x) != IAC && \ |
| 91 | (telnet->opt_states[o_we_bin.index] == ACTIVE || (x) != CR)) |
| 92 | |
| 93 | static char *telopt(int opt) |
| 94 | { |
| 95 | #define i(x) if (opt == TELOPT_ ## x) return #x; |
| 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); |
| 136 | #undef i |
| 137 | return "<unknown>"; |
| 138 | } |
| 139 | |
| 140 | static void telnet_size(void *handle, int width, int height); |
| 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 */ |
| 147 | int index; /* index into telnet->opt_states[] */ |
| 148 | enum { |
| 149 | REQUESTED, ACTIVE, INACTIVE, REALLY_INACTIVE |
| 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, |
| 162 | OPTINDEX_WE_BIN, |
| 163 | OPTINDEX_THEY_BIN, |
| 164 | NUM_OPTS |
| 165 | }; |
| 166 | |
| 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 |
| 175 | }; |
| 176 | static const struct Opt o_nenv = { WILL, WONT, DO, DONT, TELOPT_NEW_ENVIRON, |
| 177 | OPTINDEX_NENV, REQUESTED |
| 178 | }; |
| 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 }; |
| 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 }; |
| 189 | |
| 190 | static const struct Opt *const opts[] = { |
| 191 | &o_naws, &o_tspeed, &o_ttype, &o_oenv, &o_nenv, &o_echo, |
| 192 | &o_we_sga, &o_they_sga, &o_we_bin, &o_they_bin, NULL |
| 193 | }; |
| 194 | |
| 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; |
| 202 | void *ldisc; |
| 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 | |
| 222 | #define TELNET_MAX_BACKLOG 4096 |
| 223 | |
| 224 | #define SB_DELTA 1024 |
| 225 | |
| 226 | static void c_write1(Telnet telnet, int c) |
| 227 | { |
| 228 | int backlog; |
| 229 | char cc = (char) c; |
| 230 | backlog = from_backend(telnet->frontend, 0, &cc, 1); |
| 231 | sk_set_frozen(telnet->s, backlog > TELNET_MAX_BACKLOG); |
| 232 | } |
| 233 | |
| 234 | static void log_option(Telnet telnet, char *sender, int cmd, int option) |
| 235 | { |
| 236 | char *buf; |
| 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 | */ |
| 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)); |
| 246 | logevent(telnet->frontend, buf); |
| 247 | sfree(buf); |
| 248 | } |
| 249 | |
| 250 | static void send_opt(Telnet telnet, int cmd, int option) |
| 251 | { |
| 252 | unsigned char b[3]; |
| 253 | |
| 254 | b[0] = IAC; |
| 255 | b[1] = cmd; |
| 256 | b[2] = option; |
| 257 | telnet->bufsize = sk_write(telnet->s, b, 3); |
| 258 | log_option(telnet, "client", cmd, option); |
| 259 | } |
| 260 | |
| 261 | static void deactivate_option(Telnet telnet, const struct Opt *o) |
| 262 | { |
| 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; |
| 267 | } |
| 268 | |
| 269 | /* |
| 270 | * Generate side effects of enabling or disabling an option. |
| 271 | */ |
| 272 | static void option_side_effects(Telnet telnet, const struct Opt *o, int enabled) |
| 273 | { |
| 274 | if (o->option == TELOPT_ECHO && o->send == DO) |
| 275 | telnet->echoing = !enabled; |
| 276 | else if (o->option == TELOPT_SGA && o->send == DO) |
| 277 | telnet->editing = !enabled; |
| 278 | if (telnet->ldisc) /* cause ldisc to notice the change */ |
| 279 | ldisc_send(telnet->ldisc, NULL, 0, 0); |
| 280 | |
| 281 | /* Ensure we get the minimum options */ |
| 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); |
| 286 | } |
| 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); |
| 290 | } |
| 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); |
| 294 | } |
| 295 | telnet->activated = TRUE; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | static void activate_option(Telnet telnet, const struct Opt *o) |
| 300 | { |
| 301 | if (o->send == WILL && o->option == TELOPT_NAWS) |
| 302 | telnet_size(telnet, telnet->term_width, telnet->term_height); |
| 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 | */ |
| 310 | deactivate_option(telnet, o->option == |
| 311 | TELOPT_NEW_ENVIRON ? &o_oenv : &o_nenv); |
| 312 | } |
| 313 | option_side_effects(telnet, o, 1); |
| 314 | } |
| 315 | |
| 316 | static void refused_option(Telnet telnet, const struct Opt *o) |
| 317 | { |
| 318 | if (o->send == WILL && o->option == TELOPT_NEW_ENVIRON && |
| 319 | telnet->opt_states[o_oenv.index] == INACTIVE) { |
| 320 | send_opt(telnet, WILL, TELOPT_OLD_ENVIRON); |
| 321 | telnet->opt_states[o_oenv.index] = REQUESTED; |
| 322 | } |
| 323 | option_side_effects(telnet, o, 0); |
| 324 | } |
| 325 | |
| 326 | static void proc_rec_opt(Telnet telnet, int cmd, int option) |
| 327 | { |
| 328 | const struct Opt *const *o; |
| 329 | |
| 330 | log_option(telnet, "server", cmd, option); |
| 331 | for (o = opts; *o; o++) { |
| 332 | if ((*o)->option == option && (*o)->ack == cmd) { |
| 333 | switch (telnet->opt_states[(*o)->index]) { |
| 334 | case REQUESTED: |
| 335 | telnet->opt_states[(*o)->index] = ACTIVE; |
| 336 | activate_option(telnet, *o); |
| 337 | break; |
| 338 | case ACTIVE: |
| 339 | break; |
| 340 | case INACTIVE: |
| 341 | telnet->opt_states[(*o)->index] = ACTIVE; |
| 342 | send_opt(telnet, (*o)->send, option); |
| 343 | activate_option(telnet, *o); |
| 344 | break; |
| 345 | case REALLY_INACTIVE: |
| 346 | send_opt(telnet, (*o)->nsend, option); |
| 347 | break; |
| 348 | } |
| 349 | return; |
| 350 | } else if ((*o)->option == option && (*o)->nak == cmd) { |
| 351 | switch (telnet->opt_states[(*o)->index]) { |
| 352 | case REQUESTED: |
| 353 | telnet->opt_states[(*o)->index] = INACTIVE; |
| 354 | refused_option(telnet, *o); |
| 355 | break; |
| 356 | case ACTIVE: |
| 357 | telnet->opt_states[(*o)->index] = INACTIVE; |
| 358 | send_opt(telnet, (*o)->nsend, option); |
| 359 | option_side_effects(telnet, *o, 0); |
| 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 | */ |
| 372 | send_opt(telnet, (cmd == WILL ? DONT : WONT), option); |
| 373 | } |
| 374 | |
| 375 | static void process_subneg(Telnet telnet) |
| 376 | { |
| 377 | unsigned char b[2048], *p, *q; |
| 378 | int var, value, n; |
| 379 | char *e; |
| 380 | |
| 381 | switch (telnet->sb_opt) { |
| 382 | case TELOPT_TSPEED: |
| 383 | if (telnet->sb_len == 1 && telnet->sb_buf[0] == TELQUAL_SEND) { |
| 384 | char *logbuf; |
| 385 | b[0] = IAC; |
| 386 | b[1] = SB; |
| 387 | b[2] = TELOPT_TSPEED; |
| 388 | b[3] = TELQUAL_IS; |
| 389 | strcpy(b + 4, cfg.termspeed); |
| 390 | n = 4 + strlen(cfg.termspeed); |
| 391 | b[n] = IAC; |
| 392 | b[n + 1] = SE; |
| 393 | telnet->bufsize = sk_write(telnet->s, b, n + 2); |
| 394 | logevent(telnet->frontend, "server:\tSB TSPEED SEND"); |
| 395 | logbuf = dupprintf("client:\tSB TSPEED IS %s", cfg.termspeed); |
| 396 | logevent(telnet->frontend, logbuf); |
| 397 | sfree(logbuf); |
| 398 | } else |
| 399 | logevent(telnet->frontend, "server:\tSB TSPEED <something weird>"); |
| 400 | break; |
| 401 | case TELOPT_TTYPE: |
| 402 | if (telnet->sb_len == 1 && telnet->sb_buf[0] == TELQUAL_SEND) { |
| 403 | char *logbuf; |
| 404 | b[0] = IAC; |
| 405 | b[1] = SB; |
| 406 | b[2] = TELOPT_TTYPE; |
| 407 | b[3] = TELQUAL_IS; |
| 408 | for (n = 0; cfg.termtype[n]; n++) |
| 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; |
| 415 | telnet->bufsize = sk_write(telnet->s, b, n + 6); |
| 416 | b[n + 4] = 0; |
| 417 | logevent(telnet->frontend, "server:\tSB TTYPE SEND"); |
| 418 | logbuf = dupprintf("client:\tSB TTYPE IS %s", b + 4); |
| 419 | logevent(telnet->frontend, logbuf); |
| 420 | sfree(logbuf); |
| 421 | } else |
| 422 | logevent(telnet->frontend, "server:\tSB TTYPE <something weird>\r\n"); |
| 423 | break; |
| 424 | case TELOPT_OLD_ENVIRON: |
| 425 | case TELOPT_NEW_ENVIRON: |
| 426 | p = telnet->sb_buf; |
| 427 | q = p + telnet->sb_len; |
| 428 | if (p < q && *p == TELQUAL_SEND) { |
| 429 | char *logbuf; |
| 430 | p++; |
| 431 | logbuf = dupprintf("server:\tSB %s SEND", telopt(telnet->sb_opt)); |
| 432 | logevent(telnet->frontend, logbuf); |
| 433 | sfree(logbuf); |
| 434 | if (telnet->sb_opt == TELOPT_OLD_ENVIRON) { |
| 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 | } |
| 463 | b[0] = IAC; |
| 464 | b[1] = SB; |
| 465 | b[2] = telnet->sb_opt; |
| 466 | b[3] = TELQUAL_IS; |
| 467 | n = 4; |
| 468 | e = cfg.environmt; |
| 469 | while (*e) { |
| 470 | b[n++] = var; |
| 471 | while (*e && *e != '\t') |
| 472 | b[n++] = *e++; |
| 473 | if (*e == '\t') |
| 474 | e++; |
| 475 | b[n++] = value; |
| 476 | while (*e) |
| 477 | b[n++] = *e++; |
| 478 | e++; |
| 479 | } |
| 480 | if (*cfg.username) { |
| 481 | b[n++] = var; |
| 482 | b[n++] = 'U'; |
| 483 | b[n++] = 'S'; |
| 484 | b[n++] = 'E'; |
| 485 | b[n++] = 'R'; |
| 486 | b[n++] = value; |
| 487 | e = cfg.username; |
| 488 | while (*e) |
| 489 | b[n++] = *e++; |
| 490 | } |
| 491 | b[n++] = IAC; |
| 492 | b[n++] = SE; |
| 493 | telnet->bufsize = sk_write(telnet->s, b, n); |
| 494 | logbuf = dupprintf("client:\tSB %s IS %s", telopt(telnet->sb_opt), |
| 495 | n == 6 ? "<nothing>" : "<stuff>"); |
| 496 | logevent(telnet->frontend, logbuf); |
| 497 | sfree(logbuf); |
| 498 | } |
| 499 | break; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | static void do_telnet_read(Telnet telnet, char *buf, int len) |
| 504 | { |
| 505 | |
| 506 | while (len--) { |
| 507 | int c = (unsigned char) *buf++; |
| 508 | |
| 509 | switch (telnet->state) { |
| 510 | case TOP_LEVEL: |
| 511 | case SEENCR: |
| 512 | if (c == NUL && telnet->state == SEENCR) |
| 513 | telnet->state = TOP_LEVEL; |
| 514 | else if (c == IAC) |
| 515 | telnet->state = SEENIAC; |
| 516 | else { |
| 517 | if (!telnet->in_synch) |
| 518 | c_write1(telnet, c); |
| 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 | */ |
| 529 | else if (c == DM) |
| 530 | telnet->in_synch = 0; |
| 531 | #endif |
| 532 | if (c == CR && telnet->opt_states[o_they_bin.index] != ACTIVE) |
| 533 | telnet->state = SEENCR; |
| 534 | else |
| 535 | telnet->state = TOP_LEVEL; |
| 536 | } |
| 537 | break; |
| 538 | case SEENIAC: |
| 539 | if (c == DO) |
| 540 | telnet->state = SEENDO; |
| 541 | else if (c == DONT) |
| 542 | telnet->state = SEENDONT; |
| 543 | else if (c == WILL) |
| 544 | telnet->state = SEENWILL; |
| 545 | else if (c == WONT) |
| 546 | telnet->state = SEENWONT; |
| 547 | else if (c == SB) |
| 548 | telnet->state = SEENSB; |
| 549 | else if (c == DM) { |
| 550 | telnet->in_synch = 0; |
| 551 | telnet->state = TOP_LEVEL; |
| 552 | } else { |
| 553 | /* ignore everything else; print it if it's IAC */ |
| 554 | if (c == IAC) { |
| 555 | c_write1(telnet, c); |
| 556 | } |
| 557 | telnet->state = TOP_LEVEL; |
| 558 | } |
| 559 | break; |
| 560 | case SEENWILL: |
| 561 | proc_rec_opt(telnet, WILL, c); |
| 562 | telnet->state = TOP_LEVEL; |
| 563 | break; |
| 564 | case SEENWONT: |
| 565 | proc_rec_opt(telnet, WONT, c); |
| 566 | telnet->state = TOP_LEVEL; |
| 567 | break; |
| 568 | case SEENDO: |
| 569 | proc_rec_opt(telnet, DO, c); |
| 570 | telnet->state = TOP_LEVEL; |
| 571 | break; |
| 572 | case SEENDONT: |
| 573 | proc_rec_opt(telnet, DONT, c); |
| 574 | telnet->state = TOP_LEVEL; |
| 575 | break; |
| 576 | case SEENSB: |
| 577 | telnet->sb_opt = c; |
| 578 | telnet->sb_len = 0; |
| 579 | telnet->state = SUBNEGOT; |
| 580 | break; |
| 581 | case SUBNEGOT: |
| 582 | if (c == IAC) |
| 583 | telnet->state = SUBNEG_IAC; |
| 584 | else { |
| 585 | subneg_addchar: |
| 586 | if (telnet->sb_len >= telnet->sb_size) { |
| 587 | char *newbuf; |
| 588 | telnet->sb_size += SB_DELTA; |
| 589 | newbuf = (telnet->sb_buf ? |
| 590 | srealloc(telnet->sb_buf, telnet->sb_size) : |
| 591 | smalloc(telnet->sb_size)); |
| 592 | if (newbuf) |
| 593 | telnet->sb_buf = newbuf; |
| 594 | else |
| 595 | telnet->sb_size -= SB_DELTA; |
| 596 | } |
| 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 */ |
| 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 { |
| 606 | process_subneg(telnet); |
| 607 | telnet->state = TOP_LEVEL; |
| 608 | } |
| 609 | break; |
| 610 | } |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | static int telnet_closing(Plug plug, char *error_msg, int error_code, |
| 615 | int calling_back) |
| 616 | { |
| 617 | Telnet telnet = (Telnet) plug; |
| 618 | |
| 619 | if (telnet->s) { |
| 620 | sk_close(telnet->s); |
| 621 | telnet->s = NULL; |
| 622 | } |
| 623 | if (error_msg) { |
| 624 | /* A socket error has occurred. */ |
| 625 | logevent(telnet->frontend, error_msg); |
| 626 | connection_fatal("%s", error_msg); |
| 627 | } /* Otherwise, the remote side closed the connection normally. */ |
| 628 | return 0; |
| 629 | } |
| 630 | |
| 631 | static int telnet_receive(Plug plug, int urgent, char *data, int len) |
| 632 | { |
| 633 | Telnet telnet = (Telnet) plug; |
| 634 | if (urgent) |
| 635 | telnet->in_synch = TRUE; |
| 636 | do_telnet_read(telnet, data, len); |
| 637 | return 1; |
| 638 | } |
| 639 | |
| 640 | static void telnet_sent(Plug plug, int bufsize) |
| 641 | { |
| 642 | Telnet telnet = (Telnet) plug; |
| 643 | telnet->bufsize = bufsize; |
| 644 | } |
| 645 | |
| 646 | /* |
| 647 | * Called to set up the Telnet connection. |
| 648 | * |
| 649 | * Returns an error message, or NULL on success. |
| 650 | * |
| 651 | * Also places the canonical host name into `realhost'. It must be |
| 652 | * freed by the caller. |
| 653 | */ |
| 654 | static char *telnet_init(void *frontend_handle, void **backend_handle, |
| 655 | char *host, int port, char **realhost, int nodelay) |
| 656 | { |
| 657 | static const struct plug_function_table fn_table = { |
| 658 | telnet_closing, |
| 659 | telnet_receive, |
| 660 | telnet_sent |
| 661 | }; |
| 662 | SockAddr addr; |
| 663 | char *err; |
| 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; |
| 679 | |
| 680 | /* |
| 681 | * Try to find host. |
| 682 | */ |
| 683 | { |
| 684 | char *buf; |
| 685 | buf = dupprintf("Looking up host \"%s\"", host); |
| 686 | logevent(telnet->frontend, buf); |
| 687 | sfree(buf); |
| 688 | } |
| 689 | addr = name_lookup(host, port, realhost); |
| 690 | if ((err = sk_addr_error(addr))) |
| 691 | return err; |
| 692 | |
| 693 | if (port < 0) |
| 694 | port = 23; /* default telnet port */ |
| 695 | |
| 696 | /* |
| 697 | * Open socket. |
| 698 | */ |
| 699 | { |
| 700 | char *buf, addrbuf[100]; |
| 701 | sk_getaddr(addr, addrbuf, 100); |
| 702 | buf = dupprintf("Connecting to %s port %d", addrbuf, port); |
| 703 | logevent(telnet->frontend, buf); |
| 704 | sfree(buf); |
| 705 | } |
| 706 | telnet->s = new_connection(addr, *realhost, port, 0, 1, |
| 707 | nodelay, (Plug) telnet); |
| 708 | if ((err = sk_socket_error(telnet->s))) |
| 709 | return err; |
| 710 | |
| 711 | sk_addr_free(addr); |
| 712 | |
| 713 | /* |
| 714 | * Initialise option states. |
| 715 | */ |
| 716 | if (cfg.passive_telnet) { |
| 717 | const struct Opt *const *o; |
| 718 | |
| 719 | for (o = opts; *o; o++) |
| 720 | telnet->opt_states[(*o)->index] = INACTIVE; |
| 721 | } else { |
| 722 | const struct Opt *const *o; |
| 723 | |
| 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; |
| 730 | } |
| 731 | |
| 732 | /* |
| 733 | * Set up SYNCH state. |
| 734 | */ |
| 735 | telnet->in_synch = FALSE; |
| 736 | |
| 737 | return NULL; |
| 738 | } |
| 739 | |
| 740 | /* |
| 741 | * Called to send data down the Telnet connection. |
| 742 | */ |
| 743 | static int telnet_send(void *handle, char *buf, int len) |
| 744 | { |
| 745 | Telnet telnet = (Telnet) handle; |
| 746 | char *p; |
| 747 | static unsigned char iac[2] = { IAC, IAC }; |
| 748 | static unsigned char cr[2] = { CR, NUL }; |
| 749 | #if 0 |
| 750 | static unsigned char nl[2] = { CR, LF }; |
| 751 | #endif |
| 752 | |
| 753 | if (telnet->s == NULL) |
| 754 | return 0; |
| 755 | |
| 756 | p = buf; |
| 757 | while (p < buf + len) { |
| 758 | char *q = p; |
| 759 | |
| 760 | while (p < buf + len && iswritable((unsigned char) *p)) |
| 761 | p++; |
| 762 | telnet->bufsize = sk_write(telnet->s, q, p - q); |
| 763 | |
| 764 | while (p < buf + len && !iswritable((unsigned char) *p)) { |
| 765 | telnet->bufsize = |
| 766 | sk_write(telnet->s, (unsigned char) *p == IAC ? iac : cr, 2); |
| 767 | p++; |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | return telnet->bufsize; |
| 772 | } |
| 773 | |
| 774 | /* |
| 775 | * Called to query the current socket sendability status. |
| 776 | */ |
| 777 | static int telnet_sendbuffer(void *handle) |
| 778 | { |
| 779 | Telnet telnet = (Telnet) handle; |
| 780 | return telnet->bufsize; |
| 781 | } |
| 782 | |
| 783 | /* |
| 784 | * Called to set the size of the window from Telnet's POV. |
| 785 | */ |
| 786 | static void telnet_size(void *handle, int width, int height) |
| 787 | { |
| 788 | Telnet telnet = (Telnet) handle; |
| 789 | unsigned char b[16]; |
| 790 | char *logbuf; |
| 791 | |
| 792 | telnet->term_width = width; |
| 793 | telnet->term_height = height; |
| 794 | |
| 795 | if (telnet->s == NULL || telnet->opt_states[o_naws.index] != ACTIVE) |
| 796 | return; |
| 797 | b[0] = IAC; |
| 798 | b[1] = SB; |
| 799 | b[2] = TELOPT_NAWS; |
| 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; |
| 804 | b[7] = IAC; |
| 805 | b[8] = SE; |
| 806 | telnet->bufsize = sk_write(telnet->s, b, 9); |
| 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]); |
| 810 | logevent(telnet->frontend, logbuf); |
| 811 | sfree(logbuf); |
| 812 | } |
| 813 | |
| 814 | /* |
| 815 | * Send Telnet special codes. |
| 816 | */ |
| 817 | static void telnet_special(void *handle, Telnet_Special code) |
| 818 | { |
| 819 | Telnet telnet = (Telnet) handle; |
| 820 | unsigned char b[2]; |
| 821 | |
| 822 | if (telnet->s == NULL) |
| 823 | return; |
| 824 | |
| 825 | b[0] = IAC; |
| 826 | switch (code) { |
| 827 | case TS_AYT: |
| 828 | b[1] = AYT; |
| 829 | telnet->bufsize = sk_write(telnet->s, b, 2); |
| 830 | break; |
| 831 | case TS_BRK: |
| 832 | b[1] = BREAK; |
| 833 | telnet->bufsize = sk_write(telnet->s, b, 2); |
| 834 | break; |
| 835 | case TS_EC: |
| 836 | b[1] = EC; |
| 837 | telnet->bufsize = sk_write(telnet->s, b, 2); |
| 838 | break; |
| 839 | case TS_EL: |
| 840 | b[1] = EL; |
| 841 | telnet->bufsize = sk_write(telnet->s, b, 2); |
| 842 | break; |
| 843 | case TS_GA: |
| 844 | b[1] = GA; |
| 845 | telnet->bufsize = sk_write(telnet->s, b, 2); |
| 846 | break; |
| 847 | case TS_NOP: |
| 848 | b[1] = NOP; |
| 849 | telnet->bufsize = sk_write(telnet->s, b, 2); |
| 850 | break; |
| 851 | case TS_ABORT: |
| 852 | b[1] = ABORT; |
| 853 | telnet->bufsize = sk_write(telnet->s, b, 2); |
| 854 | break; |
| 855 | case TS_AO: |
| 856 | b[1] = AO; |
| 857 | telnet->bufsize = sk_write(telnet->s, b, 2); |
| 858 | break; |
| 859 | case TS_IP: |
| 860 | b[1] = IP; |
| 861 | telnet->bufsize = sk_write(telnet->s, b, 2); |
| 862 | break; |
| 863 | case TS_SUSP: |
| 864 | b[1] = SUSP; |
| 865 | telnet->bufsize = sk_write(telnet->s, b, 2); |
| 866 | break; |
| 867 | case TS_EOR: |
| 868 | b[1] = EOR; |
| 869 | telnet->bufsize = sk_write(telnet->s, b, 2); |
| 870 | break; |
| 871 | case TS_EOF: |
| 872 | b[1] = xEOF; |
| 873 | telnet->bufsize = sk_write(telnet->s, b, 2); |
| 874 | break; |
| 875 | case TS_EOL: |
| 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); |
| 881 | break; |
| 882 | case TS_SYNCH: |
| 883 | b[1] = DM; |
| 884 | telnet->bufsize = sk_write(telnet->s, b, 1); |
| 885 | telnet->bufsize = sk_write_oob(telnet->s, b + 1, 1); |
| 886 | break; |
| 887 | case TS_RECHO: |
| 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); |
| 892 | } |
| 893 | break; |
| 894 | case TS_LECHO: |
| 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); |
| 898 | } |
| 899 | break; |
| 900 | case TS_PING: |
| 901 | if (telnet->opt_states[o_they_sga.index] == ACTIVE) { |
| 902 | b[1] = NOP; |
| 903 | telnet->bufsize = sk_write(telnet->s, b, 2); |
| 904 | } |
| 905 | break; |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | static Socket telnet_socket(void *handle) |
| 910 | { |
| 911 | Telnet telnet = (Telnet) handle; |
| 912 | return telnet->s; |
| 913 | } |
| 914 | |
| 915 | static int telnet_sendok(void *handle) |
| 916 | { |
| 917 | /* Telnet telnet = (Telnet) handle; */ |
| 918 | return 1; |
| 919 | } |
| 920 | |
| 921 | static void telnet_unthrottle(void *handle, int backlog) |
| 922 | { |
| 923 | Telnet telnet = (Telnet) handle; |
| 924 | sk_set_frozen(telnet->s, backlog > TELNET_MAX_BACKLOG); |
| 925 | } |
| 926 | |
| 927 | static int telnet_ldisc(void *handle, int option) |
| 928 | { |
| 929 | Telnet telnet = (Telnet) handle; |
| 930 | if (option == LD_ECHO) |
| 931 | return telnet->echoing; |
| 932 | if (option == LD_EDIT) |
| 933 | return telnet->editing; |
| 934 | return FALSE; |
| 935 | } |
| 936 | |
| 937 | static void telnet_provide_ldisc(void *handle, void *ldisc) |
| 938 | { |
| 939 | Telnet telnet = (Telnet) handle; |
| 940 | telnet->ldisc = ldisc; |
| 941 | } |
| 942 | |
| 943 | static void telnet_provide_logctx(void *handle, void *logctx) |
| 944 | { |
| 945 | /* This is a stub. */ |
| 946 | } |
| 947 | |
| 948 | static int telnet_exitcode(void *handle) |
| 949 | { |
| 950 | /* Telnet telnet = (Telnet) handle; */ |
| 951 | /* Telnet doesn't transmit exit codes back to the client */ |
| 952 | return 0; |
| 953 | } |
| 954 | |
| 955 | Backend telnet_backend = { |
| 956 | telnet_init, |
| 957 | telnet_send, |
| 958 | telnet_sendbuffer, |
| 959 | telnet_size, |
| 960 | telnet_special, |
| 961 | telnet_socket, |
| 962 | telnet_exitcode, |
| 963 | telnet_sendok, |
| 964 | telnet_ldisc, |
| 965 | telnet_provide_ldisc, |
| 966 | telnet_provide_logctx, |
| 967 | telnet_unthrottle, |
| 968 | 23 |
| 969 | }; |