connection2-client.c 16 KB

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