X-Git-Url: https://git.distorted.org.uk/u/mdw/putty/blobdiff_plain/656fa24481da0ce15dc352c70c5b79815b00a930..6226c9390d23d6868edde63b9568891cd88631cc:/telnet.c diff --git a/telnet.c b/telnet.c index 9d825095..0d32f8c2 100644 --- a/telnet.c +++ b/telnet.c @@ -1,3 +1,7 @@ +/* + * Telnet backend. + */ + #include #include @@ -72,6 +76,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 +149,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 ""; @@ -170,12 +200,10 @@ static const struct Opt o_tspeed = { WILL, WONT, DO, DONT, TELOPT_TSPEED, OPTINDEX_TSPEED, REQUESTED }; static const struct Opt o_ttype = { WILL, WONT, DO, DONT, TELOPT_TTYPE, OPTINDEX_TTYPE, REQUESTED }; -static const struct Opt o_oenv = { WILL, WONT, DO, DONT, TELOPT_OLD_ENVIRON, - OPTINDEX_OENV, INACTIVE -}; -static const struct Opt o_nenv = { WILL, WONT, DO, DONT, TELOPT_NEW_ENVIRON, - OPTINDEX_NENV, REQUESTED -}; +static const struct Opt o_oenv = + { WILL, WONT, DO, DONT, TELOPT_OLD_ENVIRON, OPTINDEX_OENV, INACTIVE }; +static const struct Opt o_nenv = + { WILL, WONT, DO, DONT, TELOPT_NEW_ENVIRON, OPTINDEX_NENV, REQUESTED }; static const struct Opt o_echo = { DO, DONT, WILL, WONT, TELOPT_ECHO, OPTINDEX_ECHO, REQUESTED }; static const struct Opt o_we_sga = @@ -209,7 +237,7 @@ typedef struct telnet_tag { int bufsize; int in_synch; int sb_opt, sb_len; - char *sb_buf; + unsigned char *sb_buf; int sb_size; enum { @@ -217,6 +245,9 @@ typedef struct telnet_tag { SEENSB, SUBNEGOT, SUBNEG_IAC, SEENCR } state; + Config cfg; + + Pinger pinger; } *Telnet; #define TELNET_MAX_BACKLOG 4096 @@ -254,7 +285,7 @@ static void send_opt(Telnet telnet, int cmd, int option) b[0] = IAC; b[1] = cmd; b[2] = option; - telnet->bufsize = sk_write(telnet->s, b, 3); + telnet->bufsize = sk_write(telnet->s, (char *)b, 3); log_option(telnet, "client", cmd, option); } @@ -367,9 +398,12 @@ static void proc_rec_opt(Telnet telnet, int cmd, int option) } /* * If we reach here, the option was one we weren't prepared to - * cope with. So send a negative ack. + * cope with. If the request was positive (WILL or DO), we send + * a negative ack to indicate refusal. If the request was + * negative (WONT / DONT), we must do nothing. */ - send_opt(telnet, (cmd == WILL ? DONT : WONT), option); + if (cmd == WILL || cmd == DO) + send_opt(telnet, (cmd == WILL ? DONT : WONT), option); } static void process_subneg(Telnet telnet) @@ -386,13 +420,13 @@ static void process_subneg(Telnet telnet) b[1] = SB; b[2] = TELOPT_TSPEED; b[3] = TELQUAL_IS; - strcpy(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, b, n + 2); + 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,14 +439,14 @@ 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, b, n + 6); + telnet->bufsize = sk_write(telnet->s, (char *)b, n + 6); b[n + 4] = 0; logevent(telnet->frontend, "server:\tSB TTYPE SEND"); logbuf = dupprintf("client:\tSB TTYPE IS %s", b + 4); @@ -432,7 +466,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 +499,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,22 +511,27 @@ 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++; } b[n++] = IAC; b[n++] = SE; - telnet->bufsize = sk_write(telnet->s, b, n); - logbuf = dupprintf("client:\tSB %s IS %s", telopt(telnet->sb_opt), - n == 6 ? "" : ""); + telnet->bufsize = sk_write(telnet->s, (char *)b, n); + logbuf = dupprintf("client:\tSB %s IS %s%s%s%s", + telopt(telnet->sb_opt), + *telnet->cfg.username ? "USER=" : "", + telnet->cfg.username, + *telnet->cfg.username ? " " : "", + n == 6 ? "" : + (*telnet->cfg.environmt ? "" : "")); logevent(telnet->frontend, logbuf); sfree(logbuf); } @@ -584,18 +623,11 @@ static void do_telnet_read(Telnet telnet, char *buf, int len) else { subneg_addchar: if (telnet->sb_len >= telnet->sb_size) { - 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; @@ -611,7 +643,23 @@ static void do_telnet_read(Telnet telnet, char *buf, int len) } } -static int telnet_closing(Plug plug, char *error_msg, int error_code, +static void telnet_log(Plug plug, int type, SockAddr addr, int port, + const char *error_msg, int error_code) +{ + Telnet telnet = (Telnet) plug; + char addrbuf[256], *msg; + + sk_getaddr(addr, addrbuf, lenof(addrbuf)); + + if (type == 0) + msg = dupprintf("Connecting to %s port %d", addrbuf, port); + else + msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg); + + logevent(telnet->frontend, msg); +} + +static int telnet_closing(Plug plug, const char *error_msg, int error_code, int calling_back) { Telnet telnet = (Telnet) plug; @@ -619,12 +667,13 @@ static int telnet_closing(Plug plug, char *error_msg, int error_code, if (telnet->s) { sk_close(telnet->s); telnet->s = NULL; + notify_remote_exit(telnet->frontend); } if (error_msg) { - /* A socket error has occurred. */ logevent(telnet->frontend, error_msg); - connection_fatal("%s", error_msg); - } /* Otherwise, the remote side closed the connection normally. */ + connection_fatal(telnet->frontend, "%s", error_msg); + } + /* Otherwise, the remote side closed the connection normally. */ return 0; } @@ -651,20 +700,24 @@ static void telnet_sent(Plug plug, int bufsize) * Also places the canonical host name into `realhost'. It must be * freed by the caller. */ -static char *telnet_init(void *frontend_handle, void **backend_handle, - char *host, int port, char **realhost, int nodelay) +static const char *telnet_init(void *frontend_handle, void **backend_handle, + Config *cfg, + char *host, int port, char **realhost, + int nodelay, int keepalive) { static const struct plug_function_table fn_table = { + telnet_log, telnet_closing, telnet_receive, telnet_sent }; SockAddr addr; - char *err; + const 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,9 +725,11 @@ 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; + telnet->ldisc = NULL; + telnet->pinger = NULL; *backend_handle = telnet; /* @@ -682,13 +737,18 @@ static char *telnet_init(void *frontend_handle, void **backend_handle, */ { char *buf; - buf = dupprintf("Looking up host \"%s\"", host); + buf = dupprintf("Looking up host \"%s\"%s", host, + (cfg->addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" : + (cfg->addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" : + ""))); logevent(telnet->frontend, buf); sfree(buf); } - addr = name_lookup(host, port, realhost); - if ((err = sk_addr_error(addr))) + addr = name_lookup(host, port, realhost, &telnet->cfg, cfg->addressfamily); + if ((err = sk_addr_error(addr)) != NULL) { + sk_addr_free(addr); return err; + } if (port < 0) port = 23; /* default telnet port */ @@ -696,24 +756,17 @@ static char *telnet_init(void *frontend_handle, void **backend_handle, /* * Open socket. */ - { - char *buf, addrbuf[100]; - sk_getaddr(addr, addrbuf, 100); - buf = dupprintf("Connecting to %s port %d", addrbuf, port); - logevent(telnet->frontend, buf); - sfree(buf); - } telnet->s = new_connection(addr, *realhost, port, 0, 1, - nodelay, (Plug) telnet); - if ((err = sk_socket_error(telnet->s))) + nodelay, keepalive, (Plug) telnet, &telnet->cfg); + if ((err = sk_socket_error(telnet->s)) != NULL) return err; - sk_addr_free(addr); + telnet->pinger = pinger_new(&telnet->cfg, &telnet_backend, telnet); /* * Initialise option states. */ - if (cfg.passive_telnet) { + if (telnet->cfg.passive_telnet) { const struct Opt *const *o; for (o = opts; *o; o++) @@ -734,36 +787,65 @@ static char *telnet_init(void *frontend_handle, void **backend_handle, */ telnet->in_synch = FALSE; + /* + * We can send special commands from the start. + */ + update_specials_menu(telnet->frontend); + return NULL; } +static void telnet_free(void *handle) +{ + Telnet telnet = (Telnet) handle; + + sfree(telnet->sb_buf); + if (telnet->s) + sk_close(telnet->s); + if (telnet->pinger) + pinger_free(telnet->pinger); + 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; + pinger_reconfig(telnet->pinger, &telnet->cfg, cfg); + telnet->cfg = *cfg; /* STRUCTURE COPY */ +} + /* * Called to send data down the Telnet connection. */ static int telnet_send(void *handle, char *buf, int len) { Telnet telnet = (Telnet) handle; - char *p; - static unsigned char iac[2] = { IAC, IAC }; - static unsigned char cr[2] = { CR, NUL }; + unsigned char *p, *end; + 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) return 0; - p = buf; - while (p < buf + len) { - char *q = p; + p = (unsigned char *)buf; + end = (unsigned char *)(buf + len); + while (p < end) { + unsigned char *q = p; - while (p < buf + len && iswritable((unsigned char) *p)) + while (p < end && iswritable(*p)) p++; - telnet->bufsize = sk_write(telnet->s, q, p - q); + telnet->bufsize = sk_write(telnet->s, (char *)q, p - q); - while (p < buf + len && !iswritable((unsigned char) *p)) { + while (p < end && !iswritable(*p)) { telnet->bufsize = - sk_write(telnet->s, (unsigned char) *p == IAC ? iac : cr, 2); + sk_write(telnet->s, (char *)(*p == IAC ? iac : cr), 2); p++; } } @@ -786,7 +868,8 @@ static int telnet_sendbuffer(void *handle) static void telnet_size(void *handle, int width, int height) { Telnet telnet = (Telnet) handle; - unsigned char b[16]; + unsigned char b[24]; + int n; char *logbuf; telnet->term_width = width; @@ -794,19 +877,23 @@ static void telnet_size(void *handle, int width, int height) if (telnet->s == NULL || telnet->opt_states[o_naws.index] != ACTIVE) return; - b[0] = IAC; - b[1] = SB; - b[2] = TELOPT_NAWS; - b[3] = telnet->term_width >> 8; - b[4] = telnet->term_width & 0xFF; - b[5] = telnet->term_height >> 8; - b[6] = telnet->term_height & 0xFF; - b[7] = IAC; - b[8] = SE; - telnet->bufsize = sk_write(telnet->s, b, 9); + n = 0; + b[n++] = IAC; + b[n++] = SB; + b[n++] = TELOPT_NAWS; + b[n++] = telnet->term_width >> 8; + if (b[n-1] == IAC) b[n++] = IAC; /* duplicate any IAC byte occurs */ + b[n++] = telnet->term_width & 0xFF; + if (b[n-1] == IAC) b[n++] = IAC; /* duplicate any IAC byte occurs */ + b[n++] = telnet->term_height >> 8; + if (b[n-1] == IAC) b[n++] = IAC; /* duplicate any IAC byte occurs */ + b[n++] = telnet->term_height & 0xFF; + if (b[n-1] == IAC) b[n++] = IAC; /* duplicate any IAC byte occurs */ + b[n++] = IAC; + b[n++] = SE; + telnet->bufsize = sk_write(telnet->s, (char *)b, n); logbuf = dupprintf("client:\tSB NAWS %d,%d", - ((unsigned char) b[3] << 8) + (unsigned char) b[4], - ((unsigned char) b[5] << 8) + (unsigned char) b[6]); + telnet->term_width, telnet->term_height); logevent(telnet->frontend, logbuf); sfree(logbuf); } @@ -826,63 +913,64 @@ static void telnet_special(void *handle, Telnet_Special code) switch (code) { case TS_AYT: b[1] = AYT; - telnet->bufsize = sk_write(telnet->s, b, 2); + telnet->bufsize = sk_write(telnet->s, (char *)b, 2); break; case TS_BRK: b[1] = BREAK; - telnet->bufsize = sk_write(telnet->s, b, 2); + telnet->bufsize = sk_write(telnet->s, (char *)b, 2); break; case TS_EC: b[1] = EC; - telnet->bufsize = sk_write(telnet->s, b, 2); + telnet->bufsize = sk_write(telnet->s, (char *)b, 2); break; case TS_EL: b[1] = EL; - telnet->bufsize = sk_write(telnet->s, b, 2); + telnet->bufsize = sk_write(telnet->s, (char *)b, 2); break; case TS_GA: b[1] = GA; - telnet->bufsize = sk_write(telnet->s, b, 2); + telnet->bufsize = sk_write(telnet->s, (char *)b, 2); break; case TS_NOP: b[1] = NOP; - telnet->bufsize = sk_write(telnet->s, b, 2); + telnet->bufsize = sk_write(telnet->s, (char *)b, 2); break; case TS_ABORT: b[1] = ABORT; - telnet->bufsize = sk_write(telnet->s, b, 2); + telnet->bufsize = sk_write(telnet->s, (char *)b, 2); break; case TS_AO: b[1] = AO; - telnet->bufsize = sk_write(telnet->s, b, 2); + telnet->bufsize = sk_write(telnet->s, (char *)b, 2); break; case TS_IP: b[1] = IP; - telnet->bufsize = sk_write(telnet->s, b, 2); + telnet->bufsize = sk_write(telnet->s, (char *)b, 2); break; case TS_SUSP: b[1] = SUSP; - telnet->bufsize = sk_write(telnet->s, b, 2); + telnet->bufsize = sk_write(telnet->s, (char *)b, 2); break; case TS_EOR: b[1] = EOR; - telnet->bufsize = sk_write(telnet->s, b, 2); + telnet->bufsize = sk_write(telnet->s, (char *)b, 2); break; case TS_EOF: b[1] = xEOF; - telnet->bufsize = sk_write(telnet->s, b, 2); + 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; case TS_SYNCH: b[1] = DM; - telnet->bufsize = sk_write(telnet->s, b, 1); - telnet->bufsize = sk_write_oob(telnet->s, b + 1, 1); + telnet->bufsize = sk_write(telnet->s, (char *)b, 1); + telnet->bufsize = sk_write_oob(telnet->s, (char *)(b + 1), 1); break; case TS_RECHO: if (telnet->opt_states[o_echo.index] == INACTIVE || @@ -900,16 +988,41 @@ static void telnet_special(void *handle, Telnet_Special code) case TS_PING: if (telnet->opt_states[o_they_sga.index] == ACTIVE) { b[1] = NOP; - telnet->bufsize = sk_write(telnet->s, b, 2); + telnet->bufsize = sk_write(telnet->s, (char *)b, 2); } break; + default: + break; /* never heard of it */ } } -static Socket telnet_socket(void *handle) +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}, + {NULL, TS_SEP}, + {"Abort Process", TS_ABORT}, + {"Abort Output", TS_AO}, + {"Interrupt Process", TS_IP}, + {"Suspend Process", TS_SUSP}, + {NULL, TS_SEP}, + {"End Of Record", TS_EOR}, + {"End Of File", TS_EOF}, + {NULL, TS_EXITMENU} + }; + return specials; +} + +static int telnet_connected(void *handle) { Telnet telnet = (Telnet) handle; - return telnet->s; + return telnet->s != NULL; } static int telnet_sendok(void *handle) @@ -947,23 +1060,38 @@ 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 */ + 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; +} + +/* + * cfg_info for Telnet does nothing at all. + */ +static int telnet_cfg_info(void *handle) +{ return 0; } Backend telnet_backend = { telnet_init, + telnet_free, + telnet_reconfig, telnet_send, telnet_sendbuffer, telnet_size, telnet_special, - telnet_socket, + telnet_get_specials, + telnet_connected, telnet_exitcode, telnet_sendok, telnet_ldisc, telnet_provide_ldisc, telnet_provide_logctx, telnet_unthrottle, + telnet_cfg_info, 23 };