| 1 | /* |
| 2 | * Telnet backend. |
| 3 | */ |
| 4 | |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <limits.h> |
| 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 | |
| 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 | |
| 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, |
| 97 | enum { TELOPTS(telnet_enum) dummy=0 }; |
| 98 | #undef telnet_enum |
| 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 */ |
| 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 | |
| 112 | #define iswritable(x) \ |
| 113 | ( (x) != IAC && \ |
| 114 | (telnet->opt_states[o_we_bin.index] == ACTIVE || (x) != CR)) |
| 115 | |
| 116 | static char *telopt(int opt) |
| 117 | { |
| 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 |
| 125 | } |
| 126 | |
| 127 | static void telnet_size(void *handle, int width, int height); |
| 128 | |
| 129 | struct 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 */ |
| 134 | int index; /* index into telnet->opt_states[] */ |
| 135 | enum { |
| 136 | REQUESTED, ACTIVE, INACTIVE, REALLY_INACTIVE |
| 137 | } initial_state; |
| 138 | }; |
| 139 | |
| 140 | enum { |
| 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, |
| 149 | OPTINDEX_WE_BIN, |
| 150 | OPTINDEX_THEY_BIN, |
| 151 | NUM_OPTS |
| 152 | }; |
| 153 | |
| 154 | static const struct Opt o_naws = |
| 155 | { WILL, WONT, DO, DONT, TELOPT_NAWS, OPTINDEX_NAWS, REQUESTED }; |
| 156 | static const struct Opt o_tspeed = |
| 157 | { WILL, WONT, DO, DONT, TELOPT_TSPEED, OPTINDEX_TSPEED, REQUESTED }; |
| 158 | static const struct Opt o_ttype = |
| 159 | { WILL, WONT, DO, DONT, TELOPT_TTYPE, OPTINDEX_TTYPE, REQUESTED }; |
| 160 | static const struct Opt o_oenv = |
| 161 | { WILL, WONT, DO, DONT, TELOPT_OLD_ENVIRON, OPTINDEX_OENV, INACTIVE }; |
| 162 | static const struct Opt o_nenv = |
| 163 | { WILL, WONT, DO, DONT, TELOPT_NEW_ENVIRON, OPTINDEX_NENV, REQUESTED }; |
| 164 | static const struct Opt o_echo = |
| 165 | { DO, DONT, WILL, WONT, TELOPT_ECHO, OPTINDEX_ECHO, REQUESTED }; |
| 166 | static const struct Opt o_we_sga = |
| 167 | { WILL, WONT, DO, DONT, TELOPT_SGA, OPTINDEX_WE_SGA, REQUESTED }; |
| 168 | static const struct Opt o_they_sga = |
| 169 | { DO, DONT, WILL, WONT, TELOPT_SGA, OPTINDEX_THEY_SGA, REQUESTED }; |
| 170 | static const struct Opt o_we_bin = |
| 171 | { WILL, WONT, DO, DONT, TELOPT_BINARY, OPTINDEX_WE_BIN, INACTIVE }; |
| 172 | static const struct Opt o_they_bin = |
| 173 | { DO, DONT, WILL, WONT, TELOPT_BINARY, OPTINDEX_THEY_BIN, INACTIVE }; |
| 174 | |
| 175 | static const struct Opt *const opts[] = { |
| 176 | &o_naws, &o_tspeed, &o_ttype, &o_oenv, &o_nenv, &o_echo, |
| 177 | &o_we_sga, &o_they_sga, &o_we_bin, &o_they_bin, NULL |
| 178 | }; |
| 179 | |
| 180 | typedef struct telnet_tag { |
| 181 | const struct plug_function_table *fn; |
| 182 | /* the above field _must_ be first in the structure */ |
| 183 | |
| 184 | Socket s; |
| 185 | int closed_on_socket_error; |
| 186 | |
| 187 | void *frontend; |
| 188 | void *ldisc; |
| 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; |
| 198 | unsigned char *sb_buf; |
| 199 | int sb_size; |
| 200 | |
| 201 | enum { |
| 202 | TOP_LEVEL, SEENIAC, SEENWILL, SEENWONT, SEENDO, SEENDONT, |
| 203 | SEENSB, SUBNEGOT, SUBNEG_IAC, SEENCR |
| 204 | } state; |
| 205 | |
| 206 | Conf *conf; |
| 207 | |
| 208 | Pinger pinger; |
| 209 | } *Telnet; |
| 210 | |
| 211 | #define TELNET_MAX_BACKLOG 4096 |
| 212 | |
| 213 | #define SB_DELTA 1024 |
| 214 | |
| 215 | static void c_write(Telnet telnet, char *buf, int len) |
| 216 | { |
| 217 | int backlog; |
| 218 | backlog = from_backend(telnet->frontend, 0, buf, len); |
| 219 | sk_set_frozen(telnet->s, backlog > TELNET_MAX_BACKLOG); |
| 220 | } |
| 221 | |
| 222 | static void log_option(Telnet telnet, char *sender, int cmd, int option) |
| 223 | { |
| 224 | char *buf; |
| 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 | */ |
| 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)); |
| 234 | logevent(telnet->frontend, buf); |
| 235 | sfree(buf); |
| 236 | } |
| 237 | |
| 238 | static void send_opt(Telnet telnet, int cmd, int option) |
| 239 | { |
| 240 | unsigned char b[3]; |
| 241 | |
| 242 | b[0] = IAC; |
| 243 | b[1] = cmd; |
| 244 | b[2] = option; |
| 245 | telnet->bufsize = sk_write(telnet->s, (char *)b, 3); |
| 246 | log_option(telnet, "client", cmd, option); |
| 247 | } |
| 248 | |
| 249 | static void deactivate_option(Telnet telnet, const struct Opt *o) |
| 250 | { |
| 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; |
| 255 | } |
| 256 | |
| 257 | /* |
| 258 | * Generate side effects of enabling or disabling an option. |
| 259 | */ |
| 260 | static void option_side_effects(Telnet telnet, const struct Opt *o, int enabled) |
| 261 | { |
| 262 | if (o->option == TELOPT_ECHO && o->send == DO) |
| 263 | telnet->echoing = !enabled; |
| 264 | else if (o->option == TELOPT_SGA && o->send == DO) |
| 265 | telnet->editing = !enabled; |
| 266 | if (telnet->ldisc) /* cause ldisc to notice the change */ |
| 267 | ldisc_send(telnet->ldisc, NULL, 0, 0); |
| 268 | |
| 269 | /* Ensure we get the minimum options */ |
| 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); |
| 274 | } |
| 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); |
| 278 | } |
| 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); |
| 282 | } |
| 283 | telnet->activated = TRUE; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | static void activate_option(Telnet telnet, const struct Opt *o) |
| 288 | { |
| 289 | if (o->send == WILL && o->option == TELOPT_NAWS) |
| 290 | telnet_size(telnet, telnet->term_width, telnet->term_height); |
| 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 | */ |
| 298 | deactivate_option(telnet, o->option == |
| 299 | TELOPT_NEW_ENVIRON ? &o_oenv : &o_nenv); |
| 300 | } |
| 301 | option_side_effects(telnet, o, 1); |
| 302 | } |
| 303 | |
| 304 | static void refused_option(Telnet telnet, const struct Opt *o) |
| 305 | { |
| 306 | if (o->send == WILL && o->option == TELOPT_NEW_ENVIRON && |
| 307 | telnet->opt_states[o_oenv.index] == INACTIVE) { |
| 308 | send_opt(telnet, WILL, TELOPT_OLD_ENVIRON); |
| 309 | telnet->opt_states[o_oenv.index] = REQUESTED; |
| 310 | } |
| 311 | option_side_effects(telnet, o, 0); |
| 312 | } |
| 313 | |
| 314 | static void proc_rec_opt(Telnet telnet, int cmd, int option) |
| 315 | { |
| 316 | const struct Opt *const *o; |
| 317 | |
| 318 | log_option(telnet, "server", cmd, option); |
| 319 | for (o = opts; *o; o++) { |
| 320 | if ((*o)->option == option && (*o)->ack == cmd) { |
| 321 | switch (telnet->opt_states[(*o)->index]) { |
| 322 | case REQUESTED: |
| 323 | telnet->opt_states[(*o)->index] = ACTIVE; |
| 324 | activate_option(telnet, *o); |
| 325 | break; |
| 326 | case ACTIVE: |
| 327 | break; |
| 328 | case INACTIVE: |
| 329 | telnet->opt_states[(*o)->index] = ACTIVE; |
| 330 | send_opt(telnet, (*o)->send, option); |
| 331 | activate_option(telnet, *o); |
| 332 | break; |
| 333 | case REALLY_INACTIVE: |
| 334 | send_opt(telnet, (*o)->nsend, option); |
| 335 | break; |
| 336 | } |
| 337 | return; |
| 338 | } else if ((*o)->option == option && (*o)->nak == cmd) { |
| 339 | switch (telnet->opt_states[(*o)->index]) { |
| 340 | case REQUESTED: |
| 341 | telnet->opt_states[(*o)->index] = INACTIVE; |
| 342 | refused_option(telnet, *o); |
| 343 | break; |
| 344 | case ACTIVE: |
| 345 | telnet->opt_states[(*o)->index] = INACTIVE; |
| 346 | send_opt(telnet, (*o)->nsend, option); |
| 347 | option_side_effects(telnet, *o, 0); |
| 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 |
| 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. |
| 361 | */ |
| 362 | if (cmd == WILL || cmd == DO) |
| 363 | send_opt(telnet, (cmd == WILL ? DONT : WONT), option); |
| 364 | } |
| 365 | |
| 366 | static void process_subneg(Telnet telnet) |
| 367 | { |
| 368 | unsigned char *b, *p, *q; |
| 369 | int var, value, n, bsize; |
| 370 | char *e, *eval, *ekey, *user; |
| 371 | |
| 372 | switch (telnet->sb_opt) { |
| 373 | case TELOPT_TSPEED: |
| 374 | if (telnet->sb_len == 1 && telnet->sb_buf[0] == TELQUAL_SEND) { |
| 375 | char *logbuf; |
| 376 | char *termspeed = conf_get_str(telnet->conf, CONF_termspeed); |
| 377 | b = snewn(20 + strlen(termspeed), unsigned char); |
| 378 | b[0] = IAC; |
| 379 | b[1] = SB; |
| 380 | b[2] = TELOPT_TSPEED; |
| 381 | b[3] = TELQUAL_IS; |
| 382 | strcpy((char *)(b + 4), termspeed); |
| 383 | n = 4 + strlen(termspeed); |
| 384 | b[n] = IAC; |
| 385 | b[n + 1] = SE; |
| 386 | telnet->bufsize = sk_write(telnet->s, (char *)b, n + 2); |
| 387 | logevent(telnet->frontend, "server:\tSB TSPEED SEND"); |
| 388 | logbuf = dupprintf("client:\tSB TSPEED IS %s", termspeed); |
| 389 | logevent(telnet->frontend, logbuf); |
| 390 | sfree(logbuf); |
| 391 | sfree(b); |
| 392 | } else |
| 393 | logevent(telnet->frontend, "server:\tSB TSPEED <something weird>"); |
| 394 | break; |
| 395 | case TELOPT_TTYPE: |
| 396 | if (telnet->sb_len == 1 && telnet->sb_buf[0] == TELQUAL_SEND) { |
| 397 | char *logbuf; |
| 398 | char *termtype = conf_get_str(telnet->conf, CONF_termtype); |
| 399 | b = snewn(20 + strlen(termtype), unsigned char); |
| 400 | b[0] = IAC; |
| 401 | b[1] = SB; |
| 402 | b[2] = TELOPT_TTYPE; |
| 403 | b[3] = TELQUAL_IS; |
| 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]); |
| 408 | b[n + 4] = IAC; |
| 409 | b[n + 5] = SE; |
| 410 | telnet->bufsize = sk_write(telnet->s, (char *)b, n + 6); |
| 411 | b[n + 4] = 0; |
| 412 | logevent(telnet->frontend, "server:\tSB TTYPE SEND"); |
| 413 | logbuf = dupprintf("client:\tSB TTYPE IS %s", b + 4); |
| 414 | logevent(telnet->frontend, logbuf); |
| 415 | sfree(logbuf); |
| 416 | sfree(b); |
| 417 | } else |
| 418 | logevent(telnet->frontend, "server:\tSB TTYPE <something weird>\r\n"); |
| 419 | break; |
| 420 | case TELOPT_OLD_ENVIRON: |
| 421 | case TELOPT_NEW_ENVIRON: |
| 422 | p = telnet->sb_buf; |
| 423 | q = p + telnet->sb_len; |
| 424 | if (p < q && *p == TELQUAL_SEND) { |
| 425 | char *logbuf; |
| 426 | p++; |
| 427 | logbuf = dupprintf("server:\tSB %s SEND", telopt(telnet->sb_opt)); |
| 428 | logevent(telnet->frontend, logbuf); |
| 429 | sfree(logbuf); |
| 430 | if (telnet->sb_opt == TELOPT_OLD_ENVIRON) { |
| 431 | if (conf_get_int(telnet->conf, CONF_rfc_environ)) { |
| 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 | } |
| 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); |
| 471 | b[0] = IAC; |
| 472 | b[1] = SB; |
| 473 | b[2] = telnet->sb_opt; |
| 474 | b[3] = TELQUAL_IS; |
| 475 | n = 4; |
| 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)) { |
| 481 | b[n++] = var; |
| 482 | for (e = ekey; *e; e++) |
| 483 | b[n++] = *e; |
| 484 | b[n++] = value; |
| 485 | for (e = eval; *e; e++) |
| 486 | b[n++] = *e; |
| 487 | } |
| 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)); |
| 504 | logevent(telnet->frontend, logbuf); |
| 505 | sfree(logbuf); |
| 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 | } |
| 525 | } |
| 526 | sfree(b); |
| 527 | sfree(user); |
| 528 | } |
| 529 | break; |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | static void do_telnet_read(Telnet telnet, char *buf, int len) |
| 534 | { |
| 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) |
| 545 | |
| 546 | while (len--) { |
| 547 | int c = (unsigned char) *buf++; |
| 548 | |
| 549 | switch (telnet->state) { |
| 550 | case TOP_LEVEL: |
| 551 | case SEENCR: |
| 552 | if (c == NUL && telnet->state == SEENCR) |
| 553 | telnet->state = TOP_LEVEL; |
| 554 | else if (c == IAC) |
| 555 | telnet->state = SEENIAC; |
| 556 | else { |
| 557 | if (!telnet->in_synch) |
| 558 | ADDTOBUF(c); |
| 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 | */ |
| 569 | else if (c == DM) |
| 570 | telnet->in_synch = 0; |
| 571 | #endif |
| 572 | if (c == CR && telnet->opt_states[o_they_bin.index] != ACTIVE) |
| 573 | telnet->state = SEENCR; |
| 574 | else |
| 575 | telnet->state = TOP_LEVEL; |
| 576 | } |
| 577 | break; |
| 578 | case SEENIAC: |
| 579 | if (c == DO) |
| 580 | telnet->state = SEENDO; |
| 581 | else if (c == DONT) |
| 582 | telnet->state = SEENDONT; |
| 583 | else if (c == WILL) |
| 584 | telnet->state = SEENWILL; |
| 585 | else if (c == WONT) |
| 586 | telnet->state = SEENWONT; |
| 587 | else if (c == SB) |
| 588 | telnet->state = SEENSB; |
| 589 | else if (c == DM) { |
| 590 | telnet->in_synch = 0; |
| 591 | telnet->state = TOP_LEVEL; |
| 592 | } else { |
| 593 | /* ignore everything else; print it if it's IAC */ |
| 594 | if (c == IAC) { |
| 595 | ADDTOBUF(c); |
| 596 | } |
| 597 | telnet->state = TOP_LEVEL; |
| 598 | } |
| 599 | break; |
| 600 | case SEENWILL: |
| 601 | proc_rec_opt(telnet, WILL, c); |
| 602 | telnet->state = TOP_LEVEL; |
| 603 | break; |
| 604 | case SEENWONT: |
| 605 | proc_rec_opt(telnet, WONT, c); |
| 606 | telnet->state = TOP_LEVEL; |
| 607 | break; |
| 608 | case SEENDO: |
| 609 | proc_rec_opt(telnet, DO, c); |
| 610 | telnet->state = TOP_LEVEL; |
| 611 | break; |
| 612 | case SEENDONT: |
| 613 | proc_rec_opt(telnet, DONT, c); |
| 614 | telnet->state = TOP_LEVEL; |
| 615 | break; |
| 616 | case SEENSB: |
| 617 | telnet->sb_opt = c; |
| 618 | telnet->sb_len = 0; |
| 619 | telnet->state = SUBNEGOT; |
| 620 | break; |
| 621 | case SUBNEGOT: |
| 622 | if (c == IAC) |
| 623 | telnet->state = SUBNEG_IAC; |
| 624 | else { |
| 625 | subneg_addchar: |
| 626 | if (telnet->sb_len >= telnet->sb_size) { |
| 627 | telnet->sb_size += SB_DELTA; |
| 628 | telnet->sb_buf = sresize(telnet->sb_buf, telnet->sb_size, |
| 629 | unsigned char); |
| 630 | } |
| 631 | telnet->sb_buf[telnet->sb_len++] = c; |
| 632 | telnet->state = SUBNEGOT; /* in case we came here by goto */ |
| 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 { |
| 639 | process_subneg(telnet); |
| 640 | telnet->state = TOP_LEVEL; |
| 641 | } |
| 642 | break; |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | if (outbuflen) |
| 647 | c_write(telnet, outbuf, outbuflen); |
| 648 | sfree(outbuf); |
| 649 | } |
| 650 | |
| 651 | static 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); |
| 665 | sfree(msg); |
| 666 | } |
| 667 | |
| 668 | static int telnet_closing(Plug plug, const char *error_msg, int error_code, |
| 669 | int calling_back) |
| 670 | { |
| 671 | Telnet telnet = (Telnet) plug; |
| 672 | |
| 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 | |
| 679 | if (telnet->s) { |
| 680 | sk_close(telnet->s); |
| 681 | telnet->s = NULL; |
| 682 | if (error_msg) |
| 683 | telnet->closed_on_socket_error = TRUE; |
| 684 | notify_remote_exit(telnet->frontend); |
| 685 | } |
| 686 | if (error_msg) { |
| 687 | logevent(telnet->frontend, error_msg); |
| 688 | connection_fatal(telnet->frontend, "%s", error_msg); |
| 689 | } |
| 690 | /* Otherwise, the remote side closed the connection normally. */ |
| 691 | return 0; |
| 692 | } |
| 693 | |
| 694 | static int telnet_receive(Plug plug, int urgent, char *data, int len) |
| 695 | { |
| 696 | Telnet telnet = (Telnet) plug; |
| 697 | if (urgent) |
| 698 | telnet->in_synch = TRUE; |
| 699 | do_telnet_read(telnet, data, len); |
| 700 | return 1; |
| 701 | } |
| 702 | |
| 703 | static void telnet_sent(Plug plug, int bufsize) |
| 704 | { |
| 705 | Telnet telnet = (Telnet) plug; |
| 706 | telnet->bufsize = bufsize; |
| 707 | } |
| 708 | |
| 709 | /* |
| 710 | * Called to set up the Telnet connection. |
| 711 | * |
| 712 | * Returns an error message, or NULL on success. |
| 713 | * |
| 714 | * Also places the canonical host name into `realhost'. It must be |
| 715 | * freed by the caller. |
| 716 | */ |
| 717 | static const char *telnet_init(void *frontend_handle, void **backend_handle, |
| 718 | Conf *conf, char *host, int port, |
| 719 | char **realhost, int nodelay, int keepalive) |
| 720 | { |
| 721 | static const struct plug_function_table fn_table = { |
| 722 | telnet_log, |
| 723 | telnet_closing, |
| 724 | telnet_receive, |
| 725 | telnet_sent |
| 726 | }; |
| 727 | SockAddr addr; |
| 728 | const char *err; |
| 729 | Telnet telnet; |
| 730 | char *loghost; |
| 731 | int addressfamily; |
| 732 | |
| 733 | telnet = snew(struct telnet_tag); |
| 734 | telnet->fn = &fn_table; |
| 735 | telnet->conf = conf_copy(conf); |
| 736 | telnet->s = NULL; |
| 737 | telnet->closed_on_socket_error = FALSE; |
| 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; |
| 744 | telnet->term_width = conf_get_int(telnet->conf, CONF_width); |
| 745 | telnet->term_height = conf_get_int(telnet->conf, CONF_height); |
| 746 | telnet->state = TOP_LEVEL; |
| 747 | telnet->ldisc = NULL; |
| 748 | telnet->pinger = NULL; |
| 749 | *backend_handle = telnet; |
| 750 | |
| 751 | /* |
| 752 | * Try to find host. |
| 753 | */ |
| 754 | { |
| 755 | char *buf; |
| 756 | addressfamily = conf_get_int(telnet->conf, CONF_addressfamily); |
| 757 | buf = dupprintf("Looking up host \"%s\"%s", host, |
| 758 | (addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" : |
| 759 | (addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" : |
| 760 | ""))); |
| 761 | logevent(telnet->frontend, buf); |
| 762 | sfree(buf); |
| 763 | } |
| 764 | addr = name_lookup(host, port, realhost, telnet->conf, addressfamily); |
| 765 | if ((err = sk_addr_error(addr)) != NULL) { |
| 766 | sk_addr_free(addr); |
| 767 | return err; |
| 768 | } |
| 769 | |
| 770 | if (port < 0) |
| 771 | port = 23; /* default telnet port */ |
| 772 | |
| 773 | /* |
| 774 | * Open socket. |
| 775 | */ |
| 776 | telnet->s = new_connection(addr, *realhost, port, 0, 1, |
| 777 | nodelay, keepalive, (Plug) telnet, telnet->conf); |
| 778 | if ((err = sk_socket_error(telnet->s)) != NULL) |
| 779 | return err; |
| 780 | |
| 781 | telnet->pinger = pinger_new(telnet->conf, &telnet_backend, telnet); |
| 782 | |
| 783 | /* |
| 784 | * Initialise option states. |
| 785 | */ |
| 786 | if (conf_get_int(telnet->conf, CONF_passive_telnet)) { |
| 787 | const struct Opt *const *o; |
| 788 | |
| 789 | for (o = opts; *o; o++) |
| 790 | telnet->opt_states[(*o)->index] = INACTIVE; |
| 791 | } else { |
| 792 | const struct Opt *const *o; |
| 793 | |
| 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; |
| 800 | } |
| 801 | |
| 802 | /* |
| 803 | * Set up SYNCH state. |
| 804 | */ |
| 805 | telnet->in_synch = FALSE; |
| 806 | |
| 807 | /* |
| 808 | * We can send special commands from the start. |
| 809 | */ |
| 810 | update_specials_menu(telnet->frontend); |
| 811 | |
| 812 | /* |
| 813 | * loghost overrides realhost, if specified. |
| 814 | */ |
| 815 | loghost = conf_get_str(telnet->conf, CONF_loghost); |
| 816 | if (*loghost) { |
| 817 | char *colon; |
| 818 | |
| 819 | sfree(*realhost); |
| 820 | *realhost = dupstr(loghost); |
| 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 | |
| 832 | return NULL; |
| 833 | } |
| 834 | |
| 835 | static 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); |
| 842 | if (telnet->pinger) |
| 843 | pinger_free(telnet->pinger); |
| 844 | conf_free(telnet->conf); |
| 845 | sfree(telnet); |
| 846 | } |
| 847 | /* |
| 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 | */ |
| 852 | static void telnet_reconfig(void *handle, Conf *conf) |
| 853 | { |
| 854 | Telnet telnet = (Telnet) handle; |
| 855 | pinger_reconfig(telnet->pinger, telnet->conf, conf); |
| 856 | conf_free(telnet->conf); |
| 857 | telnet->conf = conf_copy(conf); |
| 858 | } |
| 859 | |
| 860 | /* |
| 861 | * Called to send data down the Telnet connection. |
| 862 | */ |
| 863 | static int telnet_send(void *handle, char *buf, int len) |
| 864 | { |
| 865 | Telnet telnet = (Telnet) handle; |
| 866 | unsigned char *p, *end; |
| 867 | static const unsigned char iac[2] = { IAC, IAC }; |
| 868 | static const unsigned char cr[2] = { CR, NUL }; |
| 869 | #if 0 |
| 870 | static const unsigned char nl[2] = { CR, LF }; |
| 871 | #endif |
| 872 | |
| 873 | if (telnet->s == NULL) |
| 874 | return 0; |
| 875 | |
| 876 | p = (unsigned char *)buf; |
| 877 | end = (unsigned char *)(buf + len); |
| 878 | while (p < end) { |
| 879 | unsigned char *q = p; |
| 880 | |
| 881 | while (p < end && iswritable(*p)) |
| 882 | p++; |
| 883 | telnet->bufsize = sk_write(telnet->s, (char *)q, p - q); |
| 884 | |
| 885 | while (p < end && !iswritable(*p)) { |
| 886 | telnet->bufsize = |
| 887 | sk_write(telnet->s, (char *)(*p == IAC ? iac : cr), 2); |
| 888 | p++; |
| 889 | } |
| 890 | } |
| 891 | |
| 892 | return telnet->bufsize; |
| 893 | } |
| 894 | |
| 895 | /* |
| 896 | * Called to query the current socket sendability status. |
| 897 | */ |
| 898 | static int telnet_sendbuffer(void *handle) |
| 899 | { |
| 900 | Telnet telnet = (Telnet) handle; |
| 901 | return telnet->bufsize; |
| 902 | } |
| 903 | |
| 904 | /* |
| 905 | * Called to set the size of the window from Telnet's POV. |
| 906 | */ |
| 907 | static void telnet_size(void *handle, int width, int height) |
| 908 | { |
| 909 | Telnet telnet = (Telnet) handle; |
| 910 | unsigned char b[24]; |
| 911 | int n; |
| 912 | char *logbuf; |
| 913 | |
| 914 | telnet->term_width = width; |
| 915 | telnet->term_height = height; |
| 916 | |
| 917 | if (telnet->s == NULL || telnet->opt_states[o_naws.index] != ACTIVE) |
| 918 | return; |
| 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; |
| 933 | telnet->bufsize = sk_write(telnet->s, (char *)b, n); |
| 934 | logbuf = dupprintf("client:\tSB NAWS %d,%d", |
| 935 | telnet->term_width, telnet->term_height); |
| 936 | logevent(telnet->frontend, logbuf); |
| 937 | sfree(logbuf); |
| 938 | } |
| 939 | |
| 940 | /* |
| 941 | * Send Telnet special codes. |
| 942 | */ |
| 943 | static void telnet_special(void *handle, Telnet_Special code) |
| 944 | { |
| 945 | Telnet telnet = (Telnet) handle; |
| 946 | unsigned char b[2]; |
| 947 | |
| 948 | if (telnet->s == NULL) |
| 949 | return; |
| 950 | |
| 951 | b[0] = IAC; |
| 952 | switch (code) { |
| 953 | case TS_AYT: |
| 954 | b[1] = AYT; |
| 955 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); |
| 956 | break; |
| 957 | case TS_BRK: |
| 958 | b[1] = BREAK; |
| 959 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); |
| 960 | break; |
| 961 | case TS_EC: |
| 962 | b[1] = EC; |
| 963 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); |
| 964 | break; |
| 965 | case TS_EL: |
| 966 | b[1] = EL; |
| 967 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); |
| 968 | break; |
| 969 | case TS_GA: |
| 970 | b[1] = GA; |
| 971 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); |
| 972 | break; |
| 973 | case TS_NOP: |
| 974 | b[1] = NOP; |
| 975 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); |
| 976 | break; |
| 977 | case TS_ABORT: |
| 978 | b[1] = ABORT; |
| 979 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); |
| 980 | break; |
| 981 | case TS_AO: |
| 982 | b[1] = AO; |
| 983 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); |
| 984 | break; |
| 985 | case TS_IP: |
| 986 | b[1] = IP; |
| 987 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); |
| 988 | break; |
| 989 | case TS_SUSP: |
| 990 | b[1] = SUSP; |
| 991 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); |
| 992 | break; |
| 993 | case TS_EOR: |
| 994 | b[1] = EOR; |
| 995 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); |
| 996 | break; |
| 997 | case TS_EOF: |
| 998 | b[1] = xEOF; |
| 999 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); |
| 1000 | break; |
| 1001 | case TS_EOL: |
| 1002 | /* In BINARY mode, CR-LF becomes just CR - |
| 1003 | * and without the NUL suffix too. */ |
| 1004 | if (telnet->opt_states[o_we_bin.index] == ACTIVE) |
| 1005 | telnet->bufsize = sk_write(telnet->s, "\r", 1); |
| 1006 | else |
| 1007 | telnet->bufsize = sk_write(telnet->s, "\r\n", 2); |
| 1008 | break; |
| 1009 | case TS_SYNCH: |
| 1010 | b[1] = DM; |
| 1011 | telnet->bufsize = sk_write(telnet->s, (char *)b, 1); |
| 1012 | telnet->bufsize = sk_write_oob(telnet->s, (char *)(b + 1), 1); |
| 1013 | break; |
| 1014 | case TS_RECHO: |
| 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); |
| 1019 | } |
| 1020 | break; |
| 1021 | case TS_LECHO: |
| 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); |
| 1025 | } |
| 1026 | break; |
| 1027 | case TS_PING: |
| 1028 | if (telnet->opt_states[o_they_sga.index] == ACTIVE) { |
| 1029 | b[1] = NOP; |
| 1030 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); |
| 1031 | } |
| 1032 | break; |
| 1033 | default: |
| 1034 | break; /* never heard of it */ |
| 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | static 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}, |
| 1048 | {NULL, TS_SEP}, |
| 1049 | {"Abort Process", TS_ABORT}, |
| 1050 | {"Abort Output", TS_AO}, |
| 1051 | {"Interrupt Process", TS_IP}, |
| 1052 | {"Suspend Process", TS_SUSP}, |
| 1053 | {NULL, TS_SEP}, |
| 1054 | {"End Of Record", TS_EOR}, |
| 1055 | {"End Of File", TS_EOF}, |
| 1056 | {NULL, TS_EXITMENU} |
| 1057 | }; |
| 1058 | return specials; |
| 1059 | } |
| 1060 | |
| 1061 | static int telnet_connected(void *handle) |
| 1062 | { |
| 1063 | Telnet telnet = (Telnet) handle; |
| 1064 | return telnet->s != NULL; |
| 1065 | } |
| 1066 | |
| 1067 | static int telnet_sendok(void *handle) |
| 1068 | { |
| 1069 | /* Telnet telnet = (Telnet) handle; */ |
| 1070 | return 1; |
| 1071 | } |
| 1072 | |
| 1073 | static void telnet_unthrottle(void *handle, int backlog) |
| 1074 | { |
| 1075 | Telnet telnet = (Telnet) handle; |
| 1076 | sk_set_frozen(telnet->s, backlog > TELNET_MAX_BACKLOG); |
| 1077 | } |
| 1078 | |
| 1079 | static int telnet_ldisc(void *handle, int option) |
| 1080 | { |
| 1081 | Telnet telnet = (Telnet) handle; |
| 1082 | if (option == LD_ECHO) |
| 1083 | return telnet->echoing; |
| 1084 | if (option == LD_EDIT) |
| 1085 | return telnet->editing; |
| 1086 | return FALSE; |
| 1087 | } |
| 1088 | |
| 1089 | static void telnet_provide_ldisc(void *handle, void *ldisc) |
| 1090 | { |
| 1091 | Telnet telnet = (Telnet) handle; |
| 1092 | telnet->ldisc = ldisc; |
| 1093 | } |
| 1094 | |
| 1095 | static void telnet_provide_logctx(void *handle, void *logctx) |
| 1096 | { |
| 1097 | /* This is a stub. */ |
| 1098 | } |
| 1099 | |
| 1100 | static int telnet_exitcode(void *handle) |
| 1101 | { |
| 1102 | Telnet telnet = (Telnet) handle; |
| 1103 | if (telnet->s != NULL) |
| 1104 | return -1; /* still connected */ |
| 1105 | else if (telnet->closed_on_socket_error) |
| 1106 | return INT_MAX; /* a socket error counts as an unclean exit */ |
| 1107 | else |
| 1108 | /* Telnet doesn't transmit exit codes back to the client */ |
| 1109 | return 0; |
| 1110 | } |
| 1111 | |
| 1112 | /* |
| 1113 | * cfg_info for Telnet does nothing at all. |
| 1114 | */ |
| 1115 | static int telnet_cfg_info(void *handle) |
| 1116 | { |
| 1117 | return 0; |
| 1118 | } |
| 1119 | |
| 1120 | Backend telnet_backend = { |
| 1121 | telnet_init, |
| 1122 | telnet_free, |
| 1123 | telnet_reconfig, |
| 1124 | telnet_send, |
| 1125 | telnet_sendbuffer, |
| 1126 | telnet_size, |
| 1127 | telnet_special, |
| 1128 | telnet_get_specials, |
| 1129 | telnet_connected, |
| 1130 | telnet_exitcode, |
| 1131 | telnet_sendok, |
| 1132 | telnet_ldisc, |
| 1133 | telnet_provide_ldisc, |
| 1134 | telnet_provide_logctx, |
| 1135 | telnet_unthrottle, |
| 1136 | telnet_cfg_info, |
| 1137 | "telnet", |
| 1138 | PROT_TELNET, |
| 1139 | 23 |
| 1140 | }; |