portfwd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. /*
  2. * SSH port forwarding.
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include "putty.h"
  7. #include "ssh.h"
  8. #ifndef FALSE
  9. #define FALSE 0
  10. #endif
  11. #ifndef TRUE
  12. #define TRUE 1
  13. #endif
  14. struct PortForwarding {
  15. const struct plug_function_table *fn;
  16. /* the above variable absolutely *must* be the first in this structure */
  17. struct ssh_channel *c; /* channel structure held by ssh.c */
  18. void *backhandle; /* instance of SSH backend itself */
  19. /* Note that backhandle need not be filled in if c is non-NULL */
  20. Socket s;
  21. int throttled, throttle_override;
  22. int ready;
  23. /*
  24. * `dynamic' does double duty. It's set to 0 for an ordinary
  25. * forwarded port, and nonzero for SOCKS-style dynamic port
  26. * forwarding; but the nonzero values are also a state machine
  27. * tracking where the SOCKS exchange has got to.
  28. */
  29. int dynamic;
  30. /*
  31. * `hostname' and `port' are the real hostname and port, once
  32. * we know what we're connecting to.
  33. */
  34. char *hostname;
  35. int port;
  36. /*
  37. * `socksbuf' is the buffer we use to accumulate a SOCKS request.
  38. */
  39. char *socksbuf;
  40. int sockslen, sockssize;
  41. /*
  42. * When doing dynamic port forwarding, we can receive
  43. * connection data before we are actually able to send it; so
  44. * we may have to temporarily hold some in a dynamically
  45. * allocated buffer here.
  46. */
  47. void *buffer;
  48. int buflen;
  49. };
  50. struct PortListener {
  51. const struct plug_function_table *fn;
  52. /* the above variable absolutely *must* be the first in this structure */
  53. void *backhandle; /* instance of SSH backend itself */
  54. Socket s;
  55. /*
  56. * `dynamic' is set to 0 for an ordinary forwarded port, and
  57. * nonzero for SOCKS-style dynamic port forwarding.
  58. */
  59. int dynamic;
  60. /*
  61. * `hostname' and `port' are the real hostname and port, for
  62. * ordinary forwardings.
  63. */
  64. char *hostname;
  65. int port;
  66. };
  67. static struct PortForwarding *new_portfwd_state(void)
  68. {
  69. struct PortForwarding *pf = snew(struct PortForwarding);
  70. pf->hostname = NULL;
  71. pf->socksbuf = NULL;
  72. pf->sockslen = pf->sockssize = 0;
  73. pf->buffer = NULL;
  74. return pf;
  75. }
  76. static void free_portfwd_state(struct PortForwarding *pf)
  77. {
  78. if (!pf)
  79. return;
  80. sfree(pf->hostname);
  81. sfree(pf->socksbuf);
  82. sfree(pf->buffer);
  83. sfree(pf);
  84. }
  85. static struct PortListener *new_portlistener_state(void)
  86. {
  87. struct PortListener *pl = snew(struct PortListener);
  88. pl->hostname = NULL;
  89. return pl;
  90. }
  91. static void free_portlistener_state(struct PortListener *pl)
  92. {
  93. if (!pl)
  94. return;
  95. sfree(pl->hostname);
  96. sfree(pl);
  97. }
  98. static void pfd_log(Plug plug, int type, SockAddr addr, int port,
  99. const char *error_msg, int error_code)
  100. {
  101. /* we have to dump these since we have no interface to logging.c */
  102. }
  103. static void pfl_log(Plug plug, int type, SockAddr addr, int port,
  104. const char *error_msg, int error_code)
  105. {
  106. /* we have to dump these since we have no interface to logging.c */
  107. }
  108. static void pfd_closing(Plug plug, const char *error_msg, int error_code,
  109. int calling_back)
  110. {
  111. struct PortForwarding *pf = (struct PortForwarding *) plug;
  112. if (error_msg) {
  113. /*
  114. * Socket error. Slam the connection instantly shut.
  115. */
  116. if (pf->c) {
  117. sshfwd_unclean_close(pf->c, error_msg);
  118. } else {
  119. /*
  120. * We might not have an SSH channel, if a socket error
  121. * occurred during SOCKS negotiation. If not, we must
  122. * clean ourself up without sshfwd_unclean_close's call
  123. * back to pfd_close.
  124. */
  125. pfd_close(pf);
  126. }
  127. } else {
  128. /*
  129. * Ordinary EOF received on socket. Send an EOF on the SSH
  130. * channel.
  131. */
  132. if (pf->c)
  133. sshfwd_write_eof(pf->c);
  134. }
  135. }
  136. static void pfl_closing(Plug plug, const char *error_msg, int error_code,
  137. int calling_back)
  138. {
  139. struct PortListener *pl = (struct PortListener *) plug;
  140. pfl_terminate(pl);
  141. }
  142. static void wrap_send_port_open(void *channel, const char *hostname, int port,
  143. Socket s)
  144. {
  145. char *peerinfo, *description;
  146. peerinfo = sk_peer_info(s);
  147. if (peerinfo) {
  148. description = dupprintf("forwarding from %s", peerinfo);
  149. sfree(peerinfo);
  150. } else {
  151. description = dupstr("forwarding");
  152. }
  153. ssh_send_port_open(channel, hostname, port, description);
  154. sfree(description);
  155. }
  156. static void pfd_receive(Plug plug, int urgent, char *data, int len)
  157. {
  158. struct PortForwarding *pf = (struct PortForwarding *) plug;
  159. if (pf->dynamic) {
  160. while (len--) {
  161. if (pf->sockslen >= pf->sockssize) {
  162. pf->sockssize = pf->sockslen * 5 / 4 + 256;
  163. pf->socksbuf = sresize(pf->socksbuf, pf->sockssize, char);
  164. }
  165. pf->socksbuf[pf->sockslen++] = *data++;
  166. /*
  167. * Now check what's in the buffer to see if it's a
  168. * valid and complete message in the SOCKS exchange.
  169. */
  170. if ((pf->dynamic == 1 || (pf->dynamic >> 12) == 4) &&
  171. pf->socksbuf[0] == 4) {
  172. /*
  173. * SOCKS 4.
  174. */
  175. if (pf->dynamic == 1)
  176. pf->dynamic = 0x4000;
  177. if (pf->sockslen < 2)
  178. continue; /* don't have command code yet */
  179. if (pf->socksbuf[1] != 1) {
  180. /* Not CONNECT. */
  181. /* Send back a SOCKS 4 error before closing. */
  182. char data[8];
  183. memset(data, 0, sizeof(data));
  184. data[1] = 91; /* generic `request rejected' */
  185. sk_write(pf->s, data, 8);
  186. pfd_close(pf);
  187. return;
  188. }
  189. if (pf->sockslen <= 8)
  190. continue; /* haven't started user/hostname */
  191. if (pf->socksbuf[pf->sockslen-1] != 0)
  192. continue; /* haven't _finished_ user/hostname */
  193. /*
  194. * Now we have a full SOCKS 4 request. Check it to
  195. * see if it's a SOCKS 4A request.
  196. */
  197. if (pf->socksbuf[4] == 0 && pf->socksbuf[5] == 0 &&
  198. pf->socksbuf[6] == 0 && pf->socksbuf[7] != 0) {
  199. /*
  200. * It's SOCKS 4A. So if we haven't yet
  201. * collected the host name, we should continue
  202. * waiting for data in order to do so; if we
  203. * have, we can go ahead.
  204. */
  205. int len;
  206. if (pf->dynamic == 0x4000) {
  207. pf->dynamic = 0x4001;
  208. pf->sockslen = 8; /* reset buffer to overwrite name */
  209. continue;
  210. }
  211. pf->socksbuf[0] = 0; /* reply version code */
  212. pf->socksbuf[1] = 90; /* request granted */
  213. sk_write(pf->s, pf->socksbuf, 8);
  214. len = pf->sockslen - 8;
  215. pf->port = GET_16BIT_MSB_FIRST(pf->socksbuf+2);
  216. pf->hostname = snewn(len+1, char);
  217. pf->hostname[len] = '\0';
  218. memcpy(pf->hostname, pf->socksbuf + 8, len);
  219. goto connect;
  220. } else {
  221. /*
  222. * It's SOCKS 4, which means we should format
  223. * the IP address into the hostname string and
  224. * then just go.
  225. */
  226. pf->socksbuf[0] = 0; /* reply version code */
  227. pf->socksbuf[1] = 90; /* request granted */
  228. sk_write(pf->s, pf->socksbuf, 8);
  229. pf->port = GET_16BIT_MSB_FIRST(pf->socksbuf+2);
  230. pf->hostname = dupprintf("%d.%d.%d.%d",
  231. (unsigned char)pf->socksbuf[4],
  232. (unsigned char)pf->socksbuf[5],
  233. (unsigned char)pf->socksbuf[6],
  234. (unsigned char)pf->socksbuf[7]);
  235. goto connect;
  236. }
  237. }
  238. if ((pf->dynamic == 1 || (pf->dynamic >> 12) == 5) &&
  239. pf->socksbuf[0] == 5) {
  240. /*
  241. * SOCKS 5.
  242. */
  243. if (pf->dynamic == 1)
  244. pf->dynamic = 0x5000;
  245. if (pf->dynamic == 0x5000) {
  246. int i, method;
  247. char data[2];
  248. /*
  249. * We're receiving a set of method identifiers.
  250. */
  251. if (pf->sockslen < 2)
  252. continue; /* no method count yet */
  253. if (pf->sockslen < 2 + (unsigned char)pf->socksbuf[1])
  254. continue; /* no methods yet */
  255. method = 0xFF; /* invalid */
  256. for (i = 0; i < (unsigned char)pf->socksbuf[1]; i++)
  257. if (pf->socksbuf[2+i] == 0) {
  258. method = 0;/* no auth */
  259. break;
  260. }
  261. data[0] = 5;
  262. data[1] = method;
  263. sk_write(pf->s, data, 2);
  264. pf->dynamic = 0x5001;
  265. pf->sockslen = 0; /* re-empty the buffer */
  266. continue;
  267. }
  268. if (pf->dynamic == 0x5001) {
  269. /*
  270. * We're receiving a SOCKS request.
  271. */
  272. unsigned char reply[10]; /* SOCKS5 atyp=1 reply */
  273. int atype, alen = 0;
  274. /*
  275. * Pre-fill reply packet.
  276. * In all cases, we set BND.{HOST,ADDR} to 0.0.0.0:0
  277. * (atyp=1) in the reply; if we succeed, we don't know
  278. * the right answers, and if we fail, they should be
  279. * ignored.
  280. */
  281. memset(reply, 0, lenof(reply));
  282. reply[0] = 5; /* VER */
  283. reply[3] = 1; /* ATYP = 1 (IPv4, 0.0.0.0:0) */
  284. if (pf->sockslen < 6) continue;
  285. atype = (unsigned char)pf->socksbuf[3];
  286. if (atype == 1) /* IPv4 address */
  287. alen = 4;
  288. if (atype == 4) /* IPv6 address */
  289. alen = 16;
  290. if (atype == 3) /* domain name has leading length */
  291. alen = 1 + (unsigned char)pf->socksbuf[4];
  292. if (pf->sockslen < 6 + alen) continue;
  293. if (pf->socksbuf[1] != 1 || pf->socksbuf[2] != 0) {
  294. /* Not CONNECT or reserved field nonzero - error */
  295. reply[1] = 1; /* generic failure */
  296. sk_write(pf->s, (char *) reply, lenof(reply));
  297. pfd_close(pf);
  298. return;
  299. }
  300. /*
  301. * Now we have a viable connect request. Switch
  302. * on atype.
  303. */
  304. pf->port = GET_16BIT_MSB_FIRST(pf->socksbuf+4+alen);
  305. if (atype == 1) {
  306. /* REP=0 (success) already */
  307. sk_write(pf->s, (char *) reply, lenof(reply));
  308. pf->hostname = dupprintf("%d.%d.%d.%d",
  309. (unsigned char)pf->socksbuf[4],
  310. (unsigned char)pf->socksbuf[5],
  311. (unsigned char)pf->socksbuf[6],
  312. (unsigned char)pf->socksbuf[7]);
  313. goto connect;
  314. } else if (atype == 3) {
  315. /* REP=0 (success) already */
  316. sk_write(pf->s, (char *) reply, lenof(reply));
  317. pf->hostname = snewn(alen, char);
  318. pf->hostname[alen-1] = '\0';
  319. memcpy(pf->hostname, pf->socksbuf + 5, alen-1);
  320. goto connect;
  321. } else {
  322. /*
  323. * Unknown address type. (FIXME: support IPv6!)
  324. */
  325. reply[1] = 8; /* atype not supported */
  326. sk_write(pf->s, (char *) reply, lenof(reply));
  327. pfd_close(pf);
  328. return;
  329. }
  330. }
  331. }
  332. /*
  333. * If we get here without either having done `continue'
  334. * or `goto connect', it must be because there is no
  335. * sensible interpretation of what's in our buffer. So
  336. * close the connection rudely.
  337. */
  338. pfd_close(pf);
  339. break;
  340. }
  341. return;
  342. /*
  343. * We come here when we're ready to make an actual
  344. * connection.
  345. */
  346. connect:
  347. sfree(pf->socksbuf);
  348. pf->socksbuf = NULL;
  349. /*
  350. * Freeze the socket until the SSH server confirms the
  351. * connection.
  352. */
  353. sk_set_frozen(pf->s, 1);
  354. pf->c = new_sock_channel(pf->backhandle, pf);
  355. if (pf->c == NULL) {
  356. pfd_close(pf);
  357. return;
  358. } else {
  359. /* asks to forward to the specified host/port for this */
  360. wrap_send_port_open(pf->c, pf->hostname, pf->port, pf->s);
  361. }
  362. pf->dynamic = 0;
  363. /*
  364. * If there's any data remaining in our current buffer,
  365. * save it to be sent on pfd_confirm().
  366. */
  367. if (len > 0) {
  368. pf->buffer = snewn(len, char);
  369. memcpy(pf->buffer, data, len);
  370. pf->buflen = len;
  371. }
  372. }
  373. if (pf->ready) {
  374. if (sshfwd_write(pf->c, data, len) > 0) {
  375. pf->throttled = 1;
  376. sk_set_frozen(pf->s, 1);
  377. }
  378. }
  379. }
  380. static void pfd_sent(Plug plug, int bufsize)
  381. {
  382. struct PortForwarding *pf = (struct PortForwarding *) plug;
  383. if (pf->c)
  384. sshfwd_unthrottle(pf->c, bufsize);
  385. }
  386. /*
  387. * Called when receiving a PORT OPEN from the server to make a
  388. * connection to a destination host.
  389. *
  390. * On success, returns NULL and fills in *pf_ret. On error, returns a
  391. * dynamically allocated error message string.
  392. */
  393. char *pfd_connect(struct PortForwarding **pf_ret, char *hostname,int port,
  394. void *c, Conf *conf, int addressfamily)
  395. {
  396. static const struct plug_function_table fn_table = {
  397. pfd_log,
  398. pfd_closing,
  399. pfd_receive,
  400. pfd_sent,
  401. NULL
  402. };
  403. SockAddr addr;
  404. const char *err;
  405. char *dummy_realhost;
  406. struct PortForwarding *pf;
  407. /*
  408. * Try to find host.
  409. */
  410. addr = name_lookup(hostname, port, &dummy_realhost, conf, addressfamily,
  411. NULL, NULL);
  412. if ((err = sk_addr_error(addr)) != NULL) {
  413. char *err_ret = dupstr(err);
  414. sk_addr_free(addr);
  415. sfree(dummy_realhost);
  416. return err_ret;
  417. }
  418. /*
  419. * Open socket.
  420. */
  421. pf = *pf_ret = new_portfwd_state();
  422. pf->fn = &fn_table;
  423. pf->throttled = pf->throttle_override = 0;
  424. pf->ready = 1;
  425. pf->c = c;
  426. pf->backhandle = NULL; /* we shouldn't need this */
  427. pf->dynamic = 0;
  428. pf->s = new_connection(addr, dummy_realhost, port,
  429. 0, 1, 0, 0, (Plug) pf, conf);
  430. sfree(dummy_realhost);
  431. if ((err = sk_socket_error(pf->s)) != NULL) {
  432. char *err_ret = dupstr(err);
  433. sk_close(pf->s);
  434. free_portfwd_state(pf);
  435. *pf_ret = NULL;
  436. return err_ret;
  437. }
  438. return NULL;
  439. }
  440. /*
  441. called when someone connects to the local port
  442. */
  443. static int pfl_accepting(Plug p, accept_fn_t constructor, accept_ctx_t ctx)
  444. {
  445. static const struct plug_function_table fn_table = {
  446. pfd_log,
  447. pfd_closing,
  448. pfd_receive,
  449. pfd_sent,
  450. NULL
  451. };
  452. struct PortForwarding *pf;
  453. struct PortListener *pl;
  454. Socket s;
  455. const char *err;
  456. pl = (struct PortListener *)p;
  457. pf = new_portfwd_state();
  458. pf->fn = &fn_table;
  459. pf->c = NULL;
  460. pf->backhandle = pl->backhandle;
  461. pf->s = s = constructor(ctx, (Plug) pf);
  462. if ((err = sk_socket_error(s)) != NULL) {
  463. free_portfwd_state(pf);
  464. return err != NULL;
  465. }
  466. pf->throttled = pf->throttle_override = 0;
  467. pf->ready = 0;
  468. if (pl->dynamic) {
  469. pf->dynamic = 1;
  470. pf->port = 0; /* "hostname" buffer is so far empty */
  471. sk_set_frozen(s, 0); /* we want to receive SOCKS _now_! */
  472. } else {
  473. pf->dynamic = 0;
  474. pf->hostname = dupstr(pl->hostname);
  475. pf->port = pl->port;
  476. pf->c = new_sock_channel(pl->backhandle, pf);
  477. if (pf->c == NULL) {
  478. free_portfwd_state(pf);
  479. return 1;
  480. } else {
  481. /* asks to forward to the specified host/port for this */
  482. wrap_send_port_open(pf->c, pf->hostname, pf->port, s);
  483. }
  484. }
  485. return 0;
  486. }
  487. /*
  488. * Add a new port-forwarding listener from srcaddr:port -> desthost:destport.
  489. *
  490. * On success, returns NULL and fills in *pl_ret. On error, returns a
  491. * dynamically allocated error message string.
  492. */
  493. char *pfl_listen(char *desthost, int destport, char *srcaddr,
  494. int port, void *backhandle, Conf *conf,
  495. struct PortListener **pl_ret, int address_family)
  496. {
  497. static const struct plug_function_table fn_table = {
  498. pfl_log,
  499. pfl_closing,
  500. NULL, /* recv */
  501. NULL, /* send */
  502. pfl_accepting
  503. };
  504. const char *err;
  505. struct PortListener *pl;
  506. /*
  507. * Open socket.
  508. */
  509. pl = *pl_ret = new_portlistener_state();
  510. pl->fn = &fn_table;
  511. if (desthost) {
  512. pl->hostname = dupstr(desthost);
  513. pl->port = destport;
  514. pl->dynamic = 0;
  515. } else
  516. pl->dynamic = 1;
  517. pl->backhandle = backhandle;
  518. pl->s = new_listener(srcaddr, port, (Plug) pl,
  519. !conf_get_int(conf, CONF_lport_acceptall),
  520. conf, address_family);
  521. if ((err = sk_socket_error(pl->s)) != NULL) {
  522. char *err_ret = dupstr(err);
  523. sk_close(pl->s);
  524. free_portlistener_state(pl);
  525. *pl_ret = NULL;
  526. return err_ret;
  527. }
  528. return NULL;
  529. }
  530. void pfd_close(struct PortForwarding *pf)
  531. {
  532. if (!pf)
  533. return;
  534. sk_close(pf->s);
  535. free_portfwd_state(pf);
  536. }
  537. /*
  538. * Terminate a listener.
  539. */
  540. void pfl_terminate(struct PortListener *pl)
  541. {
  542. if (!pl)
  543. return;
  544. sk_close(pl->s);
  545. free_portlistener_state(pl);
  546. }
  547. void pfd_unthrottle(struct PortForwarding *pf)
  548. {
  549. if (!pf)
  550. return;
  551. pf->throttled = 0;
  552. sk_set_frozen(pf->s, pf->throttled || pf->throttle_override);
  553. }
  554. void pfd_override_throttle(struct PortForwarding *pf, int enable)
  555. {
  556. if (!pf)
  557. return;
  558. pf->throttle_override = enable;
  559. sk_set_frozen(pf->s, pf->throttled || pf->throttle_override);
  560. }
  561. /*
  562. * Called to send data down the raw connection.
  563. */
  564. int pfd_send(struct PortForwarding *pf, char *data, int len)
  565. {
  566. if (pf == NULL)
  567. return 0;
  568. return sk_write(pf->s, data, len);
  569. }
  570. void pfd_send_eof(struct PortForwarding *pf)
  571. {
  572. sk_write_eof(pf->s);
  573. }
  574. void pfd_confirm(struct PortForwarding *pf)
  575. {
  576. if (pf == NULL)
  577. return;
  578. pf->ready = 1;
  579. sk_set_frozen(pf->s, 0);
  580. sk_write(pf->s, NULL, 0);
  581. if (pf->buffer) {
  582. sshfwd_write(pf->c, pf->buffer, pf->buflen);
  583. sfree(pf->buffer);
  584. pf->buffer = NULL;
  585. }
  586. }
  587. #ifdef MPEXT
  588. #include "puttyexp.h"
  589. int is_pfwd(void * handle)
  590. {
  591. Plug fn = (Plug)handle;
  592. return
  593. ((*fn)->closing == pfd_closing) ||
  594. ((*fn)->closing == pfl_closing);
  595. }
  596. void * get_pfwd_backend(void * handle)
  597. {
  598. void * backend = NULL;
  599. Plug fn = (Plug)handle;
  600. if ((*fn)->closing == pfl_closing)
  601. {
  602. backend = ((struct PortListener *)handle)->backhandle;
  603. }
  604. else if ((*fn)->closing == pfd_closing)
  605. {
  606. backend = ((struct PortForwarding *)handle)->backhandle;
  607. }
  608. return backend;
  609. }
  610. #endif