telnet.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  1. /*
  2. * Telnet backend.
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <limits.h>
  7. #include "putty.h"
  8. #define IAC 255 /* interpret as command: */
  9. #define DONT 254 /* you are not to use option */
  10. #define DO 253 /* please, you use option */
  11. #define WONT 252 /* I won't use option */
  12. #define WILL 251 /* I will use option */
  13. #define SB 250 /* interpret as subnegotiation */
  14. #define SE 240 /* end sub negotiation */
  15. #define GA 249 /* you may reverse the line */
  16. #define EL 248 /* erase the current line */
  17. #define EC 247 /* erase the current character */
  18. #define AYT 246 /* are you there */
  19. #define AO 245 /* abort output--but let prog finish */
  20. #define IP 244 /* interrupt process--permanently */
  21. #define BREAK 243 /* break */
  22. #define DM 242 /* data mark--for connect. cleaning */
  23. #define NOP 241 /* nop */
  24. #define EOR 239 /* end of record (transparent mode) */
  25. #define ABORT 238 /* Abort process */
  26. #define SUSP 237 /* Suspend process */
  27. #define xEOF 236 /* End of file: EOF is already used... */
  28. #define TELOPTS(X) \
  29. X(BINARY, 0) /* 8-bit data path */ \
  30. X(ECHO, 1) /* echo */ \
  31. X(RCP, 2) /* prepare to reconnect */ \
  32. X(SGA, 3) /* suppress go ahead */ \
  33. X(NAMS, 4) /* approximate message size */ \
  34. X(STATUS, 5) /* give status */ \
  35. X(TM, 6) /* timing mark */ \
  36. X(RCTE, 7) /* remote controlled transmission and echo */ \
  37. X(NAOL, 8) /* negotiate about output line width */ \
  38. X(NAOP, 9) /* negotiate about output page size */ \
  39. X(NAOCRD, 10) /* negotiate about CR disposition */ \
  40. X(NAOHTS, 11) /* negotiate about horizontal tabstops */ \
  41. X(NAOHTD, 12) /* negotiate about horizontal tab disposition */ \
  42. X(NAOFFD, 13) /* negotiate about formfeed disposition */ \
  43. X(NAOVTS, 14) /* negotiate about vertical tab stops */ \
  44. X(NAOVTD, 15) /* negotiate about vertical tab disposition */ \
  45. X(NAOLFD, 16) /* negotiate about output LF disposition */ \
  46. X(XASCII, 17) /* extended ascic character set */ \
  47. X(LOGOUT, 18) /* force logout */ \
  48. X(BM, 19) /* byte macro */ \
  49. X(DET, 20) /* data entry terminal */ \
  50. X(SUPDUP, 21) /* supdup protocol */ \
  51. X(SUPDUPOUTPUT, 22) /* supdup output */ \
  52. X(SNDLOC, 23) /* send location */ \
  53. X(TTYPE, 24) /* terminal type */ \
  54. X(EOR, 25) /* end or record */ \
  55. X(TUID, 26) /* TACACS user identification */ \
  56. X(OUTMRK, 27) /* output marking */ \
  57. X(TTYLOC, 28) /* terminal location number */ \
  58. X(3270REGIME, 29) /* 3270 regime */ \
  59. X(X3PAD, 30) /* X.3 PAD */ \
  60. X(NAWS, 31) /* window size */ \
  61. X(TSPEED, 32) /* terminal speed */ \
  62. X(LFLOW, 33) /* remote flow control */ \
  63. X(LINEMODE, 34) /* Linemode option */ \
  64. X(XDISPLOC, 35) /* X Display Location */ \
  65. X(OLD_ENVIRON, 36) /* Old - Environment variables */ \
  66. X(AUTHENTICATION, 37) /* Authenticate */ \
  67. X(ENCRYPT, 38) /* Encryption option */ \
  68. X(NEW_ENVIRON, 39) /* New - Environment variables */ \
  69. X(TN3270E, 40) /* TN3270 enhancements */ \
  70. X(XAUTH, 41) \
  71. X(CHARSET, 42) /* Character set */ \
  72. X(RSP, 43) /* Remote serial port */ \
  73. X(COM_PORT_OPTION, 44) /* Com port control */ \
  74. X(SLE, 45) /* Suppress local echo */ \
  75. X(STARTTLS, 46) /* Start TLS */ \
  76. X(KERMIT, 47) /* Automatic Kermit file transfer */ \
  77. X(SEND_URL, 48) \
  78. X(FORWARD_X, 49) \
  79. X(PRAGMA_LOGON, 138) \
  80. X(SSPI_LOGON, 139) \
  81. X(PRAGMA_HEARTBEAT, 140) \
  82. X(EXOPL, 255) /* extended-options-list */
  83. #define telnet_enum(x,y) TELOPT_##x = y,
  84. enum { TELOPTS(telnet_enum) dummy=0 };
  85. #undef telnet_enum
  86. #define TELQUAL_IS 0 /* option is... */
  87. #define TELQUAL_SEND 1 /* send option */
  88. #define TELQUAL_INFO 2 /* ENVIRON: informational version of IS */
  89. #define BSD_VAR 1
  90. #define BSD_VALUE 0
  91. #define RFC_VAR 0
  92. #define RFC_VALUE 1
  93. #define CR 13
  94. #define LF 10
  95. #define NUL 0
  96. #define iswritable(x) \
  97. ( (x) != IAC && \
  98. (telnet->opt_states[o_we_bin.index] == ACTIVE || (x) != CR))
  99. static const char *telopt(int opt)
  100. {
  101. #define telnet_str(x,y) case TELOPT_##x: return #x;
  102. switch (opt) {
  103. TELOPTS(telnet_str)
  104. default:
  105. return "<unknown>";
  106. }
  107. #undef telnet_str
  108. }
  109. struct Opt {
  110. int send; /* what we initially send */
  111. int nsend; /* -ve send if requested to stop it */
  112. int ack, nak; /* +ve and -ve acknowledgements */
  113. int option; /* the option code */
  114. int index; /* index into telnet->opt_states[] */
  115. enum {
  116. REQUESTED, ACTIVE, INACTIVE, REALLY_INACTIVE
  117. } initial_state;
  118. };
  119. enum {
  120. OPTINDEX_NAWS,
  121. OPTINDEX_TSPEED,
  122. OPTINDEX_TTYPE,
  123. OPTINDEX_OENV,
  124. OPTINDEX_NENV,
  125. OPTINDEX_ECHO,
  126. OPTINDEX_WE_SGA,
  127. OPTINDEX_THEY_SGA,
  128. OPTINDEX_WE_BIN,
  129. OPTINDEX_THEY_BIN,
  130. NUM_OPTS
  131. };
  132. static const struct Opt o_naws =
  133. { WILL, WONT, DO, DONT, TELOPT_NAWS, OPTINDEX_NAWS, REQUESTED };
  134. static const struct Opt o_tspeed =
  135. { WILL, WONT, DO, DONT, TELOPT_TSPEED, OPTINDEX_TSPEED, REQUESTED };
  136. static const struct Opt o_ttype =
  137. { WILL, WONT, DO, DONT, TELOPT_TTYPE, OPTINDEX_TTYPE, REQUESTED };
  138. static const struct Opt o_oenv =
  139. { WILL, WONT, DO, DONT, TELOPT_OLD_ENVIRON, OPTINDEX_OENV, INACTIVE };
  140. static const struct Opt o_nenv =
  141. { WILL, WONT, DO, DONT, TELOPT_NEW_ENVIRON, OPTINDEX_NENV, REQUESTED };
  142. static const struct Opt o_echo =
  143. { DO, DONT, WILL, WONT, TELOPT_ECHO, OPTINDEX_ECHO, REQUESTED };
  144. static const struct Opt o_we_sga =
  145. { WILL, WONT, DO, DONT, TELOPT_SGA, OPTINDEX_WE_SGA, REQUESTED };
  146. static const struct Opt o_they_sga =
  147. { DO, DONT, WILL, WONT, TELOPT_SGA, OPTINDEX_THEY_SGA, REQUESTED };
  148. static const struct Opt o_we_bin =
  149. { WILL, WONT, DO, DONT, TELOPT_BINARY, OPTINDEX_WE_BIN, INACTIVE };
  150. static const struct Opt o_they_bin =
  151. { DO, DONT, WILL, WONT, TELOPT_BINARY, OPTINDEX_THEY_BIN, INACTIVE };
  152. static const struct Opt *const opts[] = {
  153. &o_naws, &o_tspeed, &o_ttype, &o_oenv, &o_nenv, &o_echo,
  154. &o_we_sga, &o_they_sga, &o_we_bin, &o_they_bin, NULL
  155. };
  156. typedef struct Telnet Telnet;
  157. struct Telnet {
  158. Socket *s;
  159. bool socket_connected;
  160. bool closed_on_socket_error;
  161. Seat *seat;
  162. LogContext *logctx;
  163. Ldisc *ldisc;
  164. int term_width, term_height;
  165. char *description;
  166. int opt_states[NUM_OPTS];
  167. bool echoing, editing;
  168. bool activated;
  169. size_t bufsize;
  170. bool in_synch;
  171. int sb_opt;
  172. strbuf *sb_buf;
  173. enum {
  174. TOP_LEVEL, SEENIAC, SEENWILL, SEENWONT, SEENDO, SEENDONT,
  175. SEENSB, SUBNEGOT, SUBNEG_IAC, SEENCR
  176. } state;
  177. Conf *conf;
  178. Pinger *pinger;
  179. Plug plug;
  180. Backend backend;
  181. Interactor interactor;
  182. };
  183. #define TELNET_MAX_BACKLOG 4096
  184. #define SB_DELTA 1024
  185. static void c_write(Telnet *telnet, const void *buf, size_t len)
  186. {
  187. size_t backlog = seat_stdout(telnet->seat, buf, len);
  188. sk_set_frozen(telnet->s, backlog > TELNET_MAX_BACKLOG);
  189. }
  190. static void log_option(Telnet *telnet, const char *sender, int cmd, int option)
  191. {
  192. /*
  193. * The strange-looking "<?""?>" below is there to avoid a
  194. * trigraph - a double question mark followed by > maps to a
  195. * closing brace character!
  196. */
  197. logeventf(telnet->logctx, "%s negotiation: %s %s", sender,
  198. (cmd == WILL ? "WILL" : cmd == WONT ? "WONT" :
  199. cmd == DO ? "DO" : cmd == DONT ? "DONT" : "<?""?>"),
  200. telopt(option));
  201. }
  202. static void send_opt(Telnet *telnet, int cmd, int option)
  203. {
  204. unsigned char b[3];
  205. b[0] = IAC;
  206. b[1] = cmd;
  207. b[2] = option;
  208. telnet->bufsize = sk_write(telnet->s, b, 3);
  209. log_option(telnet, "client", cmd, option);
  210. }
  211. static void deactivate_option(Telnet *telnet, const struct Opt *o)
  212. {
  213. if (telnet->opt_states[o->index] == REQUESTED ||
  214. telnet->opt_states[o->index] == ACTIVE)
  215. send_opt(telnet, o->nsend, o->option);
  216. telnet->opt_states[o->index] = REALLY_INACTIVE;
  217. }
  218. /*
  219. * Generate side effects of enabling or disabling an option.
  220. */
  221. static void option_side_effects(
  222. Telnet *telnet, const struct Opt *o, bool enabled)
  223. {
  224. if (o->option == TELOPT_ECHO && o->send == DO)
  225. telnet->echoing = !enabled;
  226. else if (o->option == TELOPT_SGA && o->send == DO)
  227. telnet->editing = !enabled;
  228. if (telnet->ldisc) /* cause ldisc to notice the change */
  229. ldisc_echoedit_update(telnet->ldisc);
  230. /* Ensure we get the minimum options */
  231. if (!telnet->activated) {
  232. if (telnet->opt_states[o_echo.index] == INACTIVE) {
  233. telnet->opt_states[o_echo.index] = REQUESTED;
  234. send_opt(telnet, o_echo.send, o_echo.option);
  235. }
  236. if (telnet->opt_states[o_we_sga.index] == INACTIVE) {
  237. telnet->opt_states[o_we_sga.index] = REQUESTED;
  238. send_opt(telnet, o_we_sga.send, o_we_sga.option);
  239. }
  240. if (telnet->opt_states[o_they_sga.index] == INACTIVE) {
  241. telnet->opt_states[o_they_sga.index] = REQUESTED;
  242. send_opt(telnet, o_they_sga.send, o_they_sga.option);
  243. }
  244. telnet->activated = true;
  245. }
  246. }
  247. static void activate_option(Telnet *telnet, const struct Opt *o)
  248. {
  249. if (o->send == WILL && o->option == TELOPT_NAWS)
  250. backend_size(&telnet->backend,
  251. telnet->term_width, telnet->term_height);
  252. if (o->send == WILL &&
  253. (o->option == TELOPT_NEW_ENVIRON ||
  254. o->option == TELOPT_OLD_ENVIRON)) {
  255. /*
  256. * We may only have one kind of ENVIRON going at a time.
  257. * This is a hack, but who cares.
  258. */
  259. deactivate_option(telnet, o->option ==
  260. TELOPT_NEW_ENVIRON ? &o_oenv : &o_nenv);
  261. }
  262. option_side_effects(telnet, o, true);
  263. }
  264. static void refused_option(Telnet *telnet, const struct Opt *o)
  265. {
  266. if (o->send == WILL && o->option == TELOPT_NEW_ENVIRON &&
  267. telnet->opt_states[o_oenv.index] == INACTIVE) {
  268. send_opt(telnet, WILL, TELOPT_OLD_ENVIRON);
  269. telnet->opt_states[o_oenv.index] = REQUESTED;
  270. }
  271. option_side_effects(telnet, o, false);
  272. }
  273. static void proc_rec_opt(Telnet *telnet, int cmd, int option)
  274. {
  275. const struct Opt *const *o;
  276. log_option(telnet, "server", cmd, option);
  277. for (o = opts; *o; o++) {
  278. if ((*o)->option == option && (*o)->ack == cmd) {
  279. switch (telnet->opt_states[(*o)->index]) {
  280. case REQUESTED:
  281. telnet->opt_states[(*o)->index] = ACTIVE;
  282. activate_option(telnet, *o);
  283. break;
  284. case ACTIVE:
  285. break;
  286. case INACTIVE:
  287. telnet->opt_states[(*o)->index] = ACTIVE;
  288. send_opt(telnet, (*o)->send, option);
  289. activate_option(telnet, *o);
  290. break;
  291. case REALLY_INACTIVE:
  292. send_opt(telnet, (*o)->nsend, option);
  293. break;
  294. }
  295. return;
  296. } else if ((*o)->option == option && (*o)->nak == cmd) {
  297. switch (telnet->opt_states[(*o)->index]) {
  298. case REQUESTED:
  299. telnet->opt_states[(*o)->index] = INACTIVE;
  300. refused_option(telnet, *o);
  301. break;
  302. case ACTIVE:
  303. telnet->opt_states[(*o)->index] = INACTIVE;
  304. send_opt(telnet, (*o)->nsend, option);
  305. option_side_effects(telnet, *o, false);
  306. break;
  307. case INACTIVE:
  308. case REALLY_INACTIVE:
  309. break;
  310. }
  311. return;
  312. }
  313. }
  314. /*
  315. * If we reach here, the option was one we weren't prepared to
  316. * cope with. If the request was positive (WILL or DO), we send
  317. * a negative ack to indicate refusal. If the request was
  318. * negative (WONT / DONT), we must do nothing.
  319. */
  320. if (cmd == WILL || cmd == DO)
  321. send_opt(telnet, (cmd == WILL ? DONT : WONT), option);
  322. }
  323. static void process_subneg(Telnet *telnet)
  324. {
  325. unsigned char *p, *q;
  326. int var, value;
  327. switch (telnet->sb_opt) {
  328. case TELOPT_TSPEED:
  329. if (telnet->sb_buf->len == 1 && telnet->sb_buf->u[0] == TELQUAL_SEND) {
  330. char *termspeed = conf_get_str(telnet->conf, CONF_termspeed);
  331. strbuf *sb = strbuf_new();
  332. put_byte(sb, IAC);
  333. put_byte(sb, SB);
  334. put_byte(sb, TELOPT_TSPEED);
  335. put_byte(sb, TELQUAL_IS);
  336. put_datapl(sb, ptrlen_from_asciz(termspeed));
  337. put_byte(sb, IAC);
  338. put_byte(sb, SE);
  339. telnet->bufsize = sk_write(telnet->s, sb->s, sb->len);
  340. logevent(telnet->logctx, "server subnegotiation: SB TSPEED SEND");
  341. logeventf(telnet->logctx,
  342. "client subnegotiation: SB TSPEED IS %s", termspeed);
  343. strbuf_free(sb);
  344. } else
  345. logevent(telnet->logctx,
  346. "server subnegotiation: SB TSPEED <something weird>");
  347. break;
  348. case TELOPT_TTYPE:
  349. if (telnet->sb_buf->len == 1 && telnet->sb_buf->u[0] == TELQUAL_SEND) {
  350. char *termtype = conf_get_str(telnet->conf, CONF_termtype);
  351. strbuf *sb = strbuf_new();
  352. put_byte(sb, IAC);
  353. put_byte(sb, SB);
  354. put_byte(sb, TELOPT_TTYPE);
  355. put_byte(sb, TELQUAL_IS);
  356. size_t tt_start = sb->len;
  357. for (size_t n = 0; termtype[n]; n++)
  358. put_byte(sb, (termtype[n] >= 'a' && termtype[n] <= 'z' ?
  359. termtype[n] + 'A' - 'a' : termtype[n]));
  360. size_t tt_end = sb->len;
  361. put_byte(sb, IAC);
  362. put_byte(sb, SE);
  363. telnet->bufsize = sk_write(telnet->s, sb->s, sb->len);
  364. strbuf_shrink_to(sb, tt_end);
  365. logevent(telnet->logctx, "server subnegotiation: SB TTYPE SEND");
  366. logeventf(telnet->logctx, "client subnegotiation: SB TTYPE IS %s",
  367. sb->s + tt_start);
  368. strbuf_free(sb);
  369. } else
  370. logevent(telnet->logctx,
  371. "server subnegotiation: SB TTYPE <something weird>\r\n");
  372. break;
  373. case TELOPT_OLD_ENVIRON:
  374. case TELOPT_NEW_ENVIRON:
  375. p = telnet->sb_buf->u;
  376. q = p + telnet->sb_buf->len;
  377. if (p < q && *p == TELQUAL_SEND) {
  378. p++;
  379. logeventf(telnet->logctx, "server subnegotiation: SB %s SEND",
  380. telopt(telnet->sb_opt));
  381. if (telnet->sb_opt == TELOPT_OLD_ENVIRON) {
  382. if (conf_get_bool(telnet->conf, CONF_rfc_environ)) {
  383. value = RFC_VALUE;
  384. var = RFC_VAR;
  385. } else {
  386. value = BSD_VALUE;
  387. var = BSD_VAR;
  388. }
  389. /*
  390. * Try to guess the sense of VAR and VALUE.
  391. */
  392. while (p < q) {
  393. if (*p == RFC_VAR) {
  394. value = RFC_VALUE;
  395. var = RFC_VAR;
  396. } else if (*p == BSD_VAR) {
  397. value = BSD_VALUE;
  398. var = BSD_VAR;
  399. }
  400. p++;
  401. }
  402. } else {
  403. /*
  404. * With NEW_ENVIRON, the sense of VAR and VALUE
  405. * isn't in doubt.
  406. */
  407. value = RFC_VALUE;
  408. var = RFC_VAR;
  409. }
  410. strbuf *sb = strbuf_new();
  411. put_byte(sb, IAC);
  412. put_byte(sb, SB);
  413. put_byte(sb, telnet->sb_opt);
  414. put_byte(sb, TELQUAL_IS);
  415. char *ekey, *eval;
  416. for (eval = conf_get_str_strs(telnet->conf, CONF_environmt,
  417. NULL, &ekey);
  418. eval != NULL;
  419. eval = conf_get_str_strs(telnet->conf, CONF_environmt,
  420. ekey, &ekey)) {
  421. put_byte(sb, var);
  422. put_datapl(sb, ptrlen_from_asciz(ekey));
  423. put_byte(sb, value);
  424. put_datapl(sb, ptrlen_from_asciz(eval));
  425. }
  426. char *user = get_remote_username(telnet->conf);
  427. if (user) {
  428. put_byte(sb, var);
  429. put_datalit(sb, "USER");
  430. put_byte(sb, value);
  431. put_datapl(sb, ptrlen_from_asciz(user));
  432. }
  433. put_byte(sb, IAC);
  434. put_byte(sb, SE);
  435. telnet->bufsize = sk_write(telnet->s, sb->s, sb->len);
  436. if (sb->len == 6) {
  437. logeventf(telnet->logctx,
  438. "client subnegotiation: SB %s IS <nothing>",
  439. telopt(telnet->sb_opt));
  440. } else {
  441. logeventf(telnet->logctx, "client subnegotiation: SB %s IS:",
  442. telopt(telnet->sb_opt));
  443. for (eval = conf_get_str_strs(telnet->conf, CONF_environmt,
  444. NULL, &ekey);
  445. eval != NULL;
  446. eval = conf_get_str_strs(telnet->conf, CONF_environmt,
  447. ekey, &ekey)) {
  448. logeventf(telnet->logctx, " %s=%s", ekey, eval);
  449. }
  450. if (user)
  451. logeventf(telnet->logctx, " USER=%s", user);
  452. }
  453. strbuf_free(sb);
  454. sfree(user);
  455. }
  456. break;
  457. }
  458. }
  459. static void do_telnet_read(Telnet *telnet, const char *buf, size_t len)
  460. {
  461. strbuf *outbuf = strbuf_new_nm();
  462. while (len--) {
  463. int c = (unsigned char) *buf++;
  464. switch (telnet->state) {
  465. case TOP_LEVEL:
  466. case SEENCR:
  467. if (c == NUL && telnet->state == SEENCR)
  468. telnet->state = TOP_LEVEL;
  469. else if (c == IAC)
  470. telnet->state = SEENIAC;
  471. else {
  472. if (!telnet->in_synch)
  473. put_byte(outbuf, c);
  474. #if 1
  475. /* I can't get the F***ing winsock to insert the urgent IAC
  476. * into the right position! Even with SO_OOBINLINE it gives
  477. * it to recv too soon. And of course the DM byte (that
  478. * arrives in the same packet!) appears several K later!!
  479. *
  480. * Oh well, we do get the DM in the right place so I'll
  481. * just stop hiding on the next 0xf2 and hope for the best.
  482. */
  483. else if (c == DM)
  484. telnet->in_synch = false;
  485. #endif
  486. if (c == CR && telnet->opt_states[o_they_bin.index] != ACTIVE)
  487. telnet->state = SEENCR;
  488. else
  489. telnet->state = TOP_LEVEL;
  490. }
  491. break;
  492. case SEENIAC:
  493. if (c == DO)
  494. telnet->state = SEENDO;
  495. else if (c == DONT)
  496. telnet->state = SEENDONT;
  497. else if (c == WILL)
  498. telnet->state = SEENWILL;
  499. else if (c == WONT)
  500. telnet->state = SEENWONT;
  501. else if (c == SB)
  502. telnet->state = SEENSB;
  503. else if (c == DM) {
  504. telnet->in_synch = false;
  505. telnet->state = TOP_LEVEL;
  506. } else {
  507. /* ignore everything else; print it if it's IAC */
  508. if (c == IAC) {
  509. put_byte(outbuf, c);
  510. }
  511. telnet->state = TOP_LEVEL;
  512. }
  513. break;
  514. case SEENWILL:
  515. proc_rec_opt(telnet, WILL, c);
  516. telnet->state = TOP_LEVEL;
  517. break;
  518. case SEENWONT:
  519. proc_rec_opt(telnet, WONT, c);
  520. telnet->state = TOP_LEVEL;
  521. break;
  522. case SEENDO:
  523. proc_rec_opt(telnet, DO, c);
  524. telnet->state = TOP_LEVEL;
  525. break;
  526. case SEENDONT:
  527. proc_rec_opt(telnet, DONT, c);
  528. telnet->state = TOP_LEVEL;
  529. break;
  530. case SEENSB:
  531. telnet->sb_opt = c;
  532. strbuf_clear(telnet->sb_buf);
  533. telnet->state = SUBNEGOT;
  534. break;
  535. case SUBNEGOT:
  536. if (c == IAC)
  537. telnet->state = SUBNEG_IAC;
  538. else {
  539. subneg_addchar:
  540. put_byte(telnet->sb_buf, c);
  541. telnet->state = SUBNEGOT; /* in case we came here by goto */
  542. }
  543. break;
  544. case SUBNEG_IAC:
  545. if (c != SE)
  546. goto subneg_addchar; /* yes, it's a hack, I know, but... */
  547. else {
  548. process_subneg(telnet);
  549. telnet->state = TOP_LEVEL;
  550. }
  551. break;
  552. }
  553. if (outbuf->len >= 4096) {
  554. c_write(telnet, outbuf->u, outbuf->len);
  555. strbuf_clear(outbuf);
  556. }
  557. }
  558. if (outbuf->len)
  559. c_write(telnet, outbuf->u, outbuf->len);
  560. strbuf_free(outbuf);
  561. }
  562. static void telnet_log(Plug *plug, Socket *s, PlugLogType type, SockAddr *addr,
  563. int port, const char *error_msg, int error_code)
  564. {
  565. Telnet *telnet = container_of(plug, Telnet, plug);
  566. backend_socket_log(telnet->seat, telnet->logctx, s, type, addr, port,
  567. error_msg, error_code, telnet->conf,
  568. telnet->socket_connected);
  569. if (type == PLUGLOG_CONNECT_SUCCESS) {
  570. telnet->socket_connected = true;
  571. if (telnet->ldisc)
  572. ldisc_check_sendok(telnet->ldisc);
  573. }
  574. }
  575. static void telnet_closing(Plug *plug, PlugCloseType type,
  576. const char *error_msg)
  577. {
  578. Telnet *telnet = container_of(plug, Telnet, plug);
  579. /*
  580. * We don't implement independent EOF in each direction for Telnet
  581. * connections; as soon as we get word that the remote side has
  582. * sent us EOF, we wind up the whole connection.
  583. */
  584. if (telnet->s) {
  585. sk_close(telnet->s);
  586. telnet->s = NULL;
  587. if (error_msg)
  588. telnet->closed_on_socket_error = true;
  589. seat_notify_remote_exit(telnet->seat);
  590. seat_notify_remote_disconnect(telnet->seat);
  591. }
  592. if (type != PLUGCLOSE_NORMAL) {
  593. logevent(telnet->logctx, error_msg);
  594. if (type != PLUGCLOSE_USER_ABORT)
  595. seat_connection_fatal(telnet->seat, "%s", error_msg);
  596. }
  597. /* Otherwise, the remote side closed the connection normally. */
  598. }
  599. static void telnet_receive(
  600. Plug *plug, int urgent, const char *data, size_t len)
  601. {
  602. Telnet *telnet = container_of(plug, Telnet, plug);
  603. if (urgent)
  604. telnet->in_synch = true;
  605. do_telnet_read(telnet, data, len);
  606. }
  607. static void telnet_sent(Plug *plug, size_t bufsize)
  608. {
  609. Telnet *telnet = container_of(plug, Telnet, plug);
  610. telnet->bufsize = bufsize;
  611. seat_sent(telnet->seat, telnet->bufsize);
  612. }
  613. static const PlugVtable Telnet_plugvt = {
  614. .log = telnet_log,
  615. .closing = telnet_closing,
  616. .receive = telnet_receive,
  617. .sent = telnet_sent,
  618. };
  619. static char *telnet_description(Interactor *itr)
  620. {
  621. Telnet *telnet = container_of(itr, Telnet, interactor);
  622. return dupstr(telnet->description);
  623. }
  624. static LogPolicy *telnet_logpolicy(Interactor *itr)
  625. {
  626. Telnet *telnet = container_of(itr, Telnet, interactor);
  627. return log_get_policy(telnet->logctx);
  628. }
  629. static Seat *telnet_get_seat(Interactor *itr)
  630. {
  631. Telnet *telnet = container_of(itr, Telnet, interactor);
  632. return telnet->seat;
  633. }
  634. static void telnet_set_seat(Interactor *itr, Seat *seat)
  635. {
  636. Telnet *telnet = container_of(itr, Telnet, interactor);
  637. telnet->seat = seat;
  638. }
  639. static const InteractorVtable Telnet_interactorvt = {
  640. .description = telnet_description,
  641. .logpolicy = telnet_logpolicy,
  642. .get_seat = telnet_get_seat,
  643. .set_seat = telnet_set_seat,
  644. };
  645. /*
  646. * Called to set up the Telnet connection.
  647. *
  648. * Returns an error message, or NULL on success.
  649. *
  650. * Also places the canonical host name into `realhost'. It must be
  651. * freed by the caller.
  652. */
  653. static char *telnet_init(const BackendVtable *vt, Seat *seat,
  654. Backend **backend_handle, LogContext *logctx,
  655. Conf *conf, const char *host, int port,
  656. char **realhost, bool nodelay, bool keepalive)
  657. {
  658. SockAddr *addr;
  659. const char *err;
  660. Telnet *telnet;
  661. char *loghost;
  662. int addressfamily;
  663. telnet = snew(Telnet);
  664. memset(telnet, 0, sizeof(Telnet));
  665. telnet->plug.vt = &Telnet_plugvt;
  666. telnet->backend.vt = vt;
  667. telnet->interactor.vt = &Telnet_interactorvt;
  668. telnet->backend.interactor = &telnet->interactor;
  669. telnet->conf = conf_copy(conf);
  670. telnet->s = NULL;
  671. telnet->socket_connected = false;
  672. telnet->closed_on_socket_error = false;
  673. telnet->echoing = true;
  674. telnet->editing = true;
  675. telnet->activated = false;
  676. telnet->sb_buf = strbuf_new();
  677. telnet->seat = seat;
  678. telnet->logctx = logctx;
  679. telnet->term_width = conf_get_int(telnet->conf, CONF_width);
  680. telnet->term_height = conf_get_int(telnet->conf, CONF_height);
  681. telnet->state = TOP_LEVEL;
  682. telnet->ldisc = NULL;
  683. telnet->pinger = NULL;
  684. telnet->description = default_description(vt, host, port);
  685. *backend_handle = &telnet->backend;
  686. /*
  687. * Try to find host.
  688. */
  689. addressfamily = conf_get_int(telnet->conf, CONF_addressfamily);
  690. addr = name_lookup(host, port, realhost, telnet->conf, addressfamily,
  691. telnet->logctx, "Telnet connection");
  692. if ((err = sk_addr_error(addr)) != NULL) {
  693. sk_addr_free(addr);
  694. return dupstr(err);
  695. }
  696. if (port < 0)
  697. port = 23; /* default telnet port */
  698. /*
  699. * Open socket.
  700. */
  701. telnet->s = new_connection(addr, *realhost, port, false, true, nodelay,
  702. keepalive, &telnet->plug, telnet->conf,
  703. &telnet->interactor);
  704. if ((err = sk_socket_error(telnet->s)) != NULL)
  705. return dupstr(err);
  706. /* No local authentication phase in this protocol */
  707. seat_set_trust_status(telnet->seat, false);
  708. telnet->pinger = pinger_new(telnet->conf, &telnet->backend);
  709. /*
  710. * Initialise option states.
  711. */
  712. if (conf_get_bool(telnet->conf, CONF_passive_telnet)) {
  713. const struct Opt *const *o;
  714. for (o = opts; *o; o++)
  715. telnet->opt_states[(*o)->index] = INACTIVE;
  716. } else {
  717. const struct Opt *const *o;
  718. for (o = opts; *o; o++) {
  719. telnet->opt_states[(*o)->index] = (*o)->initial_state;
  720. if (telnet->opt_states[(*o)->index] == REQUESTED)
  721. send_opt(telnet, (*o)->send, (*o)->option);
  722. }
  723. telnet->activated = true;
  724. }
  725. /*
  726. * Set up SYNCH state.
  727. */
  728. telnet->in_synch = false;
  729. /*
  730. * We can send special commands from the start.
  731. */
  732. seat_update_specials_menu(telnet->seat);
  733. /*
  734. * loghost overrides realhost, if specified.
  735. */
  736. loghost = conf_get_str(telnet->conf, CONF_loghost);
  737. if (*loghost) {
  738. char *colon;
  739. sfree(*realhost);
  740. *realhost = dupstr(loghost);
  741. colon = host_strrchr(*realhost, ':');
  742. if (colon)
  743. *colon++ = '\0';
  744. }
  745. return NULL;
  746. }
  747. static void telnet_free(Backend *be)
  748. {
  749. Telnet *telnet = container_of(be, Telnet, backend);
  750. if (is_tempseat(telnet->seat))
  751. tempseat_free(telnet->seat);
  752. strbuf_free(telnet->sb_buf);
  753. if (telnet->s)
  754. sk_close(telnet->s);
  755. if (telnet->pinger)
  756. pinger_free(telnet->pinger);
  757. conf_free(telnet->conf);
  758. sfree(telnet->description);
  759. sfree(telnet);
  760. }
  761. /*
  762. * Reconfigure the Telnet backend. There's no immediate action
  763. * necessary, in this backend: we just save the fresh config for
  764. * any subsequent negotiations.
  765. */
  766. static void telnet_reconfig(Backend *be, Conf *conf)
  767. {
  768. Telnet *telnet = container_of(be, Telnet, backend);
  769. pinger_reconfig(telnet->pinger, telnet->conf, conf);
  770. conf_free(telnet->conf);
  771. telnet->conf = conf_copy(conf);
  772. }
  773. /*
  774. * Called to send data down the Telnet connection.
  775. */
  776. static void telnet_send(Backend *be, const char *buf, size_t len)
  777. {
  778. Telnet *telnet = container_of(be, Telnet, backend);
  779. unsigned char *p, *end;
  780. static const unsigned char iac[2] = { IAC, IAC };
  781. static const unsigned char cr[2] = { CR, NUL };
  782. #if 0
  783. static const unsigned char nl[2] = { CR, LF };
  784. #endif
  785. if (telnet->s == NULL)
  786. return;
  787. p = (unsigned char *)buf;
  788. end = (unsigned char *)(buf + len);
  789. while (p < end) {
  790. unsigned char *q = p;
  791. while (p < end && iswritable(*p))
  792. p++;
  793. telnet->bufsize = sk_write(telnet->s, q, p - q);
  794. while (p < end && !iswritable(*p)) {
  795. telnet->bufsize =
  796. sk_write(telnet->s, *p == IAC ? iac : cr, 2);
  797. p++;
  798. }
  799. }
  800. }
  801. /*
  802. * Called to query the current socket sendability status.
  803. */
  804. static size_t telnet_sendbuffer(Backend *be)
  805. {
  806. Telnet *telnet = container_of(be, Telnet, backend);
  807. return telnet->bufsize;
  808. }
  809. /*
  810. * Called to set the size of the window from Telnet's POV.
  811. */
  812. static void telnet_size(Backend *be, int width, int height)
  813. {
  814. Telnet *telnet = container_of(be, Telnet, backend);
  815. unsigned char b[24];
  816. int n;
  817. telnet->term_width = width;
  818. telnet->term_height = height;
  819. if (telnet->s == NULL || telnet->opt_states[o_naws.index] != ACTIVE)
  820. return;
  821. n = 0;
  822. b[n++] = IAC;
  823. b[n++] = SB;
  824. b[n++] = TELOPT_NAWS;
  825. b[n++] = telnet->term_width >> 8;
  826. if (b[n-1] == IAC) b[n++] = IAC; /* duplicate any IAC byte occurs */
  827. b[n++] = telnet->term_width & 0xFF;
  828. if (b[n-1] == IAC) b[n++] = IAC; /* duplicate any IAC byte occurs */
  829. b[n++] = telnet->term_height >> 8;
  830. if (b[n-1] == IAC) b[n++] = IAC; /* duplicate any IAC byte occurs */
  831. b[n++] = telnet->term_height & 0xFF;
  832. if (b[n-1] == IAC) b[n++] = IAC; /* duplicate any IAC byte occurs */
  833. b[n++] = IAC;
  834. b[n++] = SE;
  835. telnet->bufsize = sk_write(telnet->s, b, n);
  836. logeventf(telnet->logctx, "client subnegotiation: SB NAWS %d,%d",
  837. telnet->term_width, telnet->term_height);
  838. }
  839. /*
  840. * Send Telnet special codes.
  841. */
  842. static void telnet_special(Backend *be, SessionSpecialCode code, int arg)
  843. {
  844. Telnet *telnet = container_of(be, Telnet, backend);
  845. unsigned char b[2];
  846. if (telnet->s == NULL)
  847. return;
  848. b[0] = IAC;
  849. switch (code) {
  850. case SS_AYT:
  851. b[1] = AYT;
  852. telnet->bufsize = sk_write(telnet->s, b, 2);
  853. break;
  854. case SS_BRK:
  855. b[1] = BREAK;
  856. telnet->bufsize = sk_write(telnet->s, b, 2);
  857. break;
  858. case SS_EC:
  859. b[1] = EC;
  860. telnet->bufsize = sk_write(telnet->s, b, 2);
  861. break;
  862. case SS_EL:
  863. b[1] = EL;
  864. telnet->bufsize = sk_write(telnet->s, b, 2);
  865. break;
  866. case SS_GA:
  867. b[1] = GA;
  868. telnet->bufsize = sk_write(telnet->s, b, 2);
  869. break;
  870. case SS_NOP:
  871. b[1] = NOP;
  872. telnet->bufsize = sk_write(telnet->s, b, 2);
  873. break;
  874. case SS_ABORT:
  875. b[1] = ABORT;
  876. telnet->bufsize = sk_write(telnet->s, b, 2);
  877. break;
  878. case SS_AO:
  879. b[1] = AO;
  880. telnet->bufsize = sk_write(telnet->s, b, 2);
  881. break;
  882. case SS_IP:
  883. b[1] = IP;
  884. telnet->bufsize = sk_write(telnet->s, b, 2);
  885. break;
  886. case SS_SUSP:
  887. b[1] = SUSP;
  888. telnet->bufsize = sk_write(telnet->s, b, 2);
  889. break;
  890. case SS_EOR:
  891. b[1] = EOR;
  892. telnet->bufsize = sk_write(telnet->s, b, 2);
  893. break;
  894. case SS_EOF:
  895. b[1] = xEOF;
  896. telnet->bufsize = sk_write(telnet->s, b, 2);
  897. break;
  898. case SS_EOL:
  899. /* In BINARY mode, CR-LF becomes just CR -
  900. * and without the NUL suffix too. */
  901. if (telnet->opt_states[o_we_bin.index] == ACTIVE)
  902. telnet->bufsize = sk_write(telnet->s, "\r", 1);
  903. else
  904. telnet->bufsize = sk_write(telnet->s, "\r\n", 2);
  905. break;
  906. case SS_SYNCH:
  907. b[1] = DM;
  908. telnet->bufsize = sk_write(telnet->s, b, 1);
  909. telnet->bufsize = sk_write_oob(telnet->s, b + 1, 1);
  910. break;
  911. case SS_PING:
  912. if (telnet->opt_states[o_they_sga.index] == ACTIVE) {
  913. b[1] = NOP;
  914. telnet->bufsize = sk_write(telnet->s, b, 2);
  915. }
  916. break;
  917. default:
  918. break; /* never heard of it */
  919. }
  920. }
  921. static const SessionSpecial *telnet_get_specials(Backend *be)
  922. {
  923. static const SessionSpecial specials[] = {
  924. {"Are You There", SS_AYT},
  925. {"Break", SS_BRK},
  926. {"Synch", SS_SYNCH},
  927. {"Erase Character", SS_EC},
  928. {"Erase Line", SS_EL},
  929. {"Go Ahead", SS_GA},
  930. {"No Operation", SS_NOP},
  931. {NULL, SS_SEP},
  932. {"Abort Process", SS_ABORT},
  933. {"Abort Output", SS_AO},
  934. {"Interrupt Process", SS_IP},
  935. {"Suspend Process", SS_SUSP},
  936. {NULL, SS_SEP},
  937. {"End Of Record", SS_EOR},
  938. {"End Of File", SS_EOF},
  939. {NULL, SS_EXITMENU}
  940. };
  941. return specials;
  942. }
  943. static bool telnet_connected(Backend *be)
  944. {
  945. Telnet *telnet = container_of(be, Telnet, backend);
  946. return telnet->s != NULL;
  947. }
  948. static bool telnet_sendok(Backend *be)
  949. {
  950. Telnet *telnet = container_of(be, Telnet, backend);
  951. return telnet->socket_connected;
  952. }
  953. static void telnet_unthrottle(Backend *be, size_t backlog)
  954. {
  955. Telnet *telnet = container_of(be, Telnet, backend);
  956. sk_set_frozen(telnet->s, backlog > TELNET_MAX_BACKLOG);
  957. }
  958. static bool telnet_ldisc(Backend *be, int option)
  959. {
  960. Telnet *telnet = container_of(be, Telnet, backend);
  961. if (option == LD_ECHO)
  962. return telnet->echoing;
  963. if (option == LD_EDIT)
  964. return telnet->editing;
  965. return false;
  966. }
  967. static void telnet_provide_ldisc(Backend *be, Ldisc *ldisc)
  968. {
  969. Telnet *telnet = container_of(be, Telnet, backend);
  970. telnet->ldisc = ldisc;
  971. }
  972. static int telnet_exitcode(Backend *be)
  973. {
  974. Telnet *telnet = container_of(be, Telnet, backend);
  975. if (telnet->s != NULL)
  976. return -1; /* still connected */
  977. else if (telnet->closed_on_socket_error)
  978. return INT_MAX; /* a socket error counts as an unclean exit */
  979. else
  980. /* Telnet doesn't transmit exit codes back to the client */
  981. return 0;
  982. }
  983. /*
  984. * cfg_info for Telnet does nothing at all.
  985. */
  986. static int telnet_cfg_info(Backend *be)
  987. {
  988. return 0;
  989. }
  990. const BackendVtable telnet_backend = {
  991. .init = telnet_init,
  992. .free = telnet_free,
  993. .reconfig = telnet_reconfig,
  994. .send = telnet_send,
  995. .sendbuffer = telnet_sendbuffer,
  996. .size = telnet_size,
  997. .special = telnet_special,
  998. .get_specials = telnet_get_specials,
  999. .connected = telnet_connected,
  1000. .exitcode = telnet_exitcode,
  1001. .sendok = telnet_sendok,
  1002. .ldisc_option_state = telnet_ldisc,
  1003. .provide_ldisc = telnet_provide_ldisc,
  1004. .unthrottle = telnet_unthrottle,
  1005. .cfg_info = telnet_cfg_info,
  1006. .id = "telnet",
  1007. .displayname_tc = "Telnet",
  1008. .displayname_lc = "Telnet", /* proper name, so capitalise it anyway */
  1009. .protocol = PROT_TELNET,
  1010. .default_port = 23,
  1011. };