ssh2connection-client.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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 (!s->agent_fwd_enabled) {
  79. CHANOPEN_RETURN_FAILURE(
  80. SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED,
  81. ("Agent forwarding is not enabled"));
  82. }
  83. CHANOPEN_RETURN_SUCCESS(agentf_new(sc));
  84. }
  85. ChanopenResult ssh2_connection_parse_channel_open(
  86. struct ssh2_connection_state *s, ptrlen type,
  87. PktIn *pktin, SshChannel *sc)
  88. {
  89. if (ptrlen_eq_string(type, "x11")) {
  90. ptrlen peeraddr = get_string(pktin);
  91. int peerport = get_uint32(pktin);
  92. return chan_open_x11(s, sc, peeraddr, peerport);
  93. } else if (ptrlen_eq_string(type, "forwarded-tcpip")) {
  94. ptrlen fwdaddr = get_string(pktin);
  95. int fwdport = toint(get_uint32(pktin));
  96. ptrlen peeraddr = get_string(pktin);
  97. int peerport = toint(get_uint32(pktin));
  98. return chan_open_forwarded_tcpip(
  99. s, sc, fwdaddr, fwdport, peeraddr, peerport);
  100. } else if (ptrlen_eq_string(type, "[email protected]")) {
  101. return chan_open_auth_agent(s, sc);
  102. } else {
  103. CHANOPEN_RETURN_FAILURE(
  104. SSH2_OPEN_UNKNOWN_CHANNEL_TYPE,
  105. ("Unsupported channel type requested"));
  106. }
  107. }
  108. bool ssh2_connection_parse_global_request(
  109. struct ssh2_connection_state *s, ptrlen type, PktIn *pktin)
  110. {
  111. /*
  112. * We don't know of any global requests that an SSH client needs
  113. * to honour.
  114. */
  115. return false;
  116. }
  117. PktOut *ssh2_portfwd_chanopen(
  118. struct ssh2_connection_state *s, struct ssh2_channel *c,
  119. const char *hostname, int port,
  120. const char *description, const SocketPeerInfo *peerinfo)
  121. {
  122. PacketProtocolLayer *ppl = &s->ppl; /* for ppl_logevent */
  123. PktOut *pktout;
  124. /*
  125. * In client mode, this function is called by portfwdmgr in
  126. * response to PortListeners that were set up in
  127. * portfwdmgr_config, which means that the hostname and port
  128. * parameters will indicate the host we want to tell the server to
  129. * connect _to_.
  130. */
  131. ppl_logevent("Opening connection to %s:%d for %s",
  132. hostname, port, description);
  133. pktout = ssh2_chanopen_init(c, "direct-tcpip");
  134. {
  135. char *trimmed_host = host_strduptrim(hostname);
  136. put_stringz(pktout, trimmed_host);
  137. sfree(trimmed_host);
  138. }
  139. put_uint32(pktout, port);
  140. /*
  141. * We make up values for the originator data; partly it's too much
  142. * hassle to keep track, and partly I'm not convinced the server
  143. * should be told details like that about my local network
  144. * configuration. The "originator IP address" is syntactically a
  145. * numeric IP address, and some servers (e.g., Tectia) get upset
  146. * if it doesn't match this syntax.
  147. */
  148. put_stringz(pktout, "0.0.0.0");
  149. put_uint32(pktout, 0);
  150. return pktout;
  151. }
  152. static int ssh2_rportfwd_cmp(void *av, void *bv)
  153. {
  154. struct ssh_rportfwd *a = (struct ssh_rportfwd *) av;
  155. struct ssh_rportfwd *b = (struct ssh_rportfwd *) bv;
  156. int i;
  157. if ( (i = strcmp(a->shost, b->shost)) != 0)
  158. return i < 0 ? -1 : +1;
  159. if (a->sport > b->sport)
  160. return +1;
  161. if (a->sport < b->sport)
  162. return -1;
  163. return 0;
  164. }
  165. static void ssh2_rportfwd_globreq_response(struct ssh2_connection_state *s,
  166. PktIn *pktin, void *ctx)
  167. {
  168. PacketProtocolLayer *ppl = &s->ppl; /* for ppl_logevent */
  169. struct ssh_rportfwd *rpf = (struct ssh_rportfwd *)ctx;
  170. if (pktin->type == SSH2_MSG_REQUEST_SUCCESS) {
  171. ppl_logevent("Remote port forwarding from %s enabled",
  172. rpf->log_description);
  173. } else {
  174. ppl_logevent("Remote port forwarding from %s refused",
  175. rpf->log_description);
  176. { // WINSCP
  177. struct ssh_rportfwd *realpf = del234(s->rportfwds, rpf);
  178. assert(realpf == rpf);
  179. portfwdmgr_close(s->portfwdmgr, rpf->pfr);
  180. free_rportfwd(rpf);
  181. } // WINSCP
  182. }
  183. }
  184. struct ssh_rportfwd *ssh2_rportfwd_alloc(
  185. ConnectionLayer *cl,
  186. const char *shost, int sport, const char *dhost, int dport,
  187. int addressfamily, const char *log_description, PortFwdRecord *pfr,
  188. ssh_sharing_connstate *share_ctx)
  189. {
  190. struct ssh2_connection_state *s =
  191. container_of(cl, struct ssh2_connection_state, cl);
  192. struct ssh_rportfwd *rpf = snew(struct ssh_rportfwd);
  193. if (!s->rportfwds)
  194. s->rportfwds = newtree234(ssh2_rportfwd_cmp);
  195. rpf->shost = dupstr(shost);
  196. rpf->sport = sport;
  197. rpf->dhost = dupstr(dhost);
  198. rpf->dport = dport;
  199. rpf->addressfamily = addressfamily;
  200. rpf->log_description = dupstr(log_description);
  201. rpf->pfr = pfr;
  202. rpf->share_ctx = share_ctx;
  203. if (add234(s->rportfwds, rpf) != rpf) {
  204. free_rportfwd(rpf);
  205. return NULL;
  206. }
  207. if (!rpf->share_ctx) {
  208. PktOut *pktout = ssh_bpp_new_pktout(
  209. s->ppl.bpp, SSH2_MSG_GLOBAL_REQUEST);
  210. put_stringz(pktout, "tcpip-forward");
  211. put_bool(pktout, true); /* want reply */
  212. put_stringz(pktout, rpf->shost);
  213. put_uint32(pktout, rpf->sport);
  214. pq_push(s->ppl.out_pq, pktout);
  215. ssh2_queue_global_request_handler(
  216. s, ssh2_rportfwd_globreq_response, rpf);
  217. }
  218. return rpf;
  219. }
  220. void ssh2_rportfwd_remove(ConnectionLayer *cl, struct ssh_rportfwd *rpf)
  221. {
  222. struct ssh2_connection_state *s =
  223. container_of(cl, struct ssh2_connection_state, cl);
  224. if (rpf->share_ctx) {
  225. /*
  226. * We don't manufacture a cancel-tcpip-forward message for
  227. * remote port forwardings being removed on behalf of a
  228. * downstream; we just pass through the one the downstream
  229. * sent to us.
  230. */
  231. } else {
  232. PktOut *pktout = ssh_bpp_new_pktout(
  233. s->ppl.bpp, SSH2_MSG_GLOBAL_REQUEST);
  234. put_stringz(pktout, "cancel-tcpip-forward");
  235. put_bool(pktout, false); /* _don't_ want reply */
  236. put_stringz(pktout, rpf->shost);
  237. put_uint32(pktout, rpf->sport);
  238. pq_push(s->ppl.out_pq, pktout);
  239. }
  240. assert(s->rportfwds);
  241. { // WINSCP
  242. struct ssh_rportfwd *realpf = del234(s->rportfwds, rpf);
  243. assert(realpf == rpf);
  244. } // WINSCP
  245. free_rportfwd(rpf);
  246. }
  247. SshChannel *ssh2_session_open(ConnectionLayer *cl, Channel *chan)
  248. {
  249. struct ssh2_connection_state *s =
  250. container_of(cl, struct ssh2_connection_state, cl);
  251. PacketProtocolLayer *ppl = &s->ppl; /* for ppl_logevent */
  252. struct ssh2_channel *c = snew(struct ssh2_channel);
  253. PktOut *pktout;
  254. c->connlayer = s;
  255. ssh2_channel_init(c);
  256. c->halfopen = true;
  257. c->chan = chan;
  258. ppl_logevent("Opening main session channel");
  259. pktout = ssh2_chanopen_init(c, "session");
  260. pq_push(s->ppl.out_pq, pktout);
  261. return &c->sc;
  262. }
  263. SshChannel *ssh2_serverside_x11_open(
  264. ConnectionLayer *cl, Channel *chan, const SocketPeerInfo *pi)
  265. {
  266. unreachable("Should never be called in the client");
  267. }
  268. SshChannel *ssh2_serverside_agent_open(ConnectionLayer *cl, Channel *chan)
  269. {
  270. unreachable("Should never be called in the client");
  271. }
  272. static void ssh2_channel_response(
  273. struct ssh2_channel *c, PktIn *pkt, void *ctx)
  274. {
  275. chan_request_response(c->chan, pkt->type == SSH2_MSG_CHANNEL_SUCCESS);
  276. }
  277. void ssh2channel_start_shell(SshChannel *sc, bool want_reply)
  278. {
  279. struct ssh2_channel *c = container_of(sc, struct ssh2_channel, sc);
  280. struct ssh2_connection_state *s = c->connlayer;
  281. PktOut *pktout = ssh2_chanreq_init(
  282. c, "shell", want_reply ? ssh2_channel_response : NULL, NULL);
  283. pq_push(s->ppl.out_pq, pktout);
  284. }
  285. void ssh2channel_start_command(
  286. SshChannel *sc, bool want_reply, const char *command)
  287. {
  288. struct ssh2_channel *c = container_of(sc, struct ssh2_channel, sc);
  289. struct ssh2_connection_state *s = c->connlayer;
  290. PktOut *pktout = ssh2_chanreq_init(
  291. c, "exec", want_reply ? ssh2_channel_response : NULL, NULL);
  292. put_stringz(pktout, command);
  293. pq_push(s->ppl.out_pq, pktout);
  294. }
  295. bool ssh2channel_start_subsystem(
  296. SshChannel *sc, bool want_reply, const char *subsystem)
  297. {
  298. struct ssh2_channel *c = container_of(sc, struct ssh2_channel, sc);
  299. struct ssh2_connection_state *s = c->connlayer;
  300. PktOut *pktout = ssh2_chanreq_init(
  301. c, "subsystem", want_reply ? ssh2_channel_response : NULL, NULL);
  302. put_stringz(pktout, subsystem);
  303. pq_push(s->ppl.out_pq, pktout);
  304. return true;
  305. }
  306. void ssh2channel_send_exit_status(SshChannel *sc, int status)
  307. {
  308. unreachable("Should never be called in the client");
  309. }
  310. void ssh2channel_send_exit_signal(
  311. SshChannel *sc, ptrlen signame, bool core_dumped, ptrlen msg)
  312. {
  313. unreachable("Should never be called in the client");
  314. }
  315. void ssh2channel_send_exit_signal_numeric(
  316. SshChannel *sc, int signum, bool core_dumped, ptrlen msg)
  317. {
  318. unreachable("Should never be called in the client");
  319. }
  320. void ssh2channel_request_x11_forwarding(
  321. SshChannel *sc, bool want_reply, const char *authproto,
  322. const char *authdata, int screen_number, bool oneshot)
  323. {
  324. struct ssh2_channel *c = container_of(sc, struct ssh2_channel, sc);
  325. struct ssh2_connection_state *s = c->connlayer;
  326. PktOut *pktout = ssh2_chanreq_init(
  327. c, "x11-req", want_reply ? ssh2_channel_response : NULL, NULL);
  328. put_bool(pktout, oneshot);
  329. put_stringz(pktout, authproto);
  330. put_stringz(pktout, authdata);
  331. put_uint32(pktout, screen_number);
  332. pq_push(s->ppl.out_pq, pktout);
  333. }
  334. void ssh2channel_request_agent_forwarding(SshChannel *sc, bool want_reply)
  335. {
  336. struct ssh2_channel *c = container_of(sc, struct ssh2_channel, sc);
  337. struct ssh2_connection_state *s = c->connlayer;
  338. PktOut *pktout = ssh2_chanreq_init(
  339. c, "[email protected]",
  340. want_reply ? ssh2_channel_response : NULL, NULL);
  341. pq_push(s->ppl.out_pq, pktout);
  342. }
  343. void ssh2channel_request_pty(
  344. SshChannel *sc, bool want_reply, Conf *conf, int w, int h)
  345. {
  346. struct ssh2_channel *c = container_of(sc, struct ssh2_channel, sc);
  347. struct ssh2_connection_state *s = c->connlayer;
  348. strbuf *modebuf;
  349. PktOut *pktout = ssh2_chanreq_init(
  350. c, "pty-req", want_reply ? ssh2_channel_response : NULL, NULL);
  351. put_stringz(pktout, conf_get_str(conf, CONF_termtype));
  352. put_uint32(pktout, w);
  353. put_uint32(pktout, h);
  354. put_uint32(pktout, 0); /* pixel width */
  355. put_uint32(pktout, 0); /* pixel height */
  356. modebuf = strbuf_new();
  357. write_ttymodes_to_packet(
  358. BinarySink_UPCAST(modebuf), 2,
  359. get_ttymodes_from_conf(s->ppl.seat, conf));
  360. put_stringsb(pktout, modebuf);
  361. pq_push(s->ppl.out_pq, pktout);
  362. }
  363. bool ssh2channel_send_env_var(
  364. SshChannel *sc, bool want_reply, const char *var, const char *value)
  365. {
  366. struct ssh2_channel *c = container_of(sc, struct ssh2_channel, sc);
  367. struct ssh2_connection_state *s = c->connlayer;
  368. PktOut *pktout = ssh2_chanreq_init(
  369. c, "env", want_reply ? ssh2_channel_response : NULL, NULL);
  370. put_stringz(pktout, var);
  371. put_stringz(pktout, value);
  372. pq_push(s->ppl.out_pq, pktout);
  373. return true;
  374. }
  375. bool ssh2channel_send_serial_break(SshChannel *sc, bool want_reply, int length)
  376. {
  377. struct ssh2_channel *c = container_of(sc, struct ssh2_channel, sc);
  378. struct ssh2_connection_state *s = c->connlayer;
  379. PktOut *pktout = ssh2_chanreq_init(
  380. c, "break", want_reply ? ssh2_channel_response : NULL, NULL);
  381. put_uint32(pktout, length);
  382. pq_push(s->ppl.out_pq, pktout);
  383. return true;
  384. }
  385. bool ssh2channel_send_signal(
  386. SshChannel *sc, bool want_reply, const char *signame)
  387. {
  388. struct ssh2_channel *c = container_of(sc, struct ssh2_channel, sc);
  389. struct ssh2_connection_state *s = c->connlayer;
  390. PktOut *pktout = ssh2_chanreq_init(
  391. c, "signal", want_reply ? ssh2_channel_response : NULL, NULL);
  392. put_stringz(pktout, signame);
  393. pq_push(s->ppl.out_pq, pktout);
  394. return true;
  395. }
  396. void ssh2channel_send_terminal_size_change(SshChannel *sc, int w, int h)
  397. {
  398. struct ssh2_channel *c = container_of(sc, struct ssh2_channel, sc);
  399. struct ssh2_connection_state *s = c->connlayer;
  400. PktOut *pktout = ssh2_chanreq_init(c, "window-change", NULL, NULL);
  401. put_uint32(pktout, w);
  402. put_uint32(pktout, h);
  403. put_uint32(pktout, 0); /* pixel width */
  404. put_uint32(pktout, 0); /* pixel height */
  405. pq_push(s->ppl.out_pq, pktout);
  406. }
  407. bool ssh2_connection_need_antispoof_prompt(struct ssh2_connection_state *s)
  408. {
  409. return !seat_set_trust_status(s->ppl.seat, false);
  410. }