| 1 | #include <windows.h> |
| 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | |
| 5 | #include "putty.h" |
| 6 | |
| 7 | #ifndef FALSE |
| 8 | #define FALSE 0 |
| 9 | #endif |
| 10 | #ifndef TRUE |
| 11 | #define TRUE 1 |
| 12 | #endif |
| 13 | |
| 14 | static Socket s = NULL; |
| 15 | |
| 16 | #define IAC 255 /* interpret as command: */ |
| 17 | #define DONT 254 /* you are not to use option */ |
| 18 | #define DO 253 /* please, you use option */ |
| 19 | #define WONT 252 /* I won't use option */ |
| 20 | #define WILL 251 /* I will use option */ |
| 21 | #define SB 250 /* interpret as subnegotiation */ |
| 22 | #define SE 240 /* end sub negotiation */ |
| 23 | |
| 24 | #define GA 249 /* you may reverse the line */ |
| 25 | #define EL 248 /* erase the current line */ |
| 26 | #define EC 247 /* erase the current character */ |
| 27 | #define AYT 246 /* are you there */ |
| 28 | #define AO 245 /* abort output--but let prog finish */ |
| 29 | #define IP 244 /* interrupt process--permanently */ |
| 30 | #define BREAK 243 /* break */ |
| 31 | #define DM 242 /* data mark--for connect. cleaning */ |
| 32 | #define NOP 241 /* nop */ |
| 33 | #define EOR 239 /* end of record (transparent mode) */ |
| 34 | #define ABORT 238 /* Abort process */ |
| 35 | #define SUSP 237 /* Suspend process */ |
| 36 | #define xEOF 236 /* End of file: EOF is already used... */ |
| 37 | |
| 38 | #define TELOPT_BINARY 0 /* 8-bit data path */ |
| 39 | #define TELOPT_ECHO 1 /* echo */ |
| 40 | #define TELOPT_RCP 2 /* prepare to reconnect */ |
| 41 | #define TELOPT_SGA 3 /* suppress go ahead */ |
| 42 | #define TELOPT_NAMS 4 /* approximate message size */ |
| 43 | #define TELOPT_STATUS 5 /* give status */ |
| 44 | #define TELOPT_TM 6 /* timing mark */ |
| 45 | #define TELOPT_RCTE 7 /* remote controlled transmission and echo */ |
| 46 | #define TELOPT_NAOL 8 /* negotiate about output line width */ |
| 47 | #define TELOPT_NAOP 9 /* negotiate about output page size */ |
| 48 | #define TELOPT_NAOCRD 10 /* negotiate about CR disposition */ |
| 49 | #define TELOPT_NAOHTS 11 /* negotiate about horizontal tabstops */ |
| 50 | #define TELOPT_NAOHTD 12 /* negotiate about horizontal tab disposition */ |
| 51 | #define TELOPT_NAOFFD 13 /* negotiate about formfeed disposition */ |
| 52 | #define TELOPT_NAOVTS 14 /* negotiate about vertical tab stops */ |
| 53 | #define TELOPT_NAOVTD 15 /* negotiate about vertical tab disposition */ |
| 54 | #define TELOPT_NAOLFD 16 /* negotiate about output LF disposition */ |
| 55 | #define TELOPT_XASCII 17 /* extended ascic character set */ |
| 56 | #define TELOPT_LOGOUT 18 /* force logout */ |
| 57 | #define TELOPT_BM 19 /* byte macro */ |
| 58 | #define TELOPT_DET 20 /* data entry terminal */ |
| 59 | #define TELOPT_SUPDUP 21 /* supdup protocol */ |
| 60 | #define TELOPT_SUPDUPOUTPUT 22 /* supdup output */ |
| 61 | #define TELOPT_SNDLOC 23 /* send location */ |
| 62 | #define TELOPT_TTYPE 24 /* terminal type */ |
| 63 | #define TELOPT_EOR 25 /* end or record */ |
| 64 | #define TELOPT_TUID 26 /* TACACS user identification */ |
| 65 | #define TELOPT_OUTMRK 27 /* output marking */ |
| 66 | #define TELOPT_TTYLOC 28 /* terminal location number */ |
| 67 | #define TELOPT_3270REGIME 29 /* 3270 regime */ |
| 68 | #define TELOPT_X3PAD 30 /* X.3 PAD */ |
| 69 | #define TELOPT_NAWS 31 /* window size */ |
| 70 | #define TELOPT_TSPEED 32 /* terminal speed */ |
| 71 | #define TELOPT_LFLOW 33 /* remote flow control */ |
| 72 | #define TELOPT_LINEMODE 34 /* Linemode option */ |
| 73 | #define TELOPT_XDISPLOC 35 /* X Display Location */ |
| 74 | #define TELOPT_OLD_ENVIRON 36 /* Old - Environment variables */ |
| 75 | #define TELOPT_AUTHENTICATION 37 /* Authenticate */ |
| 76 | #define TELOPT_ENCRYPT 38 /* Encryption option */ |
| 77 | #define TELOPT_NEW_ENVIRON 39 /* New - Environment variables */ |
| 78 | #define TELOPT_EXOPL 255 /* extended-options-list */ |
| 79 | |
| 80 | #define TELQUAL_IS 0 /* option is... */ |
| 81 | #define TELQUAL_SEND 1 /* send option */ |
| 82 | #define TELQUAL_INFO 2 /* ENVIRON: informational version of IS */ |
| 83 | #define BSD_VAR 1 |
| 84 | #define BSD_VALUE 0 |
| 85 | #define RFC_VAR 0 |
| 86 | #define RFC_VALUE 1 |
| 87 | |
| 88 | #define CR 13 |
| 89 | #define LF 10 |
| 90 | #define NUL 0 |
| 91 | |
| 92 | #define iswritable(x) ( (x) != IAC && (x) != CR ) |
| 93 | |
| 94 | static char *telopt(int opt) |
| 95 | { |
| 96 | #define i(x) if (opt == TELOPT_ ## x) return #x; |
| 97 | i(BINARY); |
| 98 | i(ECHO); |
| 99 | i(RCP); |
| 100 | i(SGA); |
| 101 | i(NAMS); |
| 102 | i(STATUS); |
| 103 | i(TM); |
| 104 | i(RCTE); |
| 105 | i(NAOL); |
| 106 | i(NAOP); |
| 107 | i(NAOCRD); |
| 108 | i(NAOHTS); |
| 109 | i(NAOHTD); |
| 110 | i(NAOFFD); |
| 111 | i(NAOVTS); |
| 112 | i(NAOVTD); |
| 113 | i(NAOLFD); |
| 114 | i(XASCII); |
| 115 | i(LOGOUT); |
| 116 | i(BM); |
| 117 | i(DET); |
| 118 | i(SUPDUP); |
| 119 | i(SUPDUPOUTPUT); |
| 120 | i(SNDLOC); |
| 121 | i(TTYPE); |
| 122 | i(EOR); |
| 123 | i(TUID); |
| 124 | i(OUTMRK); |
| 125 | i(TTYLOC); |
| 126 | i(X3PAD); |
| 127 | i(NAWS); |
| 128 | i(TSPEED); |
| 129 | i(LFLOW); |
| 130 | i(LINEMODE); |
| 131 | i(XDISPLOC); |
| 132 | i(OLD_ENVIRON); |
| 133 | i(AUTHENTICATION); |
| 134 | i(ENCRYPT); |
| 135 | i(NEW_ENVIRON); |
| 136 | i(EXOPL); |
| 137 | #undef i |
| 138 | return "<unknown>"; |
| 139 | } |
| 140 | |
| 141 | static void telnet_size(void); |
| 142 | |
| 143 | struct Opt { |
| 144 | int send; /* what we initially send */ |
| 145 | int nsend; /* -ve send if requested to stop it */ |
| 146 | int ack, nak; /* +ve and -ve acknowledgements */ |
| 147 | int option; /* the option code */ |
| 148 | enum { |
| 149 | REQUESTED, ACTIVE, INACTIVE, REALLY_INACTIVE |
| 150 | } state; |
| 151 | }; |
| 152 | |
| 153 | static struct Opt o_naws = |
| 154 | { WILL, WONT, DO, DONT, TELOPT_NAWS, REQUESTED }; |
| 155 | static struct Opt o_tspeed = |
| 156 | { WILL, WONT, DO, DONT, TELOPT_TSPEED, REQUESTED }; |
| 157 | static struct Opt o_ttype = |
| 158 | { WILL, WONT, DO, DONT, TELOPT_TTYPE, REQUESTED }; |
| 159 | static struct Opt o_oenv = { WILL, WONT, DO, DONT, TELOPT_OLD_ENVIRON, |
| 160 | INACTIVE |
| 161 | }; |
| 162 | static struct Opt o_nenv = { WILL, WONT, DO, DONT, TELOPT_NEW_ENVIRON, |
| 163 | REQUESTED |
| 164 | }; |
| 165 | static struct Opt o_echo = |
| 166 | { DO, DONT, WILL, WONT, TELOPT_ECHO, REQUESTED }; |
| 167 | static struct Opt o_we_sga = |
| 168 | { WILL, WONT, DO, DONT, TELOPT_SGA, REQUESTED }; |
| 169 | static struct Opt o_they_sga = |
| 170 | { DO, DONT, WILL, WONT, TELOPT_SGA, REQUESTED }; |
| 171 | |
| 172 | static struct Opt *opts[] = { |
| 173 | &o_naws, &o_tspeed, &o_ttype, &o_oenv, &o_nenv, &o_echo, |
| 174 | &o_we_sga, &o_they_sga, NULL |
| 175 | }; |
| 176 | |
| 177 | #define TELNET_MAX_BACKLOG 4096 |
| 178 | |
| 179 | static int echoing = TRUE, editing = TRUE; |
| 180 | static int activated = FALSE; |
| 181 | static int telnet_bufsize; |
| 182 | static int in_synch; |
| 183 | static int sb_opt, sb_len; |
| 184 | static char *sb_buf = NULL; |
| 185 | static int sb_size = 0; |
| 186 | #define SB_DELTA 1024 |
| 187 | |
| 188 | static void c_write1(int c) |
| 189 | { |
| 190 | int backlog; |
| 191 | char cc = (char) c; |
| 192 | backlog = from_backend(0, &cc, 1); |
| 193 | sk_set_frozen(s, backlog > TELNET_MAX_BACKLOG); |
| 194 | } |
| 195 | |
| 196 | static void log_option(char *sender, int cmd, int option) |
| 197 | { |
| 198 | char buf[50]; |
| 199 | sprintf(buf, "%s:\t%s %s", sender, |
| 200 | (cmd == WILL ? "WILL" : cmd == WONT ? "WONT" : |
| 201 | cmd == DO ? "DO" : cmd == DONT ? "DONT" : "<??>"), |
| 202 | telopt(option)); |
| 203 | logevent(buf); |
| 204 | } |
| 205 | |
| 206 | static void send_opt(int cmd, int option) |
| 207 | { |
| 208 | unsigned char b[3]; |
| 209 | |
| 210 | b[0] = IAC; |
| 211 | b[1] = cmd; |
| 212 | b[2] = option; |
| 213 | telnet_bufsize = sk_write(s, b, 3); |
| 214 | log_option("client", cmd, option); |
| 215 | } |
| 216 | |
| 217 | static void deactivate_option(struct Opt *o) |
| 218 | { |
| 219 | if (o->state == REQUESTED || o->state == ACTIVE) |
| 220 | send_opt(o->nsend, o->option); |
| 221 | o->state = REALLY_INACTIVE; |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * Generate side effects of enabling or disabling an option. |
| 226 | */ |
| 227 | static void option_side_effects(struct Opt *o, int enabled) |
| 228 | { |
| 229 | if (o->option == TELOPT_ECHO && o->send == DO) |
| 230 | echoing = !enabled; |
| 231 | else if (o->option == TELOPT_SGA && o->send == DO) |
| 232 | editing = !enabled; |
| 233 | ldisc_send(NULL, 0); /* cause ldisc to notice the change */ |
| 234 | |
| 235 | /* Ensure we get the minimum options */ |
| 236 | if (!activated) { |
| 237 | if (o_echo.state == INACTIVE) { |
| 238 | o_echo.state = REQUESTED; |
| 239 | send_opt(o_echo.send, o_echo.option); |
| 240 | } |
| 241 | if (o_we_sga.state == INACTIVE) { |
| 242 | o_we_sga.state = REQUESTED; |
| 243 | send_opt(o_we_sga.send, o_we_sga.option); |
| 244 | } |
| 245 | if (o_they_sga.state == INACTIVE) { |
| 246 | o_they_sga.state = REQUESTED; |
| 247 | send_opt(o_they_sga.send, o_they_sga.option); |
| 248 | } |
| 249 | activated = TRUE; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | static void activate_option(struct Opt *o) |
| 254 | { |
| 255 | if (o->send == WILL && o->option == TELOPT_NAWS) |
| 256 | telnet_size(); |
| 257 | if (o->send == WILL && |
| 258 | (o->option == TELOPT_NEW_ENVIRON || |
| 259 | o->option == TELOPT_OLD_ENVIRON)) { |
| 260 | /* |
| 261 | * We may only have one kind of ENVIRON going at a time. |
| 262 | * This is a hack, but who cares. |
| 263 | */ |
| 264 | deactivate_option(o->option == |
| 265 | TELOPT_NEW_ENVIRON ? &o_oenv : &o_nenv); |
| 266 | } |
| 267 | option_side_effects(o, 1); |
| 268 | } |
| 269 | |
| 270 | static void refused_option(struct Opt *o) |
| 271 | { |
| 272 | if (o->send == WILL && o->option == TELOPT_NEW_ENVIRON && |
| 273 | o_oenv.state == INACTIVE) { |
| 274 | send_opt(WILL, TELOPT_OLD_ENVIRON); |
| 275 | o_oenv.state = REQUESTED; |
| 276 | } |
| 277 | option_side_effects(o, 0); |
| 278 | } |
| 279 | |
| 280 | static void proc_rec_opt(int cmd, int option) |
| 281 | { |
| 282 | struct Opt **o; |
| 283 | |
| 284 | log_option("server", cmd, option); |
| 285 | for (o = opts; *o; o++) { |
| 286 | if ((*o)->option == option && (*o)->ack == cmd) { |
| 287 | switch ((*o)->state) { |
| 288 | case REQUESTED: |
| 289 | (*o)->state = ACTIVE; |
| 290 | activate_option(*o); |
| 291 | break; |
| 292 | case ACTIVE: |
| 293 | break; |
| 294 | case INACTIVE: |
| 295 | (*o)->state = ACTIVE; |
| 296 | send_opt((*o)->send, option); |
| 297 | activate_option(*o); |
| 298 | break; |
| 299 | case REALLY_INACTIVE: |
| 300 | send_opt((*o)->nsend, option); |
| 301 | break; |
| 302 | } |
| 303 | return; |
| 304 | } else if ((*o)->option == option && (*o)->nak == cmd) { |
| 305 | switch ((*o)->state) { |
| 306 | case REQUESTED: |
| 307 | (*o)->state = INACTIVE; |
| 308 | refused_option(*o); |
| 309 | break; |
| 310 | case ACTIVE: |
| 311 | (*o)->state = INACTIVE; |
| 312 | send_opt((*o)->nsend, option); |
| 313 | option_side_effects(*o, 0); |
| 314 | break; |
| 315 | case INACTIVE: |
| 316 | case REALLY_INACTIVE: |
| 317 | break; |
| 318 | } |
| 319 | return; |
| 320 | } |
| 321 | } |
| 322 | /* |
| 323 | * If we reach here, the option was one we weren't prepared to |
| 324 | * cope with. So send a negative ack. |
| 325 | */ |
| 326 | send_opt((cmd == WILL ? DONT : WONT), option); |
| 327 | } |
| 328 | |
| 329 | static void process_subneg(void) |
| 330 | { |
| 331 | unsigned char b[2048], *p, *q; |
| 332 | int var, value, n; |
| 333 | char *e; |
| 334 | |
| 335 | switch (sb_opt) { |
| 336 | case TELOPT_TSPEED: |
| 337 | if (sb_len == 1 && sb_buf[0] == TELQUAL_SEND) { |
| 338 | char logbuf[sizeof(cfg.termspeed) + 80]; |
| 339 | b[0] = IAC; |
| 340 | b[1] = SB; |
| 341 | b[2] = TELOPT_TSPEED; |
| 342 | b[3] = TELQUAL_IS; |
| 343 | strcpy(b + 4, cfg.termspeed); |
| 344 | n = 4 + strlen(cfg.termspeed); |
| 345 | b[n] = IAC; |
| 346 | b[n + 1] = SE; |
| 347 | telnet_bufsize = sk_write(s, b, n + 2); |
| 348 | logevent("server:\tSB TSPEED SEND"); |
| 349 | sprintf(logbuf, "client:\tSB TSPEED IS %s", cfg.termspeed); |
| 350 | logevent(logbuf); |
| 351 | } else |
| 352 | logevent("server:\tSB TSPEED <something weird>"); |
| 353 | break; |
| 354 | case TELOPT_TTYPE: |
| 355 | if (sb_len == 1 && sb_buf[0] == TELQUAL_SEND) { |
| 356 | char logbuf[sizeof(cfg.termtype) + 80]; |
| 357 | b[0] = IAC; |
| 358 | b[1] = SB; |
| 359 | b[2] = TELOPT_TTYPE; |
| 360 | b[3] = TELQUAL_IS; |
| 361 | for (n = 0; cfg.termtype[n]; n++) |
| 362 | b[n + 4] = (cfg.termtype[n] >= 'a' |
| 363 | && cfg.termtype[n] <= |
| 364 | 'z' ? cfg.termtype[n] + 'A' - |
| 365 | 'a' : cfg.termtype[n]); |
| 366 | b[n + 4] = IAC; |
| 367 | b[n + 5] = SE; |
| 368 | telnet_bufsize = sk_write(s, b, n + 6); |
| 369 | b[n + 4] = 0; |
| 370 | logevent("server:\tSB TTYPE SEND"); |
| 371 | sprintf(logbuf, "client:\tSB TTYPE IS %s", b + 4); |
| 372 | logevent(logbuf); |
| 373 | } else |
| 374 | logevent("server:\tSB TTYPE <something weird>\r\n"); |
| 375 | break; |
| 376 | case TELOPT_OLD_ENVIRON: |
| 377 | case TELOPT_NEW_ENVIRON: |
| 378 | p = sb_buf; |
| 379 | q = p + sb_len; |
| 380 | if (p < q && *p == TELQUAL_SEND) { |
| 381 | char logbuf[50]; |
| 382 | p++; |
| 383 | sprintf(logbuf, "server:\tSB %s SEND", telopt(sb_opt)); |
| 384 | logevent(logbuf); |
| 385 | if (sb_opt == TELOPT_OLD_ENVIRON) { |
| 386 | if (cfg.rfc_environ) { |
| 387 | value = RFC_VALUE; |
| 388 | var = RFC_VAR; |
| 389 | } else { |
| 390 | value = BSD_VALUE; |
| 391 | var = BSD_VAR; |
| 392 | } |
| 393 | /* |
| 394 | * Try to guess the sense of VAR and VALUE. |
| 395 | */ |
| 396 | while (p < q) { |
| 397 | if (*p == RFC_VAR) { |
| 398 | value = RFC_VALUE; |
| 399 | var = RFC_VAR; |
| 400 | } else if (*p == BSD_VAR) { |
| 401 | value = BSD_VALUE; |
| 402 | var = BSD_VAR; |
| 403 | } |
| 404 | p++; |
| 405 | } |
| 406 | } else { |
| 407 | /* |
| 408 | * With NEW_ENVIRON, the sense of VAR and VALUE |
| 409 | * isn't in doubt. |
| 410 | */ |
| 411 | value = RFC_VALUE; |
| 412 | var = RFC_VAR; |
| 413 | } |
| 414 | b[0] = IAC; |
| 415 | b[1] = SB; |
| 416 | b[2] = sb_opt; |
| 417 | b[3] = TELQUAL_IS; |
| 418 | n = 4; |
| 419 | e = cfg.environmt; |
| 420 | while (*e) { |
| 421 | b[n++] = var; |
| 422 | while (*e && *e != '\t') |
| 423 | b[n++] = *e++; |
| 424 | if (*e == '\t') |
| 425 | e++; |
| 426 | b[n++] = value; |
| 427 | while (*e) |
| 428 | b[n++] = *e++; |
| 429 | e++; |
| 430 | } |
| 431 | if (*cfg.username) { |
| 432 | b[n++] = var; |
| 433 | b[n++] = 'U'; |
| 434 | b[n++] = 'S'; |
| 435 | b[n++] = 'E'; |
| 436 | b[n++] = 'R'; |
| 437 | b[n++] = value; |
| 438 | e = cfg.username; |
| 439 | while (*e) |
| 440 | b[n++] = *e++; |
| 441 | } |
| 442 | b[n++] = IAC; |
| 443 | b[n++] = SE; |
| 444 | telnet_bufsize = sk_write(s, b, n); |
| 445 | sprintf(logbuf, "client:\tSB %s IS %s", telopt(sb_opt), |
| 446 | n == 6 ? "<nothing>" : "<stuff>"); |
| 447 | logevent(logbuf); |
| 448 | } |
| 449 | break; |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | static enum { |
| 454 | TOPLEVEL, SEENIAC, SEENWILL, SEENWONT, SEENDO, SEENDONT, |
| 455 | SEENSB, SUBNEGOT, SUBNEG_IAC, SEENCR |
| 456 | } telnet_state = TOPLEVEL; |
| 457 | |
| 458 | static void do_telnet_read(char *buf, int len) |
| 459 | { |
| 460 | |
| 461 | while (len--) { |
| 462 | int c = (unsigned char) *buf++; |
| 463 | |
| 464 | switch (telnet_state) { |
| 465 | case TOPLEVEL: |
| 466 | case SEENCR: |
| 467 | if (c == NUL && telnet_state == SEENCR) |
| 468 | telnet_state = TOPLEVEL; |
| 469 | else if (c == IAC) |
| 470 | telnet_state = SEENIAC; |
| 471 | else { |
| 472 | if (!in_synch) |
| 473 | c_write1(c); |
| 474 | |
| 475 | #if 1 |
| 476 | /* I can't get the F***ing winsock to insert the urgent IAC |
| 477 | * into the right position! Even with SO_OOBINLINE it gives |
| 478 | * it to recv too soon. And of course the DM byte (that |
| 479 | * arrives in the same packet!) appears several K later!! |
| 480 | * |
| 481 | * Oh well, we do get the DM in the right place so I'll |
| 482 | * just stop hiding on the next 0xf2 and hope for the best. |
| 483 | */ |
| 484 | else if (c == DM) |
| 485 | in_synch = 0; |
| 486 | #endif |
| 487 | if (c == CR) |
| 488 | telnet_state = SEENCR; |
| 489 | else |
| 490 | telnet_state = TOPLEVEL; |
| 491 | } |
| 492 | break; |
| 493 | case SEENIAC: |
| 494 | if (c == DO) |
| 495 | telnet_state = SEENDO; |
| 496 | else if (c == DONT) |
| 497 | telnet_state = SEENDONT; |
| 498 | else if (c == WILL) |
| 499 | telnet_state = SEENWILL; |
| 500 | else if (c == WONT) |
| 501 | telnet_state = SEENWONT; |
| 502 | else if (c == SB) |
| 503 | telnet_state = SEENSB; |
| 504 | else if (c == DM) { |
| 505 | in_synch = 0; |
| 506 | telnet_state = TOPLEVEL; |
| 507 | } else { |
| 508 | /* ignore everything else; print it if it's IAC */ |
| 509 | if (c == IAC) { |
| 510 | c_write1(c); |
| 511 | } |
| 512 | telnet_state = TOPLEVEL; |
| 513 | } |
| 514 | break; |
| 515 | case SEENWILL: |
| 516 | proc_rec_opt(WILL, c); |
| 517 | telnet_state = TOPLEVEL; |
| 518 | break; |
| 519 | case SEENWONT: |
| 520 | proc_rec_opt(WONT, c); |
| 521 | telnet_state = TOPLEVEL; |
| 522 | break; |
| 523 | case SEENDO: |
| 524 | proc_rec_opt(DO, c); |
| 525 | telnet_state = TOPLEVEL; |
| 526 | break; |
| 527 | case SEENDONT: |
| 528 | proc_rec_opt(DONT, c); |
| 529 | telnet_state = TOPLEVEL; |
| 530 | break; |
| 531 | case SEENSB: |
| 532 | sb_opt = c; |
| 533 | sb_len = 0; |
| 534 | telnet_state = SUBNEGOT; |
| 535 | break; |
| 536 | case SUBNEGOT: |
| 537 | if (c == IAC) |
| 538 | telnet_state = SUBNEG_IAC; |
| 539 | else { |
| 540 | subneg_addchar: |
| 541 | if (sb_len >= sb_size) { |
| 542 | char *newbuf; |
| 543 | sb_size += SB_DELTA; |
| 544 | newbuf = (sb_buf ? |
| 545 | srealloc(sb_buf, sb_size) : |
| 546 | smalloc(sb_size)); |
| 547 | if (newbuf) |
| 548 | sb_buf = newbuf; |
| 549 | else |
| 550 | sb_size -= SB_DELTA; |
| 551 | } |
| 552 | if (sb_len < sb_size) |
| 553 | sb_buf[sb_len++] = c; |
| 554 | telnet_state = SUBNEGOT; /* in case we came here by goto */ |
| 555 | } |
| 556 | break; |
| 557 | case SUBNEG_IAC: |
| 558 | if (c != SE) |
| 559 | goto subneg_addchar; /* yes, it's a hack, I know, but... */ |
| 560 | else { |
| 561 | process_subneg(); |
| 562 | telnet_state = TOPLEVEL; |
| 563 | } |
| 564 | break; |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | static int telnet_closing(Plug plug, char *error_msg, int error_code, |
| 570 | int calling_back) |
| 571 | { |
| 572 | if (s) { |
| 573 | sk_close(s); |
| 574 | s = NULL; |
| 575 | } |
| 576 | if (error_msg) { |
| 577 | /* A socket error has occurred. */ |
| 578 | connection_fatal(error_msg); |
| 579 | } /* Otherwise, the remote side closed the connection normally. */ |
| 580 | return 0; |
| 581 | } |
| 582 | |
| 583 | static int telnet_receive(Plug plug, int urgent, char *data, int len) |
| 584 | { |
| 585 | if (urgent) |
| 586 | in_synch = TRUE; |
| 587 | do_telnet_read(data, len); |
| 588 | return 1; |
| 589 | } |
| 590 | |
| 591 | static void telnet_sent(Plug plug, int bufsize) |
| 592 | { |
| 593 | telnet_bufsize = bufsize; |
| 594 | } |
| 595 | |
| 596 | /* |
| 597 | * Called to set up the Telnet connection. |
| 598 | * |
| 599 | * Returns an error message, or NULL on success. |
| 600 | * |
| 601 | * Also places the canonical host name into `realhost'. It must be |
| 602 | * freed by the caller. |
| 603 | */ |
| 604 | static char *telnet_init(char *host, int port, char **realhost) |
| 605 | { |
| 606 | static struct plug_function_table fn_table = { |
| 607 | telnet_closing, |
| 608 | telnet_receive, |
| 609 | telnet_sent |
| 610 | }, *fn_table_ptr = &fn_table; |
| 611 | |
| 612 | SockAddr addr; |
| 613 | char *err; |
| 614 | |
| 615 | /* |
| 616 | * Try to find host. |
| 617 | */ |
| 618 | { |
| 619 | char buf[200]; |
| 620 | sprintf(buf, "Looking up host \"%.170s\"", host); |
| 621 | logevent(buf); |
| 622 | } |
| 623 | addr = sk_namelookup(host, realhost); |
| 624 | if ((err = sk_addr_error(addr))) |
| 625 | return err; |
| 626 | |
| 627 | if (port < 0) |
| 628 | port = 23; /* default telnet port */ |
| 629 | |
| 630 | /* |
| 631 | * Open socket. |
| 632 | */ |
| 633 | { |
| 634 | char buf[200], addrbuf[100]; |
| 635 | sk_getaddr(addr, addrbuf, 100); |
| 636 | sprintf(buf, "Connecting to %.100s port %d", addrbuf, port); |
| 637 | logevent(buf); |
| 638 | } |
| 639 | s = sk_new(addr, port, 0, 1, &fn_table_ptr); |
| 640 | if ((err = sk_socket_error(s))) |
| 641 | return err; |
| 642 | |
| 643 | sk_addr_free(addr); |
| 644 | |
| 645 | /* |
| 646 | * Initialise option states. |
| 647 | */ |
| 648 | if (cfg.passive_telnet) { |
| 649 | struct Opt **o; |
| 650 | |
| 651 | for (o = opts; *o; o++) |
| 652 | if ((*o)->state == REQUESTED) |
| 653 | (*o)->state = INACTIVE; |
| 654 | } else { |
| 655 | struct Opt **o; |
| 656 | |
| 657 | for (o = opts; *o; o++) |
| 658 | if ((*o)->state == REQUESTED) |
| 659 | send_opt((*o)->send, (*o)->option); |
| 660 | activated = TRUE; |
| 661 | } |
| 662 | |
| 663 | /* |
| 664 | * Set up SYNCH state. |
| 665 | */ |
| 666 | in_synch = FALSE; |
| 667 | |
| 668 | return NULL; |
| 669 | } |
| 670 | |
| 671 | /* |
| 672 | * Called to send data down the Telnet connection. |
| 673 | */ |
| 674 | static int telnet_send(char *buf, int len) |
| 675 | { |
| 676 | char *p; |
| 677 | static unsigned char iac[2] = { IAC, IAC }; |
| 678 | static unsigned char cr[2] = { CR, NUL }; |
| 679 | #if 0 |
| 680 | static unsigned char nl[2] = { CR, LF }; |
| 681 | #endif |
| 682 | |
| 683 | if (s == NULL) |
| 684 | return 0; |
| 685 | |
| 686 | p = buf; |
| 687 | while (p < buf + len) { |
| 688 | char *q = p; |
| 689 | |
| 690 | while (iswritable((unsigned char) *p) && p < buf + len) |
| 691 | p++; |
| 692 | telnet_bufsize = sk_write(s, q, p - q); |
| 693 | |
| 694 | while (p < buf + len && !iswritable((unsigned char) *p)) { |
| 695 | telnet_bufsize = |
| 696 | sk_write(s, (unsigned char) *p == IAC ? iac : cr, 2); |
| 697 | p++; |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | return telnet_bufsize; |
| 702 | } |
| 703 | |
| 704 | /* |
| 705 | * Called to query the current socket sendability status. |
| 706 | */ |
| 707 | static int telnet_sendbuffer(void) |
| 708 | { |
| 709 | return telnet_bufsize; |
| 710 | } |
| 711 | |
| 712 | /* |
| 713 | * Called to set the size of the window from Telnet's POV. |
| 714 | */ |
| 715 | static void telnet_size(void) |
| 716 | { |
| 717 | unsigned char b[16]; |
| 718 | char logbuf[50]; |
| 719 | |
| 720 | if (s == NULL || o_naws.state != ACTIVE) |
| 721 | return; |
| 722 | b[0] = IAC; |
| 723 | b[1] = SB; |
| 724 | b[2] = TELOPT_NAWS; |
| 725 | b[3] = cols >> 8; |
| 726 | b[4] = cols & 0xFF; |
| 727 | b[5] = rows >> 8; |
| 728 | b[6] = rows & 0xFF; |
| 729 | b[7] = IAC; |
| 730 | b[8] = SE; |
| 731 | telnet_bufsize = sk_write(s, b, 9); |
| 732 | sprintf(logbuf, "client:\tSB NAWS %d,%d", |
| 733 | ((unsigned char) b[3] << 8) + (unsigned char) b[4], |
| 734 | ((unsigned char) b[5] << 8) + (unsigned char) b[6]); |
| 735 | logevent(logbuf); |
| 736 | } |
| 737 | |
| 738 | /* |
| 739 | * Send Telnet special codes. |
| 740 | */ |
| 741 | static void telnet_special(Telnet_Special code) |
| 742 | { |
| 743 | unsigned char b[2]; |
| 744 | |
| 745 | if (s == NULL) |
| 746 | return; |
| 747 | |
| 748 | b[0] = IAC; |
| 749 | switch (code) { |
| 750 | case TS_AYT: |
| 751 | b[1] = AYT; |
| 752 | telnet_bufsize = sk_write(s, b, 2); |
| 753 | break; |
| 754 | case TS_BRK: |
| 755 | b[1] = BREAK; |
| 756 | telnet_bufsize = sk_write(s, b, 2); |
| 757 | break; |
| 758 | case TS_EC: |
| 759 | b[1] = EC; |
| 760 | telnet_bufsize = sk_write(s, b, 2); |
| 761 | break; |
| 762 | case TS_EL: |
| 763 | b[1] = EL; |
| 764 | telnet_bufsize = sk_write(s, b, 2); |
| 765 | break; |
| 766 | case TS_GA: |
| 767 | b[1] = GA; |
| 768 | telnet_bufsize = sk_write(s, b, 2); |
| 769 | break; |
| 770 | case TS_NOP: |
| 771 | b[1] = NOP; |
| 772 | telnet_bufsize = sk_write(s, b, 2); |
| 773 | break; |
| 774 | case TS_ABORT: |
| 775 | b[1] = ABORT; |
| 776 | telnet_bufsize = sk_write(s, b, 2); |
| 777 | break; |
| 778 | case TS_AO: |
| 779 | b[1] = AO; |
| 780 | telnet_bufsize = sk_write(s, b, 2); |
| 781 | break; |
| 782 | case TS_IP: |
| 783 | b[1] = IP; |
| 784 | telnet_bufsize = sk_write(s, b, 2); |
| 785 | break; |
| 786 | case TS_SUSP: |
| 787 | b[1] = SUSP; |
| 788 | telnet_bufsize = sk_write(s, b, 2); |
| 789 | break; |
| 790 | case TS_EOR: |
| 791 | b[1] = EOR; |
| 792 | telnet_bufsize = sk_write(s, b, 2); |
| 793 | break; |
| 794 | case TS_EOF: |
| 795 | b[1] = xEOF; |
| 796 | telnet_bufsize = sk_write(s, b, 2); |
| 797 | break; |
| 798 | case TS_EOL: |
| 799 | telnet_bufsize = sk_write(s, "\r\n", 2); |
| 800 | break; |
| 801 | case TS_SYNCH: |
| 802 | b[1] = DM; |
| 803 | telnet_bufsize = sk_write(s, b, 1); |
| 804 | telnet_bufsize = sk_write_oob(s, b + 1, 1); |
| 805 | break; |
| 806 | case TS_RECHO: |
| 807 | if (o_echo.state == INACTIVE || o_echo.state == REALLY_INACTIVE) { |
| 808 | o_echo.state = REQUESTED; |
| 809 | send_opt(o_echo.send, o_echo.option); |
| 810 | } |
| 811 | break; |
| 812 | case TS_LECHO: |
| 813 | if (o_echo.state == ACTIVE) { |
| 814 | o_echo.state = REQUESTED; |
| 815 | send_opt(o_echo.nsend, o_echo.option); |
| 816 | } |
| 817 | break; |
| 818 | case TS_PING: |
| 819 | if (o_they_sga.state == ACTIVE) { |
| 820 | b[1] = NOP; |
| 821 | telnet_bufsize = sk_write(s, b, 2); |
| 822 | } |
| 823 | break; |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | static Socket telnet_socket(void) |
| 828 | { |
| 829 | return s; |
| 830 | } |
| 831 | |
| 832 | static int telnet_sendok(void) |
| 833 | { |
| 834 | return 1; |
| 835 | } |
| 836 | |
| 837 | static void telnet_unthrottle(int backlog) |
| 838 | { |
| 839 | sk_set_frozen(s, backlog > TELNET_MAX_BACKLOG); |
| 840 | } |
| 841 | |
| 842 | static int telnet_ldisc(int option) |
| 843 | { |
| 844 | if (option == LD_ECHO) |
| 845 | return echoing; |
| 846 | if (option == LD_EDIT) |
| 847 | return editing; |
| 848 | return FALSE; |
| 849 | } |
| 850 | |
| 851 | Backend telnet_backend = { |
| 852 | telnet_init, |
| 853 | telnet_send, |
| 854 | telnet_sendbuffer, |
| 855 | telnet_size, |
| 856 | telnet_special, |
| 857 | telnet_socket, |
| 858 | telnet_sendok, |
| 859 | telnet_ldisc, |
| 860 | telnet_unthrottle, |
| 861 | 23 |
| 862 | }; |