ssh1connection-client.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /*
  2. * Client-specific parts of the SSH-1 connection layer.
  3. */
  4. #include <assert.h>
  5. #include "putty.h"
  6. #include "ssh.h"
  7. #include "sshbpp.h"
  8. #include "sshppl.h"
  9. #include "sshchan.h"
  10. #include "sshcr.h"
  11. #include "ssh1connection.h"
  12. void ssh1_connection_direction_specific_setup(
  13. struct ssh1_connection_state *s)
  14. {
  15. if (!s->mainchan) {
  16. /*
  17. * Start up the main session, by telling mainchan.c to do it
  18. * all just as it would in SSH-2, and translating those
  19. * concepts to SSH-1's non-channel-shaped idea of the main
  20. * session.
  21. */
  22. s->mainchan = mainchan_new(
  23. &s->ppl, &s->cl, s->conf, s->term_width, s->term_height,
  24. false /* is_simple */, NULL);
  25. }
  26. }
  27. typedef void (*sf_handler_fn_t)(struct ssh1_connection_state *s,
  28. bool success, void *ctx);
  29. struct outstanding_succfail {
  30. sf_handler_fn_t handler;
  31. void *ctx;
  32. struct outstanding_succfail *next;
  33. /*
  34. * The 'trivial' flag is set if this handler is in response to a
  35. * request for which the SSH-1 protocol doesn't actually specify a
  36. * response packet. The client of this system (mainchan.c) will
  37. * expect to get an acknowledgment regardless, so we arrange to
  38. * send that ack immediately after the rest of the queue empties.
  39. */
  40. bool trivial;
  41. };
  42. static void ssh1_connection_process_trivial_succfails(void *vs);
  43. static void ssh1_queue_succfail_handler(
  44. struct ssh1_connection_state *s, sf_handler_fn_t handler, void *ctx,
  45. bool trivial)
  46. {
  47. struct outstanding_succfail *osf = snew(struct outstanding_succfail);
  48. osf->handler = handler;
  49. osf->ctx = ctx;
  50. osf->trivial = trivial;
  51. osf->next = NULL;
  52. if (s->succfail_tail)
  53. s->succfail_tail->next = osf;
  54. else
  55. s->succfail_head = osf;
  56. s->succfail_tail = osf;
  57. /* In case this one was trivial and the queue was already empty,
  58. * we should make sure we run the handler promptly, and the
  59. * easiest way is to queue it anyway and then run a trivials pass
  60. * by callback. */
  61. queue_toplevel_callback(ssh1_connection_process_trivial_succfails, s);
  62. }
  63. static void ssh1_connection_process_succfail(
  64. struct ssh1_connection_state *s, bool success)
  65. {
  66. struct outstanding_succfail *prevhead = s->succfail_head;
  67. s->succfail_head = s->succfail_head->next;
  68. if (!s->succfail_head)
  69. s->succfail_tail = NULL;
  70. prevhead->handler(s, success, prevhead->ctx);
  71. sfree(prevhead);
  72. }
  73. static void ssh1_connection_process_trivial_succfails(void *vs)
  74. {
  75. struct ssh1_connection_state *s = (struct ssh1_connection_state *)vs;
  76. while (s->succfail_head && s->succfail_head->trivial)
  77. ssh1_connection_process_succfail(s, true);
  78. }
  79. bool ssh1_handle_direction_specific_packet(
  80. struct ssh1_connection_state *s, PktIn *pktin)
  81. {
  82. PacketProtocolLayer *ppl = &s->ppl; /* for ppl_logevent */
  83. PktOut *pktout;
  84. struct ssh1_channel *c;
  85. unsigned remid;
  86. struct ssh_rportfwd pf, *pfp;
  87. ptrlen host, data;
  88. int port;
  89. switch (pktin->type) {
  90. case SSH1_SMSG_SUCCESS:
  91. case SSH1_SMSG_FAILURE:
  92. if (!s->succfail_head) {
  93. ssh_remote_error(s->ppl.ssh,
  94. "Received %s with no outstanding request",
  95. ssh1_pkt_type(pktin->type));
  96. return true;
  97. }
  98. ssh1_connection_process_succfail(
  99. s, pktin->type == SSH1_SMSG_SUCCESS);
  100. queue_toplevel_callback(
  101. ssh1_connection_process_trivial_succfails, s);
  102. return true;
  103. case SSH1_SMSG_X11_OPEN:
  104. remid = get_uint32(pktin);
  105. /* Refuse if X11 forwarding is disabled. */
  106. if (!s->X11_fwd_enabled) {
  107. pktout = ssh_bpp_new_pktout(
  108. s->ppl.bpp, SSH1_MSG_CHANNEL_OPEN_FAILURE);
  109. put_uint32(pktout, remid);
  110. pq_push(s->ppl.out_pq, pktout);
  111. ppl_logevent("Rejected X11 connect request");
  112. } else {
  113. c = snew(struct ssh1_channel);
  114. c->connlayer = s;
  115. ssh1_channel_init(c);
  116. c->remoteid = remid;
  117. c->chan = x11_new_channel(s->x11authtree, &c->sc,
  118. NULL, -1, false);
  119. c->remoteid = remid;
  120. c->halfopen = false;
  121. pktout = ssh_bpp_new_pktout(
  122. s->ppl.bpp, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION);
  123. put_uint32(pktout, c->remoteid);
  124. put_uint32(pktout, c->localid);
  125. pq_push(s->ppl.out_pq, pktout);
  126. ppl_logevent("Opened X11 forward channel");
  127. }
  128. return true;
  129. case SSH1_SMSG_AGENT_OPEN:
  130. remid = get_uint32(pktin);
  131. /* Refuse if agent forwarding is disabled. */
  132. if (!s->agent_fwd_enabled) {
  133. pktout = ssh_bpp_new_pktout(
  134. s->ppl.bpp, SSH1_MSG_CHANNEL_OPEN_FAILURE);
  135. put_uint32(pktout, remid);
  136. pq_push(s->ppl.out_pq, pktout);
  137. } else {
  138. c = snew(struct ssh1_channel);
  139. c->connlayer = s;
  140. ssh1_channel_init(c);
  141. c->remoteid = remid;
  142. c->chan = agentf_new(&c->sc);
  143. c->halfopen = false;
  144. pktout = ssh_bpp_new_pktout(
  145. s->ppl.bpp, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION);
  146. put_uint32(pktout, c->remoteid);
  147. put_uint32(pktout, c->localid);
  148. pq_push(s->ppl.out_pq, pktout);
  149. }
  150. return true;
  151. case SSH1_MSG_PORT_OPEN:
  152. remid = get_uint32(pktin);
  153. host = get_string(pktin);
  154. port = toint(get_uint32(pktin));
  155. pf.dhost = mkstr(host);
  156. pf.dport = port;
  157. pfp = find234(s->rportfwds, &pf, NULL);
  158. if (!pfp) {
  159. ppl_logevent("Rejected remote port open request for %s:%d",
  160. pf.dhost, port);
  161. pktout = ssh_bpp_new_pktout(
  162. s->ppl.bpp, SSH1_MSG_CHANNEL_OPEN_FAILURE);
  163. put_uint32(pktout, remid);
  164. pq_push(s->ppl.out_pq, pktout);
  165. } else {
  166. char *err;
  167. c = snew(struct ssh1_channel);
  168. c->connlayer = s;
  169. ppl_logevent("Received remote port open request for %s:%d",
  170. pf.dhost, port);
  171. err = portfwdmgr_connect(
  172. s->portfwdmgr, &c->chan, pf.dhost, port,
  173. &c->sc, pfp->addressfamily);
  174. if (err) {
  175. ppl_logevent("Port open failed: %s", err);
  176. sfree(err);
  177. ssh1_channel_free(c);
  178. pktout = ssh_bpp_new_pktout(
  179. s->ppl.bpp, SSH1_MSG_CHANNEL_OPEN_FAILURE);
  180. put_uint32(pktout, remid);
  181. pq_push(s->ppl.out_pq, pktout);
  182. } else {
  183. ssh1_channel_init(c);
  184. c->remoteid = remid;
  185. c->halfopen = false;
  186. pktout = ssh_bpp_new_pktout(
  187. s->ppl.bpp, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION);
  188. put_uint32(pktout, c->remoteid);
  189. put_uint32(pktout, c->localid);
  190. pq_push(s->ppl.out_pq, pktout);
  191. ppl_logevent("Forwarded port opened successfully");
  192. }
  193. }
  194. sfree(pf.dhost);
  195. return true;
  196. case SSH1_SMSG_STDOUT_DATA:
  197. case SSH1_SMSG_STDERR_DATA:
  198. data = get_string(pktin);
  199. if (!get_err(pktin)) {
  200. int bufsize = seat_output(
  201. s->ppl.seat, pktin->type == SSH1_SMSG_STDERR_DATA,
  202. data.ptr, data.len);
  203. if (!s->stdout_throttling && bufsize > SSH1_BUFFER_LIMIT) {
  204. s->stdout_throttling = true;
  205. ssh_throttle_conn(s->ppl.ssh, +1);
  206. }
  207. }
  208. return true;
  209. case SSH1_SMSG_EXIT_STATUS:
  210. {
  211. int exitcode = get_uint32(pktin);
  212. ppl_logevent("Server sent command exit status %d", exitcode);
  213. ssh_got_exitcode(s->ppl.ssh, exitcode);
  214. s->session_terminated = true;
  215. }
  216. return true;
  217. default:
  218. return false;
  219. }
  220. }
  221. static void ssh1mainchan_succfail_wantreply(struct ssh1_connection_state *s,
  222. bool success, void *ctx)
  223. {
  224. chan_request_response(s->mainchan_chan, success);
  225. }
  226. static void ssh1mainchan_succfail_nowantreply(struct ssh1_connection_state *s,
  227. bool success, void *ctx)
  228. {
  229. }
  230. static void ssh1mainchan_queue_response(struct ssh1_connection_state *s,
  231. bool want_reply, bool trivial)
  232. {
  233. sf_handler_fn_t handler = (want_reply ? ssh1mainchan_succfail_wantreply :
  234. ssh1mainchan_succfail_nowantreply);
  235. ssh1_queue_succfail_handler(s, handler, NULL, trivial);
  236. }
  237. static void ssh1mainchan_request_x11_forwarding(
  238. SshChannel *sc, bool want_reply, const char *authproto,
  239. const char *authdata, int screen_number, bool oneshot)
  240. {
  241. struct ssh1_connection_state *s =
  242. container_of(sc, struct ssh1_connection_state, mainchan_sc);
  243. PktOut *pktout;
  244. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_CMSG_X11_REQUEST_FORWARDING);
  245. put_stringz(pktout, authproto);
  246. put_stringz(pktout, authdata);
  247. if (s->local_protoflags & SSH1_PROTOFLAG_SCREEN_NUMBER)
  248. put_uint32(pktout, screen_number);
  249. pq_push(s->ppl.out_pq, pktout);
  250. ssh1mainchan_queue_response(s, want_reply, false);
  251. }
  252. static void ssh1mainchan_request_agent_forwarding(
  253. SshChannel *sc, bool want_reply)
  254. {
  255. struct ssh1_connection_state *s =
  256. container_of(sc, struct ssh1_connection_state, mainchan_sc);
  257. PktOut *pktout;
  258. pktout = ssh_bpp_new_pktout(
  259. s->ppl.bpp, SSH1_CMSG_AGENT_REQUEST_FORWARDING);
  260. pq_push(s->ppl.out_pq, pktout);
  261. ssh1mainchan_queue_response(s, want_reply, false);
  262. }
  263. static void ssh1mainchan_request_pty(
  264. SshChannel *sc, bool want_reply, Conf *conf, int w, int h)
  265. {
  266. struct ssh1_connection_state *s =
  267. container_of(sc, struct ssh1_connection_state, mainchan_sc);
  268. PktOut *pktout;
  269. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_CMSG_REQUEST_PTY);
  270. put_stringz(pktout, conf_get_str(s->conf, CONF_termtype));
  271. put_uint32(pktout, h);
  272. put_uint32(pktout, w);
  273. put_uint32(pktout, 0); /* width in pixels */
  274. put_uint32(pktout, 0); /* height in pixels */
  275. write_ttymodes_to_packet(
  276. BinarySink_UPCAST(pktout), 1,
  277. get_ttymodes_from_conf(s->ppl.seat, conf));
  278. pq_push(s->ppl.out_pq, pktout);
  279. ssh1mainchan_queue_response(s, want_reply, false);
  280. }
  281. static bool ssh1mainchan_send_env_var(
  282. SshChannel *sc, bool want_reply, const char *var, const char *value)
  283. {
  284. return false; /* SSH-1 doesn't support this at all */
  285. }
  286. static void ssh1mainchan_start_shell(SshChannel *sc, bool want_reply)
  287. {
  288. struct ssh1_connection_state *s =
  289. container_of(sc, struct ssh1_connection_state, mainchan_sc);
  290. PktOut *pktout;
  291. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_CMSG_EXEC_SHELL);
  292. pq_push(s->ppl.out_pq, pktout);
  293. ssh1mainchan_queue_response(s, want_reply, true);
  294. }
  295. static void ssh1mainchan_start_command(
  296. SshChannel *sc, bool want_reply, const char *command)
  297. {
  298. struct ssh1_connection_state *s =
  299. container_of(sc, struct ssh1_connection_state, mainchan_sc);
  300. PktOut *pktout;
  301. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_CMSG_EXEC_CMD);
  302. put_stringz(pktout, command);
  303. pq_push(s->ppl.out_pq, pktout);
  304. ssh1mainchan_queue_response(s, want_reply, true);
  305. }
  306. static bool ssh1mainchan_start_subsystem(
  307. SshChannel *sc, bool want_reply, const char *subsystem)
  308. {
  309. return false; /* SSH-1 doesn't support this at all */
  310. }
  311. static bool ssh1mainchan_send_serial_break(
  312. SshChannel *sc, bool want_reply, int length)
  313. {
  314. return false; /* SSH-1 doesn't support this at all */
  315. }
  316. static bool ssh1mainchan_send_signal(
  317. SshChannel *sc, bool want_reply, const char *signame)
  318. {
  319. return false; /* SSH-1 doesn't support this at all */
  320. }
  321. static void ssh1mainchan_send_terminal_size_change(
  322. SshChannel *sc, int w, int h)
  323. {
  324. struct ssh1_connection_state *s =
  325. container_of(sc, struct ssh1_connection_state, mainchan_sc);
  326. PktOut *pktout;
  327. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_CMSG_WINDOW_SIZE);
  328. put_uint32(pktout, h);
  329. put_uint32(pktout, w);
  330. put_uint32(pktout, 0); /* width in pixels */
  331. put_uint32(pktout, 0); /* height in pixels */
  332. pq_push(s->ppl.out_pq, pktout);
  333. }
  334. static void ssh1mainchan_hint_channel_is_simple(SshChannel *sc)
  335. {
  336. }
  337. static int ssh1mainchan_write(
  338. SshChannel *sc, bool is_stderr, const void *data, int len)
  339. {
  340. struct ssh1_connection_state *s =
  341. container_of(sc, struct ssh1_connection_state, mainchan_sc);
  342. PktOut *pktout;
  343. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_CMSG_STDIN_DATA);
  344. put_string(pktout, data, len);
  345. pq_push(s->ppl.out_pq, pktout);
  346. return 0;
  347. }
  348. static void ssh1mainchan_write_eof(SshChannel *sc)
  349. {
  350. struct ssh1_connection_state *s =
  351. container_of(sc, struct ssh1_connection_state, mainchan_sc);
  352. PktOut *pktout;
  353. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_CMSG_EOF);
  354. pq_push(s->ppl.out_pq, pktout);
  355. }
  356. static const struct SshChannelVtable ssh1mainchan_vtable = {
  357. ssh1mainchan_write,
  358. ssh1mainchan_write_eof,
  359. NULL /* unclean_close */,
  360. NULL /* unthrottle */,
  361. NULL /* get_conf */,
  362. NULL /* window_override_removed is only used by SSH-2 sharing */,
  363. NULL /* x11_sharing_handover, likewise */,
  364. NULL /* send_exit_status */,
  365. NULL /* send_exit_signal */,
  366. NULL /* send_exit_signal_numeric */,
  367. ssh1mainchan_request_x11_forwarding,
  368. ssh1mainchan_request_agent_forwarding,
  369. ssh1mainchan_request_pty,
  370. ssh1mainchan_send_env_var,
  371. ssh1mainchan_start_shell,
  372. ssh1mainchan_start_command,
  373. ssh1mainchan_start_subsystem,
  374. ssh1mainchan_send_serial_break,
  375. ssh1mainchan_send_signal,
  376. ssh1mainchan_send_terminal_size_change,
  377. ssh1mainchan_hint_channel_is_simple,
  378. };
  379. static void ssh1_session_confirm_callback(void *vctx)
  380. {
  381. struct ssh1_connection_state *s = (struct ssh1_connection_state *)vctx;
  382. chan_open_confirmation(s->mainchan_chan);
  383. }
  384. SshChannel *ssh1_session_open(ConnectionLayer *cl, Channel *chan)
  385. {
  386. struct ssh1_connection_state *s =
  387. container_of(cl, struct ssh1_connection_state, cl);
  388. s->mainchan_sc.vt = &ssh1mainchan_vtable;
  389. s->mainchan_sc.cl = &s->cl;
  390. s->mainchan_chan = chan;
  391. queue_toplevel_callback(ssh1_session_confirm_callback, s);
  392. return &s->mainchan_sc;
  393. }
  394. static void ssh1_rportfwd_response(struct ssh1_connection_state *s,
  395. bool success, void *ctx)
  396. {
  397. PacketProtocolLayer *ppl = &s->ppl; /* for ppl_logevent */
  398. struct ssh_rportfwd *rpf = (struct ssh_rportfwd *)ctx;
  399. if (success) {
  400. ppl_logevent("Remote port forwarding from %s enabled",
  401. rpf->log_description);
  402. } else {
  403. ppl_logevent("Remote port forwarding from %s refused",
  404. rpf->log_description);
  405. struct ssh_rportfwd *realpf = del234(s->rportfwds, rpf);
  406. assert(realpf == rpf);
  407. portfwdmgr_close(s->portfwdmgr, rpf->pfr);
  408. free_rportfwd(rpf);
  409. }
  410. }
  411. struct ssh_rportfwd *ssh1_rportfwd_alloc(
  412. ConnectionLayer *cl,
  413. const char *shost, int sport, const char *dhost, int dport,
  414. int addressfamily, const char *log_description, PortFwdRecord *pfr,
  415. ssh_sharing_connstate *share_ctx)
  416. {
  417. struct ssh1_connection_state *s =
  418. container_of(cl, struct ssh1_connection_state, cl);
  419. struct ssh_rportfwd *rpf = snew(struct ssh_rportfwd);
  420. rpf->shost = dupstr(shost);
  421. rpf->sport = sport;
  422. rpf->dhost = dupstr(dhost);
  423. rpf->dport = dport;
  424. rpf->addressfamily = addressfamily;
  425. rpf->log_description = dupstr(log_description);
  426. rpf->pfr = pfr;
  427. if (add234(s->rportfwds, rpf) != rpf) {
  428. free_rportfwd(rpf);
  429. return NULL;
  430. }
  431. PktOut *pktout = ssh_bpp_new_pktout(
  432. s->ppl.bpp, SSH1_CMSG_PORT_FORWARD_REQUEST);
  433. put_uint32(pktout, rpf->sport);
  434. put_stringz(pktout, rpf->dhost);
  435. put_uint32(pktout, rpf->dport);
  436. pq_push(s->ppl.out_pq, pktout);
  437. ssh1_queue_succfail_handler(s, ssh1_rportfwd_response, rpf, false);
  438. return rpf;
  439. }
  440. SshChannel *ssh1_serverside_x11_open(
  441. ConnectionLayer *cl, Channel *chan, const SocketPeerInfo *pi)
  442. {
  443. assert(false && "Should never be called in the client");
  444. return NULL;
  445. }
  446. SshChannel *ssh1_serverside_agent_open(ConnectionLayer *cl, Channel *chan)
  447. {
  448. assert(false && "Should never be called in the client");
  449. return NULL;
  450. }