ssh2connection-client.c 16 KB

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