X-Git-Url: https://git.distorted.org.uk/u/mdw/putty/blobdiff_plain/a1fd562ddbd8ec7b4c85b479bbe0806b7c6b5589..125105d16c788398562ac03e91ce7a0dc0292492:/telnet.c diff --git a/telnet.c b/telnet.c index f4ea82ba..e3fff49f 100644 --- a/telnet.c +++ b/telnet.c @@ -72,6 +72,19 @@ #define TELOPT_AUTHENTICATION 37 /* Authenticate */ #define TELOPT_ENCRYPT 38 /* Encryption option */ #define TELOPT_NEW_ENVIRON 39 /* New - Environment variables */ +#define TELOPT_TN3270E 40 /* TN3270 enhancements */ +#define TELOPT_XAUTH 41 +#define TELOPT_CHARSET 42 /* Character set */ +#define TELOPT_RSP 43 /* Remote serial port */ +#define TELOPT_COM_PORT_OPTION 44 /* Com port control */ +#define TELOPT_SLE 45 /* Suppress local echo */ +#define TELOPT_STARTTLS 46 /* Start TLS */ +#define TELOPT_KERMIT 47 /* Automatic Kermit file transfer */ +#define TELOPT_SEND_URL 48 +#define TELOPT_FORWARD_X 49 +#define TELOPT_PRAGMA_LOGON 138 +#define TELOPT_SSPI_LOGON 139 +#define TELOPT_PRAGMA_HEARTBEAT 140 #define TELOPT_EXOPL 255 /* extended-options-list */ #define TELQUAL_IS 0 /* option is... */ @@ -132,6 +145,19 @@ static char *telopt(int opt) i(AUTHENTICATION); i(ENCRYPT); i(NEW_ENVIRON); + i(TN3270E); + i(XAUTH); + i(CHARSET); + i(RSP); + i(COM_PORT_OPTION); + i(SLE); + i(STARTTLS); + i(KERMIT); + i(SEND_URL); + i(FORWARD_X); + i(PRAGMA_LOGON); + i(SSPI_LOGON); + i(PRAGMA_HEARTBEAT); i(EXOPL); #undef i return ""; @@ -217,6 +243,7 @@ typedef struct telnet_tag { SEENSB, SUBNEGOT, SUBNEG_IAC, SEENCR } state; + Config cfg; } *Telnet; #define TELNET_MAX_BACKLOG 4096 @@ -386,13 +413,13 @@ static void process_subneg(Telnet telnet) b[1] = SB; b[2] = TELOPT_TSPEED; b[3] = TELQUAL_IS; - strcpy((char *)(b + 4), cfg.termspeed); - n = 4 + strlen(cfg.termspeed); + strcpy((char *)(b + 4), telnet->cfg.termspeed); + n = 4 + strlen(telnet->cfg.termspeed); b[n] = IAC; b[n + 1] = SE; telnet->bufsize = sk_write(telnet->s, (char *)b, n + 2); logevent(telnet->frontend, "server:\tSB TSPEED SEND"); - logbuf = dupprintf("client:\tSB TSPEED IS %s", cfg.termspeed); + logbuf = dupprintf("client:\tSB TSPEED IS %s", telnet->cfg.termspeed); logevent(telnet->frontend, logbuf); sfree(logbuf); } else @@ -405,11 +432,11 @@ static void process_subneg(Telnet telnet) b[1] = SB; b[2] = TELOPT_TTYPE; b[3] = TELQUAL_IS; - for (n = 0; cfg.termtype[n]; n++) - b[n + 4] = (cfg.termtype[n] >= 'a' - && cfg.termtype[n] <= - 'z' ? cfg.termtype[n] + 'A' - - 'a' : cfg.termtype[n]); + for (n = 0; telnet->cfg.termtype[n]; n++) + b[n + 4] = (telnet->cfg.termtype[n] >= 'a' + && telnet->cfg.termtype[n] <= + 'z' ? telnet->cfg.termtype[n] + 'A' - + 'a' : telnet->cfg.termtype[n]); b[n + 4] = IAC; b[n + 5] = SE; telnet->bufsize = sk_write(telnet->s, (char *)b, n + 6); @@ -432,7 +459,7 @@ static void process_subneg(Telnet telnet) logevent(telnet->frontend, logbuf); sfree(logbuf); if (telnet->sb_opt == TELOPT_OLD_ENVIRON) { - if (cfg.rfc_environ) { + if (telnet->cfg.rfc_environ) { value = RFC_VALUE; var = RFC_VAR; } else { @@ -465,7 +492,7 @@ static void process_subneg(Telnet telnet) b[2] = telnet->sb_opt; b[3] = TELQUAL_IS; n = 4; - e = cfg.environmt; + e = telnet->cfg.environmt; while (*e) { b[n++] = var; while (*e && *e != '\t') @@ -477,14 +504,14 @@ static void process_subneg(Telnet telnet) b[n++] = *e++; e++; } - if (*cfg.username) { + if (*telnet->cfg.username) { b[n++] = var; b[n++] = 'U'; b[n++] = 'S'; b[n++] = 'E'; b[n++] = 'R'; b[n++] = value; - e = cfg.username; + e = telnet->cfg.username; while (*e) b[n++] = *e++; } @@ -584,18 +611,11 @@ static void do_telnet_read(Telnet telnet, char *buf, int len) else { subneg_addchar: if (telnet->sb_len >= telnet->sb_size) { - unsigned char *newbuf; telnet->sb_size += SB_DELTA; - newbuf = (telnet->sb_buf ? - srealloc(telnet->sb_buf, telnet->sb_size) : - smalloc(telnet->sb_size)); - if (newbuf) - telnet->sb_buf = newbuf; - else - telnet->sb_size -= SB_DELTA; + telnet->sb_buf = sresize(telnet->sb_buf, telnet->sb_size, + unsigned char); } - if (telnet->sb_len < telnet->sb_size) - telnet->sb_buf[telnet->sb_len++] = c; + telnet->sb_buf[telnet->sb_len++] = c; telnet->state = SUBNEGOT; /* in case we came here by goto */ } break; @@ -652,6 +672,7 @@ static void telnet_sent(Plug plug, int bufsize) * freed by the caller. */ static char *telnet_init(void *frontend_handle, void **backend_handle, + Config *cfg, char *host, int port, char **realhost, int nodelay) { static const struct plug_function_table fn_table = { @@ -663,8 +684,9 @@ static char *telnet_init(void *frontend_handle, void **backend_handle, char *err; Telnet telnet; - telnet = smalloc(sizeof(*telnet)); + telnet = snew(struct telnet_tag); telnet->fn = &fn_table; + telnet->cfg = *cfg; /* STRUCTURE COPY */ telnet->s = NULL; telnet->echoing = TRUE; telnet->editing = TRUE; @@ -672,8 +694,8 @@ static char *telnet_init(void *frontend_handle, void **backend_handle, telnet->sb_buf = NULL; telnet->sb_size = 0; telnet->frontend = frontend_handle; - telnet->term_width = cfg.width; - telnet->term_height = cfg.height; + telnet->term_width = telnet->cfg.width; + telnet->term_height = telnet->cfg.height; telnet->state = TOP_LEVEL; *backend_handle = telnet; @@ -686,7 +708,7 @@ static char *telnet_init(void *frontend_handle, void **backend_handle, logevent(telnet->frontend, buf); sfree(buf); } - addr = name_lookup(host, port, realhost); + addr = name_lookup(host, port, realhost, &telnet->cfg); if ((err = sk_addr_error(addr)) != NULL) return err; @@ -704,7 +726,7 @@ static char *telnet_init(void *frontend_handle, void **backend_handle, sfree(buf); } telnet->s = new_connection(addr, *realhost, port, 0, 1, - nodelay, (Plug) telnet); + nodelay, (Plug) telnet, &telnet->cfg); if ((err = sk_socket_error(telnet->s)) != NULL) return err; @@ -713,7 +735,7 @@ static char *telnet_init(void *frontend_handle, void **backend_handle, /* * Initialise option states. */ - if (cfg.passive_telnet) { + if (telnet->cfg.passive_telnet) { const struct Opt *const *o; for (o = opts; *o; o++) @@ -737,6 +759,26 @@ static char *telnet_init(void *frontend_handle, void **backend_handle, return NULL; } +static void telnet_free(void *handle) +{ + Telnet telnet = (Telnet) handle; + + sfree(telnet->sb_buf); + if (telnet->s) + sk_close(telnet->s); + sfree(telnet); +} +/* + * Reconfigure the Telnet backend. There's no immediate action + * necessary, in this backend: we just save the fresh config for + * any subsequent negotiations. + */ +static void telnet_reconfig(void *handle, Config *cfg) +{ + Telnet telnet = (Telnet) handle; + telnet->cfg = *cfg; /* STRUCTURE COPY */ +} + /* * Called to send data down the Telnet connection. */ @@ -744,10 +786,10 @@ static int telnet_send(void *handle, char *buf, int len) { Telnet telnet = (Telnet) handle; unsigned char *p, *end; - static unsigned char iac[2] = { IAC, IAC }; - static unsigned char cr[2] = { CR, NUL }; + static const unsigned char iac[2] = { IAC, IAC }; + static const unsigned char cr[2] = { CR, NUL }; #if 0 - static unsigned char nl[2] = { CR, LF }; + static const unsigned char nl[2] = { CR, LF }; #endif if (telnet->s == NULL) @@ -879,9 +921,10 @@ static void telnet_special(void *handle, Telnet_Special code) telnet->bufsize = sk_write(telnet->s, (char *)b, 2); break; case TS_EOL: - /* In BINARY mode, CR-LF becomes just CR. */ + /* In BINARY mode, CR-LF becomes just CR - + * and without the NUL suffix too. */ if (telnet->opt_states[o_we_bin.index] == ACTIVE) - telnet->bufsize = sk_write(telnet->s, "\r", 2); + telnet->bufsize = sk_write(telnet->s, "\r", 1); else telnet->bufsize = sk_write(telnet->s, "\r\n", 2); break; @@ -912,6 +955,29 @@ static void telnet_special(void *handle, Telnet_Special code) } } +static const struct telnet_special *telnet_get_specials(void *handle) +{ + static const struct telnet_special specials[] = { + {"Are You There", TS_AYT}, + {"Break", TS_BRK}, + {"Synch", TS_SYNCH}, + {"Erase Character", TS_EC}, + {"Erase Line", TS_EL}, + {"Go Ahead", TS_GA}, + {"No Operation", TS_NOP}, + {"", 0}, + {"Abort Process", TS_ABORT}, + {"Abort Output", TS_AO}, + {"Interrupt Process", TS_IP}, + {"Suspend Process", TS_SUSP}, + {"", 0}, + {"End Of Record", TS_EOR}, + {"End Of File", TS_EOF}, + {NULL, 0} + }; + return specials; +} + static Socket telnet_socket(void *handle) { Telnet telnet = (Telnet) handle; @@ -953,17 +1019,23 @@ static void telnet_provide_logctx(void *handle, void *logctx) static int telnet_exitcode(void *handle) { - /* Telnet telnet = (Telnet) handle; */ - /* Telnet doesn't transmit exit codes back to the client */ - return 0; + Telnet telnet = (Telnet) handle; + if (telnet->s != NULL) + return -1; /* still connected */ + else + /* Telnet doesn't transmit exit codes back to the client */ + return 0; } Backend telnet_backend = { telnet_init, + telnet_free, + telnet_reconfig, telnet_send, telnet_sendbuffer, telnet_size, telnet_special, + telnet_get_specials, telnet_socket, telnet_exitcode, telnet_sendok,