rlogin.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. * Rlogin backend.
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <limits.h>
  7. #include <ctype.h>
  8. #include "putty.h"
  9. #define RLOGIN_MAX_BACKLOG 4096
  10. typedef struct Rlogin Rlogin;
  11. struct Rlogin {
  12. Socket *s;
  13. bool closed_on_socket_error;
  14. int bufsize;
  15. bool socket_connected;
  16. bool firstbyte;
  17. bool cansize;
  18. int term_width, term_height;
  19. Seat *seat;
  20. LogContext *logctx;
  21. Ldisc *ldisc;
  22. char *description;
  23. Conf *conf;
  24. /* In case we need to read a username from the terminal before starting */
  25. prompts_t *prompt;
  26. Plug plug;
  27. Backend backend;
  28. Interactor interactor;
  29. };
  30. static void rlogin_startup(Rlogin *rlogin, SeatPromptResult spr,
  31. const char *ruser);
  32. static void rlogin_try_username_prompt(void *ctx);
  33. static void c_write(Rlogin *rlogin, const void *buf, size_t len)
  34. {
  35. size_t backlog = seat_stdout(rlogin->seat, buf, len);
  36. sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
  37. }
  38. static void rlogin_log(Plug *plug, Socket *s, PlugLogType type, SockAddr *addr,
  39. int port, const char *error_msg, int error_code)
  40. {
  41. Rlogin *rlogin = container_of(plug, Rlogin, plug);
  42. backend_socket_log(rlogin->seat, rlogin->logctx, s, type, addr, port,
  43. error_msg, error_code,
  44. rlogin->conf, rlogin->socket_connected);
  45. if (type == PLUGLOG_CONNECT_SUCCESS) {
  46. rlogin->socket_connected = true;
  47. char *ruser = get_remote_username(rlogin->conf);
  48. if (ruser) {
  49. /*
  50. * If we already know the remote username, call
  51. * rlogin_startup, which will send the initial protocol
  52. * greeting including local username, remote username,
  53. * terminal type and terminal speed.
  54. */
  55. /* Next terminal output will come from server */
  56. seat_set_trust_status(rlogin->seat, false);
  57. rlogin_startup(rlogin, SPR_OK, ruser);
  58. sfree(ruser);
  59. } else {
  60. /*
  61. * Otherwise, set up a prompts_t asking for the local
  62. * username. If it completes synchronously, call
  63. * rlogin_startup as above; otherwise, wait until it does.
  64. */
  65. rlogin->prompt = new_prompts();
  66. rlogin->prompt->to_server = true;
  67. rlogin->prompt->from_server = false;
  68. rlogin->prompt->name = dupstr("Rlogin login name");
  69. rlogin->prompt->callback = rlogin_try_username_prompt;
  70. rlogin->prompt->callback_ctx = rlogin;
  71. add_prompt(rlogin->prompt, dupstr("rlogin username: "), true);
  72. rlogin_try_username_prompt(rlogin);
  73. }
  74. }
  75. }
  76. static void rlogin_closing(Plug *plug, PlugCloseType type,
  77. const char *error_msg)
  78. {
  79. Rlogin *rlogin = container_of(plug, Rlogin, plug);
  80. /*
  81. * We don't implement independent EOF in each direction for Telnet
  82. * connections; as soon as we get word that the remote side has
  83. * sent us EOF, we wind up the whole connection.
  84. */
  85. if (rlogin->s) {
  86. sk_close(rlogin->s);
  87. rlogin->s = NULL;
  88. if (error_msg)
  89. rlogin->closed_on_socket_error = true;
  90. seat_notify_remote_exit(rlogin->seat);
  91. seat_notify_remote_disconnect(rlogin->seat);
  92. }
  93. if (type != PLUGCLOSE_NORMAL) {
  94. /* A socket error has occurred. */
  95. logevent(rlogin->logctx, error_msg);
  96. if (type != PLUGCLOSE_USER_ABORT)
  97. seat_connection_fatal(rlogin->seat, "%s", error_msg);
  98. }
  99. /* Otherwise, the remote side closed the connection normally. */
  100. }
  101. static void rlogin_receive(
  102. Plug *plug, int urgent, const char *data, size_t len)
  103. {
  104. Rlogin *rlogin = container_of(plug, Rlogin, plug);
  105. if (len == 0)
  106. return;
  107. if (urgent == 2) {
  108. char c;
  109. c = *data++;
  110. len--;
  111. if (c == '\x80') {
  112. rlogin->cansize = true;
  113. backend_size(&rlogin->backend,
  114. rlogin->term_width, rlogin->term_height);
  115. }
  116. /*
  117. * We should flush everything (aka Telnet SYNCH) if we see
  118. * 0x02, and we should turn off and on _local_ flow control
  119. * on 0x10 and 0x20 respectively. I'm not convinced it's
  120. * worth it...
  121. */
  122. } else {
  123. /*
  124. * Main rlogin protocol. This is really simple: the first
  125. * byte is expected to be NULL and is ignored, and the rest
  126. * is printed.
  127. */
  128. if (rlogin->firstbyte) {
  129. if (data[0] == '\0') {
  130. data++;
  131. len--;
  132. }
  133. rlogin->firstbyte = false;
  134. }
  135. if (len > 0)
  136. c_write(rlogin, data, len);
  137. }
  138. }
  139. static void rlogin_sent(Plug *plug, size_t bufsize)
  140. {
  141. Rlogin *rlogin = container_of(plug, Rlogin, plug);
  142. rlogin->bufsize = bufsize;
  143. seat_sent(rlogin->seat, rlogin->bufsize);
  144. }
  145. static void rlogin_startup(Rlogin *rlogin, SeatPromptResult spr,
  146. const char *ruser)
  147. {
  148. char z = 0;
  149. char *p;
  150. if (spr.kind == SPRK_USER_ABORT) {
  151. /* User aborted at the username prompt. */
  152. sk_close(rlogin->s);
  153. rlogin->s = NULL;
  154. seat_notify_remote_exit(rlogin->seat);
  155. } else if (spr.kind == SPRK_SW_ABORT) {
  156. /* Something else went wrong at the username prompt, so we
  157. * have to show some kind of error. */
  158. sk_close(rlogin->s);
  159. rlogin->s = NULL;
  160. char *err = spr_get_error_message(spr);
  161. seat_connection_fatal(rlogin->seat, "%s", err);
  162. sfree(err);
  163. } else {
  164. sk_write(rlogin->s, &z, 1);
  165. p = conf_get_str(rlogin->conf, CONF_localusername);
  166. sk_write(rlogin->s, p, strlen(p));
  167. sk_write(rlogin->s, &z, 1);
  168. sk_write(rlogin->s, ruser, strlen(ruser));
  169. sk_write(rlogin->s, &z, 1);
  170. p = conf_get_str(rlogin->conf, CONF_termtype);
  171. sk_write(rlogin->s, p, strlen(p));
  172. sk_write(rlogin->s, "/", 1);
  173. p = conf_get_str(rlogin->conf, CONF_termspeed);
  174. sk_write(rlogin->s, p, strspn(p, "0123456789"));
  175. rlogin->bufsize = sk_write(rlogin->s, &z, 1);
  176. }
  177. rlogin->prompt = NULL;
  178. if (rlogin->ldisc)
  179. ldisc_check_sendok(rlogin->ldisc);
  180. }
  181. static const PlugVtable Rlogin_plugvt = {
  182. .log = rlogin_log,
  183. .closing = rlogin_closing,
  184. .receive = rlogin_receive,
  185. .sent = rlogin_sent,
  186. };
  187. static char *rlogin_description(Interactor *itr)
  188. {
  189. Rlogin *rlogin = container_of(itr, Rlogin, interactor);
  190. return dupstr(rlogin->description);
  191. }
  192. static LogPolicy *rlogin_logpolicy(Interactor *itr)
  193. {
  194. Rlogin *rlogin = container_of(itr, Rlogin, interactor);
  195. return log_get_policy(rlogin->logctx);
  196. }
  197. static Seat *rlogin_get_seat(Interactor *itr)
  198. {
  199. Rlogin *rlogin = container_of(itr, Rlogin, interactor);
  200. return rlogin->seat;
  201. }
  202. static void rlogin_set_seat(Interactor *itr, Seat *seat)
  203. {
  204. Rlogin *rlogin = container_of(itr, Rlogin, interactor);
  205. rlogin->seat = seat;
  206. }
  207. static const InteractorVtable Rlogin_interactorvt = {
  208. .description = rlogin_description,
  209. .logpolicy = rlogin_logpolicy,
  210. .get_seat = rlogin_get_seat,
  211. .set_seat = rlogin_set_seat,
  212. };
  213. /*
  214. * Called to set up the rlogin connection.
  215. *
  216. * Returns an error message, or NULL on success.
  217. *
  218. * Also places the canonical host name into `realhost'. It must be
  219. * freed by the caller.
  220. */
  221. static char *rlogin_init(const BackendVtable *vt, Seat *seat,
  222. Backend **backend_handle, LogContext *logctx,
  223. Conf *conf, const char *host, int port,
  224. char **realhost, bool nodelay, bool keepalive)
  225. {
  226. SockAddr *addr;
  227. const char *err;
  228. Rlogin *rlogin;
  229. int addressfamily;
  230. char *loghost;
  231. rlogin = snew(Rlogin);
  232. memset(rlogin, 0, sizeof(Rlogin));
  233. rlogin->plug.vt = &Rlogin_plugvt;
  234. rlogin->backend.vt = vt;
  235. rlogin->interactor.vt = &Rlogin_interactorvt;
  236. rlogin->backend.interactor = &rlogin->interactor;
  237. rlogin->s = NULL;
  238. rlogin->closed_on_socket_error = false;
  239. rlogin->seat = seat;
  240. rlogin->logctx = logctx;
  241. rlogin->term_width = conf_get_int(conf, CONF_width);
  242. rlogin->term_height = conf_get_int(conf, CONF_height);
  243. rlogin->socket_connected = false;
  244. rlogin->firstbyte = true;
  245. rlogin->cansize = false;
  246. rlogin->prompt = NULL;
  247. rlogin->conf = conf_copy(conf);
  248. rlogin->description = default_description(vt, host, port);
  249. *backend_handle = &rlogin->backend;
  250. addressfamily = conf_get_int(conf, CONF_addressfamily);
  251. /*
  252. * Try to find host.
  253. */
  254. addr = name_lookup(host, port, realhost, conf, addressfamily,
  255. rlogin->logctx, "rlogin connection");
  256. if ((err = sk_addr_error(addr)) != NULL) {
  257. sk_addr_free(addr);
  258. return dupstr(err);
  259. }
  260. if (port < 0)
  261. port = 513; /* default rlogin port */
  262. /*
  263. * Open socket.
  264. */
  265. rlogin->s = new_connection(addr, *realhost, port, true, false,
  266. nodelay, keepalive, &rlogin->plug, conf,
  267. &rlogin->interactor);
  268. if ((err = sk_socket_error(rlogin->s)) != NULL)
  269. return dupstr(err);
  270. loghost = conf_get_str(conf, CONF_loghost);
  271. if (*loghost) {
  272. char *colon;
  273. sfree(*realhost);
  274. *realhost = dupstr(loghost);
  275. colon = host_strrchr(*realhost, ':');
  276. if (colon)
  277. *colon++ = '\0';
  278. }
  279. return NULL;
  280. }
  281. static void rlogin_free(Backend *be)
  282. {
  283. Rlogin *rlogin = container_of(be, Rlogin, backend);
  284. if (is_tempseat(rlogin->seat))
  285. tempseat_free(rlogin->seat);
  286. if (rlogin->prompt)
  287. free_prompts(rlogin->prompt);
  288. if (rlogin->s)
  289. sk_close(rlogin->s);
  290. conf_free(rlogin->conf);
  291. sfree(rlogin->description);
  292. sfree(rlogin);
  293. }
  294. /*
  295. * Stub routine (we don't have any need to reconfigure this backend).
  296. */
  297. static void rlogin_reconfig(Backend *be, Conf *conf)
  298. {
  299. }
  300. static void rlogin_try_username_prompt(void *ctx)
  301. {
  302. Rlogin *rlogin = (Rlogin *)ctx;
  303. SeatPromptResult spr = seat_get_userpass_input(
  304. interactor_announce(&rlogin->interactor), rlogin->prompt);
  305. if (spr.kind == SPRK_INCOMPLETE)
  306. return;
  307. /* Next terminal output will come from server */
  308. seat_set_trust_status(rlogin->seat, false);
  309. /* Send the rlogin setup protocol data, and then we're ready to
  310. * start receiving normal input to send down the wire, which
  311. * rlogin_startup will signal to rlogin_sendok by nulling out
  312. * rlogin->prompt. */
  313. rlogin_startup(
  314. rlogin, spr, prompt_get_result_ref(rlogin->prompt->prompts[0]));
  315. }
  316. /*
  317. * Called to send data down the rlogin connection.
  318. */
  319. static void rlogin_send(Backend *be, const char *buf, size_t len)
  320. {
  321. Rlogin *rlogin = container_of(be, Rlogin, backend);
  322. if (rlogin->s == NULL)
  323. return;
  324. rlogin->bufsize = sk_write(rlogin->s, buf, len);
  325. }
  326. /*
  327. * Called to query the current socket sendability status.
  328. */
  329. static size_t rlogin_sendbuffer(Backend *be)
  330. {
  331. Rlogin *rlogin = container_of(be, Rlogin, backend);
  332. return rlogin->bufsize;
  333. }
  334. /*
  335. * Called to set the size of the window
  336. */
  337. static void rlogin_size(Backend *be, int width, int height)
  338. {
  339. Rlogin *rlogin = container_of(be, Rlogin, backend);
  340. char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
  341. rlogin->term_width = width;
  342. rlogin->term_height = height;
  343. if (rlogin->s == NULL || !rlogin->cansize)
  344. return;
  345. b[6] = rlogin->term_width >> 8;
  346. b[7] = rlogin->term_width & 0xFF;
  347. b[4] = rlogin->term_height >> 8;
  348. b[5] = rlogin->term_height & 0xFF;
  349. rlogin->bufsize = sk_write(rlogin->s, b, 12);
  350. return;
  351. }
  352. /*
  353. * Send rlogin special codes.
  354. */
  355. static void rlogin_special(Backend *be, SessionSpecialCode code, int arg)
  356. {
  357. /* Do nothing! */
  358. return;
  359. }
  360. /*
  361. * Return a list of the special codes that make sense in this
  362. * protocol.
  363. */
  364. static const SessionSpecial *rlogin_get_specials(Backend *be)
  365. {
  366. return NULL;
  367. }
  368. static bool rlogin_connected(Backend *be)
  369. {
  370. Rlogin *rlogin = container_of(be, Rlogin, backend);
  371. return rlogin->s != NULL;
  372. }
  373. static bool rlogin_sendok(Backend *be)
  374. {
  375. /*
  376. * We only want to receive input data if the socket is connected
  377. * and we're not still at the username prompt stage.
  378. */
  379. Rlogin *rlogin = container_of(be, Rlogin, backend);
  380. return rlogin->socket_connected && !rlogin->prompt;
  381. }
  382. static void rlogin_unthrottle(Backend *be, size_t backlog)
  383. {
  384. Rlogin *rlogin = container_of(be, Rlogin, backend);
  385. sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
  386. }
  387. static bool rlogin_ldisc(Backend *be, int option)
  388. {
  389. /* Rlogin *rlogin = container_of(be, Rlogin, backend); */
  390. return false;
  391. }
  392. static void rlogin_provide_ldisc(Backend *be, Ldisc *ldisc)
  393. {
  394. Rlogin *rlogin = container_of(be, Rlogin, backend);
  395. rlogin->ldisc = ldisc;
  396. }
  397. static int rlogin_exitcode(Backend *be)
  398. {
  399. Rlogin *rlogin = container_of(be, Rlogin, backend);
  400. if (rlogin->s != NULL)
  401. return -1; /* still connected */
  402. else if (rlogin->closed_on_socket_error)
  403. return INT_MAX; /* a socket error counts as an unclean exit */
  404. else
  405. /* If we ever implement RSH, we'll probably need to do this properly */
  406. return 0;
  407. }
  408. /*
  409. * cfg_info for rlogin does nothing at all.
  410. */
  411. static int rlogin_cfg_info(Backend *be)
  412. {
  413. return 0;
  414. }
  415. const BackendVtable rlogin_backend = {
  416. .init = rlogin_init,
  417. .free = rlogin_free,
  418. .reconfig = rlogin_reconfig,
  419. .send = rlogin_send,
  420. .sendbuffer = rlogin_sendbuffer,
  421. .size = rlogin_size,
  422. .special = rlogin_special,
  423. .get_specials = rlogin_get_specials,
  424. .connected = rlogin_connected,
  425. .exitcode = rlogin_exitcode,
  426. .sendok = rlogin_sendok,
  427. .ldisc_option_state = rlogin_ldisc,
  428. .provide_ldisc = rlogin_provide_ldisc,
  429. .unthrottle = rlogin_unthrottle,
  430. .cfg_info = rlogin_cfg_info,
  431. .id = "rlogin",
  432. .displayname_tc = "Rlogin",
  433. .displayname_lc = "Rlogin", /* proper name, so capitalise it anyway */
  434. .protocol = PROT_RLOGIN,
  435. .default_port = 513,
  436. };