connection2-client.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /*
  2. * Client-specific parts of the SSH-2 connection layer.
  3. */
  4. #include <assert.h>
  5. #include "putty.h"
  6. #include "ssh.h"
  7. #include "bpp.h"
  8. #include "ppl.h"
  9. #include "channel.h"
  10. #include "sshcr.h"
  11. #include "connection2.h"
  12. static ChanopenResult chan_open_x11(
  13. struct ssh2_connection_state *s, SshChannel *sc,
  14. ptrlen peeraddr, int peerport)
  15. {
  16. #ifndef WINSCP
  17. PacketProtocolLayer *ppl = &s->ppl; /* for ppl_logevent */
  18. char *peeraddr_str;
  19. Channel *ch;
  20. ppl_logevent("Received X11 connect request from %.*s:%d",
  21. PTRLEN_PRINTF(peeraddr), peerport);
  22. if (!s->X11_fwd_enabled && !s->connshare) {
  23. CHANOPEN_RETURN_FAILURE(
  24. SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED,
  25. ("X11 forwarding is not enabled"));
  26. }
  27. peeraddr_str = peeraddr.ptr ? mkstr(peeraddr) : NULL;
  28. ch = x11_new_channel(
  29. s->x11authtree, sc, peeraddr_str, peerport, s->connshare != NULL);
  30. sfree(peeraddr_str);
  31. ppl_logevent("Opened X11 forward channel");
  32. CHANOPEN_RETURN_SUCCESS(ch);
  33. #else
  34. assert(false);
  35. CHANOPEN_RETURN_FAILURE(SSH2_OPEN_UNKNOWN_CHANNEL_TYPE, ("Unsupported"));
  36. #endif
  37. }
  38. static ChanopenResult chan_open_forwarded_tcpip(
  39. struct ssh2_connection_state *s, SshChannel *sc,
  40. ptrlen fwdaddr, int fwdport, ptrlen peeraddr, int peerport)
  41. {
  42. PacketProtocolLayer *ppl = &s->ppl; /* for ppl_logevent */
  43. struct ssh_rportfwd pf, *realpf;
  44. Channel *ch;
  45. char *err;
  46. ppl_logevent("Received remote port %.*s:%d open request from %.*s:%d",
  47. PTRLEN_PRINTF(fwdaddr), fwdport,
  48. PTRLEN_PRINTF(peeraddr), peerport);
  49. pf.shost = mkstr(fwdaddr);
  50. pf.sport = fwdport;
  51. realpf = find234(s->rportfwds, &pf, NULL);
  52. sfree(pf.shost);
  53. if (realpf == NULL) {
  54. CHANOPEN_RETURN_FAILURE(
  55. SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED,
  56. ("Remote port is not recognised"));
  57. }
  58. if (realpf->share_ctx) {
  59. /*
  60. * This port forwarding is on behalf of a connection-sharing
  61. * downstream.
  62. */
  63. CHANOPEN_RETURN_DOWNSTREAM(realpf->share_ctx);
  64. }
  65. err = portfwdmgr_connect(
  66. s->portfwdmgr, &ch, realpf->dhost, realpf->dport,
  67. sc, realpf->addressfamily);
  68. ppl_logevent("Attempting to forward remote port to %s:%d",
  69. realpf->dhost, realpf->dport);
  70. if (err != NULL) {
  71. ppl_logevent("Port open failed: %s", err);
  72. sfree(err);
  73. CHANOPEN_RETURN_FAILURE(
  74. SSH2_OPEN_CONNECT_FAILED,
  75. ("Port open failed"));
  76. }
  77. ppl_logevent("Forwarded port opened successfully");
  78. CHANOPEN_RETURN_SUCCESS(ch);
  79. }
  80. static ChanopenResult chan_open_auth_agent(
  81. struct ssh2_connection_state *s, SshChannel *sc)
  82. {
  83. if (!ssh_agent_forwarding_permitted(&s->cl)) {
  84. CHANOPEN_RETURN_FAILURE(
  85. SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED,
  86. ("Agent forwarding is not enabled"));
  87. }
  88. /*
  89. * If possible, make a stream-oriented connection to the agent and
  90. * set up an ordinary port-forwarding type channel over it.
  91. */
  92. { // WINSCP
  93. Plug *plug;
  94. Channel *ch = portfwd_raw_new(&s->cl, &plug, true);
  95. Socket *skt = agent_connect(plug);
  96. if (!sk_socket_error(skt)) {
  97. portfwd_raw_setup(ch, skt, sc);
  98. CHANOPEN_RETURN_SUCCESS(ch);
  99. } else {
  100. portfwd_raw_free(ch);
  101. /*
  102. * Otherwise, fall back to the old-fashioned system of parsing the
  103. * forwarded data stream ourselves for message boundaries, and
  104. * passing each individual message to the one-off agent_query().
  105. */
  106. CHANOPEN_RETURN_SUCCESS(agentf_new(sc, plug));
  107. }
  108. } // WINSCP
  109. }
  110. ChanopenResult ssh2_connection_parse_channel_open(
  111. struct ssh2_connection_state *s, ptrlen type,
  112. PktIn *pktin, SshChannel *sc)
  113. {
  114. if (ptrlen_eq_string(type, "x11")) {
  115. ptrlen peeraddr = get_string(pktin);
  116. int peerport = get_uint32(pktin);
  117. return chan_open_x11(s, sc, peeraddr, peerport);
  118. } else if (ptrlen_eq_string(type, "forwarded-tcpip")) {
  119. ptrlen fwdaddr = get_string(pktin);
  120. int fwdport = toint(get_uint32(pktin));
  121. ptrlen peeraddr = get_string(pktin);
  122. int peerport = toint(get_uint32(pktin));
  123. return chan_open_forwarded_tcpip(
  124. s, sc, fwdaddr, fwdport, peeraddr, peerport);
  125. } else if (ptrlen_eq_string(type, "[email protected]")) {
  126. return chan_open_auth_agent(s, sc);
  127. } else {
  128. CHANOPEN_RETURN_FAILURE(
  129. SSH2_OPEN_UNKNOWN_CHANNEL_TYPE,
  130. ("Unsupported channel type requested"));
  131. }
  132. }
  133. bool ssh2_connection_parse_global_request(
  134. struct ssh2_connection_state *s, ptrlen type, PktIn *pktin)
  135. {
  136. /*
  137. * We don't know of any global requests that an SSH client needs
  138. * to honour.
  139. */
  140. return false;
  141. }
  142. PktOut *ssh2_portfwd_chanopen(
  143. struct ssh2_connection_state *s, struct ssh2_channel *c,
  144. const char *hostname, int port,
  145. const char *description, const SocketPeerInfo *peerinfo)
  146. {
  147. PacketProtocolLayer *ppl = &s->ppl; /* for ppl_logevent */
  148. PktOut *pktout;
  149. /*
  150. * In client mode, this function is called by portfwdmgr in
  151. * response to PortListeners that were set up in
  152. * portfwdmgr_config, which means that the hostname and port
  153. * parameters will indicate the host we want to tell the server to
  154. * connect _to_.
  155. */
  156. ppl_logevent("Opening connection to %s:%d for %s",
  157. hostname, port, description);
  158. pktout = ssh2_chanopen_init(c, "direct-tcpip");
  159. {
  160. char *trimmed_host = host_strduptrim(hostname);
  161. put_stringz(pktout, trimmed_host);
  162. sfree(trimmed_host);
  163. }
  164. put_uint32(pktout, port);
  165. /*
  166. * We make up values for the originator data; partly it's too much
  167. * hassle to keep track, and partly I'm not convinced the server
  168. * should be told details like that about my local network
  169. * configuration. The "originator IP address" is syntactically a
  170. * numeric IP address, and some servers (e.g., Tectia) get upset
  171. * if it doesn't match this syntax.
  172. */
  173. put_stringz(pktout, "0.0.0.0");
  174. put_uint32(pktout, 0);
  175. return pktout;
  176. }
  177. static int ssh2_rportfwd_cmp(void *av, void *bv)
  178. {
  179. struct ssh_rportfwd *a = (struct ssh_rportfwd *) av;
  180. struct ssh_rportfwd *b = (struct ssh_rportfwd *) bv;
  181. int i;
  182. if ( (i = strcmp(a->shost, b->shost)) != 0)
  183. return i < 0 ? -1 : +1;
  184. if (a->sport > b->sport)
  185. return +1;
  186. if (a->sport < b->sport)
  187. return -1;
  188. return 0;
  189. }
  190. static void ssh2_rportfwd_globreq_response(struct ssh2_connection_state *s,
  191. PktIn *pktin, void *ctx)
  192. {
  193. PacketProtocolLayer *ppl = &s->ppl; /* for ppl_logevent */
  194. struct ssh_rportfwd *rpf = (struct ssh_rportfwd *)ctx;
  195. if (pktin->type == SSH2_MSG_REQUEST_SUCCESS) {
  196. ppl_logevent("Remote port forwarding from %s enabled",
  197. rpf->log_description);
  198. } else {
  199. ppl_logevent("Remote port forwarding from %s refused",
  200. rpf->log_description);
  201. { // WINSCP
  202. struct ssh_rportfwd *realpf = del234(s->rportfwds, rpf);
  203. assert(realpf == rpf);
  204. portfwdmgr_close(s->portfwdmgr, rpf->pfr);
  205. free_rportfwd(rpf);
  206. } // WINSCP
  207. }
  208. }
  209. struct ssh_rportfwd *ssh2_rportfwd_alloc(
  210. ConnectionLayer *cl,
  211. const char *shost, int sport, const char *dhost, int dport,
  212. int addressfamily, const char *log_description, PortFwdRecord *pfr,
  213. ssh_sharing_connstate *share_ctx)
  214. {
  215. struct ssh2_connection_state *s =
  216. container_of(cl, struct ssh2_connection_state, cl);
  217. struct ssh_rportfwd *rpf = snew(struct ssh_rportfwd);
  218. if (!s->rportfwds)
  219. s->rportfwds = newtree234(ssh2_rportfwd_cmp);
  220. rpf->shost = dupstr(shost);
  221. rpf->sport = sport;
  222. rpf->dhost = dupstr(dhost);
  223. rpf->dport = dport;
  224. rpf->addressfamily = addressfamily;
  225. rpf->log_description = dupstr(log_description);
  226. rpf->pfr = pfr;
  227. rpf->share_ctx = share_ctx;
  228. if (add234(s->rportfwds, rpf) != rpf) {
  229. free_rportfwd(rpf);
  230. return NULL;
  231. }
  232. if (!rpf->share_ctx) {
  233. PktOut *pktout = ssh_bpp_new_pktout(
  234. s->ppl.bpp, SSH2_MSG_GLOBAL_REQUEST);
  235. put_stringz(pktout, "tcpip-forward");
  236. put_bool(pktout, true); /* want reply */
  237. put_stringz(pktout, rpf->shost);
  238. put_uint32(pktout, rpf->sport);
  239. pq_push(s->ppl.out_pq, pktout);
  240. ssh2_queue_global_request_handler(
  241. s, ssh2_rportfwd_globreq_response, rpf);
  242. }
  243. return rpf;
  244. }
  245. void ssh2_rportfwd_remove(ConnectionLayer *cl, struct ssh_rportfwd *rpf)
  246. {
  247. struct ssh2_connection_state *s =
  248. container_of(cl, struct ssh2_connection_state, cl);
  249. if (rpf->share_ctx) {
  250. /*
  251. * We don't manufacture a cancel-tcpip-forward message for
  252. * remote port forwardings being removed on behalf of a
  253. * downstream; we just pass through the one the downstream
  254. * sent to us.
  255. */
  256. } else {
  257. PktOut *pktout = ssh_bpp_new_pktout(
  258. s->ppl.bpp, SSH2_MSG_GLOBAL_REQUEST);
  259. put_stringz(pktout, "cancel-tcpip-forward");
  260. put_bool(pktout, false); /* _don't_ want reply */
  261. put_stringz(pktout, rpf->shost);
  262. put_uint32(pktout, rpf->sport);
  263. pq_push(s->ppl.out_pq, pktout);
  264. }
  265. assert(s->rportfwds);
  266. { // WINSCP
  267. struct ssh_rportfwd *realpf = del234(s->rportfwds, rpf);
  268. assert(realpf == rpf);
  269. } // WINSCP
  270. free_rportfwd(rpf);
  271. }
  272. SshChannel *ssh2_session_open(ConnectionLayer *cl, Channel *chan)
  273. {
  274. struct ssh2_connection_state *s =
  275. container_of(cl, struct ssh2_connection_state, cl);
  276. PacketProtocolLayer *ppl = &s->ppl; /* for ppl_logevent */
  277. struct ssh2_channel *c = snew(struct ssh2_channel);
  278. PktOut *pktout;
  279. c->connlayer = s;
  280. ssh2_channel_init(c);
  281. c->halfopen = true;
  282. c->chan = chan;
  283. ppl_logevent("Opening main session channel");
  284. pktout = ssh2_chanopen_init(c, "session");
  285. pq_push(s->ppl.out_pq, pktout);
  286. return &c->sc;
  287. }
  288. SshChannel *ssh2_serverside_x11_open(
  289. ConnectionLayer *cl, Channel *chan, const SocketPeerInfo *pi)
  290. {
  291. unreachable("Should never be called in the client");
  292. }
  293. SshChannel *ssh2_serverside_agent_open(ConnectionLayer *cl, Channel *chan)
  294. {
  295. unreachable("Should never be called in the client");
  296. }
  297. static void ssh2_channel_response(
  298. struct ssh2_channel *c, PktIn *pkt, void *ctx)
  299. {
  300. /* If pkt==NULL (because this handler has been called in response
  301. * to CHANNEL_CLOSE arriving while the request was still
  302. * outstanding), we treat that the same as CHANNEL_FAILURE. */
  303. chan_request_response(c->chan,
  304. pkt && pkt->type == SSH2_MSG_CHANNEL_SUCCESS);
  305. }
  306. void ssh2channel_start_shell(SshChannel *sc, bool want_reply)
  307. {
  308. struct ssh2_channel *c = container_of(sc, struct ssh2_channel, sc);
  309. struct ssh2_connection_state *s = c->connlayer;
  310. PktOut *pktout = ssh2_chanreq_init(
  311. c, "shell", want_reply ? ssh2_channel_response : NULL, NULL);
  312. pq_push(s->ppl.out_pq, pktout);
  313. }
  314. void ssh2channel_start_command(
  315. SshChannel *sc, bool want_reply, const char *command)
  316. {
  317. struct ssh2_channel *c = container_of(sc, struct ssh2_channel, sc);
  318. struct ssh2_connection_state *s = c->connlayer;
  319. PktOut *pktout = ssh2_chanreq_init(
  320. c, "exec", want_reply ? ssh2_channel_response : NULL, NULL);
  321. put_stringz(pktout, command);
  322. pq_push(s->ppl.out_pq, pktout);
  323. }
  324. bool ssh2channel_start_subsystem(
  325. SshChannel *sc, bool want_reply, const char *subsystem)
  326. {
  327. struct ssh2_channel *c = container_of(sc, struct ssh2_channel, sc);
  328. struct ssh2_connection_state *s = c->connlayer;
  329. PktOut *pktout = ssh2_chanreq_init(
  330. c, "subsystem", want_reply ? ssh2_channel_response : NULL, NULL);
  331. put_stringz(pktout, subsystem);
  332. pq_push(s->ppl.out_pq, pktout);
  333. return true;
  334. }
  335. void ssh2channel_send_exit_status(SshChannel *sc, int status)
  336. {
  337. unreachable("Should never be called in the client");
  338. }
  339. void ssh2channel_send_exit_signal(
  340. SshChannel *sc, ptrlen signame, bool core_dumped, ptrlen msg)
  341. {
  342. unreachable("Should never be called in the client");
  343. }
  344. void ssh2channel_send_exit_signal_numeric(
  345. SshChannel *sc, int signum, bool core_dumped, ptrlen msg)
  346. {
  347. unreachable("Should never be called in the client");
  348. }
  349. void ssh2channel_request_x11_forwarding(
  350. SshChannel *sc, bool want_reply, const char *authproto,
  351. const char *authdata, int screen_number, bool oneshot)
  352. {
  353. struct ssh2_channel *c = container_of(sc, struct ssh2_channel, sc);
  354. struct ssh2_connection_state *s = c->connlayer;
  355. PktOut *pktout = ssh2_chanreq_init(
  356. c, "x11-req", want_reply ? ssh2_channel_response : NULL, NULL);
  357. put_bool(pktout, oneshot);
  358. put_stringz(pktout, authproto);
  359. put_stringz(pktout, authdata);
  360. put_uint32(pktout, screen_number);
  361. pq_push(s->ppl.out_pq, pktout);
  362. }
  363. void ssh2channel_request_agent_forwarding(SshChannel *sc, bool want_reply)
  364. {
  365. struct ssh2_channel *c = container_of(sc, struct ssh2_channel, sc);
  366. struct ssh2_connection_state *s = c->connlayer;
  367. PktOut *pktout = ssh2_chanreq_init(
  368. c, "[email protected]",
  369. want_reply ? ssh2_channel_response : NULL, NULL);
  370. pq_push(s->ppl.out_pq, pktout);
  371. }
  372. void ssh2channel_request_pty(
  373. SshChannel *sc, bool want_reply, Conf *conf, int w, int h)
  374. {
  375. struct ssh2_channel *c = container_of(sc, struct ssh2_channel, sc);
  376. struct ssh2_connection_state *s = c->connlayer;
  377. strbuf *modebuf;
  378. PktOut *pktout = ssh2_chanreq_init(
  379. c, "pty-req", want_reply ? ssh2_channel_response : NULL, NULL);
  380. put_stringz(pktout, conf_get_str(conf, CONF_termtype));
  381. put_uint32(pktout, w);
  382. put_uint32(pktout, h);
  383. put_uint32(pktout, 0); /* pixel width */
  384. put_uint32(pktout, 0); /* pixel height */
  385. modebuf = strbuf_new();
  386. write_ttymodes_to_packet(
  387. BinarySink_UPCAST(modebuf), 2,
  388. get_ttymodes_from_conf(s->ppl.seat, conf));
  389. put_stringsb(pktout, modebuf);
  390. pq_push(s->ppl.out_pq, pktout);
  391. }
  392. bool ssh2channel_send_env_var(
  393. SshChannel *sc, bool want_reply, const char *var, const char *value)
  394. {
  395. struct ssh2_channel *c = container_of(sc, struct ssh2_channel, sc);
  396. struct ssh2_connection_state *s = c->connlayer;
  397. PktOut *pktout = ssh2_chanreq_init(
  398. c, "env", want_reply ? ssh2_channel_response : NULL, NULL);
  399. put_stringz(pktout, var);
  400. put_stringz(pktout, value);
  401. pq_push(s->ppl.out_pq, pktout);
  402. return true;
  403. }
  404. bool ssh2channel_send_serial_break(SshChannel *sc, bool want_reply, int length)
  405. {
  406. struct ssh2_channel *c = container_of(sc, struct ssh2_channel, sc);
  407. struct ssh2_connection_state *s = c->connlayer;
  408. PktOut *pktout = ssh2_chanreq_init(
  409. c, "break", want_reply ? ssh2_channel_response : NULL, NULL);
  410. put_uint32(pktout, length);
  411. pq_push(s->ppl.out_pq, pktout);
  412. return true;
  413. }
  414. bool ssh2channel_send_signal(
  415. SshChannel *sc, bool want_reply, const char *signame)
  416. {
  417. struct ssh2_channel *c = container_of(sc, struct ssh2_channel, sc);
  418. struct ssh2_connection_state *s = c->connlayer;
  419. PktOut *pktout = ssh2_chanreq_init(
  420. c, "signal", want_reply ? ssh2_channel_response : NULL, NULL);
  421. put_stringz(pktout, signame);
  422. pq_push(s->ppl.out_pq, pktout);
  423. return true;
  424. }
  425. void ssh2channel_send_terminal_size_change(SshChannel *sc, int w, int h)
  426. {
  427. struct ssh2_channel *c = container_of(sc, struct ssh2_channel, sc);
  428. struct ssh2_connection_state *s = c->connlayer;
  429. PktOut *pktout = ssh2_chanreq_init(c, "window-change", NULL, NULL);
  430. put_uint32(pktout, w);
  431. put_uint32(pktout, h);
  432. put_uint32(pktout, 0); /* pixel width */
  433. put_uint32(pktout, 0); /* pixel height */
  434. pq_push(s->ppl.out_pq, pktout);
  435. }
  436. bool ssh2_connection_need_antispoof_prompt(struct ssh2_connection_state *s)
  437. {
  438. seat_set_trust_status(s->ppl.seat, false);
  439. if (!seat_has_mixed_input_stream(s->ppl.seat))
  440. return false;
  441. if (seat_can_set_trust_status(s->ppl.seat))
  442. return false;
  443. if (ssh_is_bare(s->ppl.ssh))
  444. return false;
  445. return true;
  446. }