portfwd.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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 int 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. return 1;
  136. }
  137. static int pfl_closing(Plug plug, const char *error_msg, int error_code,
  138. int calling_back)
  139. {
  140. struct PortListener *pl = (struct PortListener *) plug;
  141. pfl_terminate(pl);
  142. return 1;
  143. }
  144. static void wrap_send_port_open(void *channel, const char *hostname, int port,
  145. Socket s)
  146. {
  147. char *peerinfo, *description;
  148. peerinfo = sk_peer_info(s);
  149. if (peerinfo) {
  150. description = dupprintf("forwarding from %s", peerinfo);
  151. sfree(peerinfo);
  152. } else {
  153. description = dupstr("forwarding");
  154. }
  155. ssh_send_port_open(channel, hostname, port, description);
  156. sfree(description);
  157. }
  158. static int pfd_receive(Plug plug, int urgent, char *data, int len)
  159. {
  160. struct PortForwarding *pf = (struct PortForwarding *) plug;
  161. if (pf->dynamic) {
  162. while (len--) {
  163. if (pf->sockslen >= pf->sockssize) {
  164. pf->sockssize = pf->sockslen * 5 / 4 + 256;
  165. pf->socksbuf = sresize(pf->socksbuf, pf->sockssize, char);
  166. }
  167. pf->socksbuf[pf->sockslen++] = *data++;
  168. /*
  169. * Now check what's in the buffer to see if it's a
  170. * valid and complete message in the SOCKS exchange.
  171. */
  172. if ((pf->dynamic == 1 || (pf->dynamic >> 12) == 4) &&
  173. pf->socksbuf[0] == 4) {
  174. /*
  175. * SOCKS 4.
  176. */
  177. if (pf->dynamic == 1)
  178. pf->dynamic = 0x4000;
  179. if (pf->sockslen < 2)
  180. continue; /* don't have command code yet */
  181. if (pf->socksbuf[1] != 1) {
  182. /* Not CONNECT. */
  183. /* Send back a SOCKS 4 error before closing. */
  184. char data[8];
  185. memset(data, 0, sizeof(data));
  186. data[1] = 91; /* generic `request rejected' */
  187. sk_write(pf->s, data, 8);
  188. pfd_close(pf);
  189. return 1;
  190. }
  191. if (pf->sockslen <= 8)
  192. continue; /* haven't started user/hostname */
  193. if (pf->socksbuf[pf->sockslen-1] != 0)
  194. continue; /* haven't _finished_ user/hostname */
  195. /*
  196. * Now we have a full SOCKS 4 request. Check it to
  197. * see if it's a SOCKS 4A request.
  198. */
  199. if (pf->socksbuf[4] == 0 && pf->socksbuf[5] == 0 &&
  200. pf->socksbuf[6] == 0 && pf->socksbuf[7] != 0) {
  201. /*
  202. * It's SOCKS 4A. So if we haven't yet
  203. * collected the host name, we should continue
  204. * waiting for data in order to do so; if we
  205. * have, we can go ahead.
  206. */
  207. int len;
  208. if (pf->dynamic == 0x4000) {
  209. pf->dynamic = 0x4001;
  210. pf->sockslen = 8; /* reset buffer to overwrite name */
  211. continue;
  212. }
  213. pf->socksbuf[0] = 0; /* reply version code */
  214. pf->socksbuf[1] = 90; /* request granted */
  215. sk_write(pf->s, pf->socksbuf, 8);
  216. len = pf->sockslen - 8;
  217. pf->port = GET_16BIT_MSB_FIRST(pf->socksbuf+2);
  218. pf->hostname = snewn(len+1, char);
  219. pf->hostname[len] = '\0';
  220. memcpy(pf->hostname, pf->socksbuf + 8, len);
  221. goto connect;
  222. } else {
  223. /*
  224. * It's SOCKS 4, which means we should format
  225. * the IP address into the hostname string and
  226. * then just go.
  227. */
  228. pf->socksbuf[0] = 0; /* reply version code */
  229. pf->socksbuf[1] = 90; /* request granted */
  230. sk_write(pf->s, pf->socksbuf, 8);
  231. pf->port = GET_16BIT_MSB_FIRST(pf->socksbuf+2);
  232. pf->hostname = dupprintf("%d.%d.%d.%d",
  233. (unsigned char)pf->socksbuf[4],
  234. (unsigned char)pf->socksbuf[5],
  235. (unsigned char)pf->socksbuf[6],
  236. (unsigned char)pf->socksbuf[7]);
  237. goto connect;
  238. }
  239. }
  240. if ((pf->dynamic == 1 || (pf->dynamic >> 12) == 5) &&
  241. pf->socksbuf[0] == 5) {
  242. /*
  243. * SOCKS 5.
  244. */
  245. if (pf->dynamic == 1)
  246. pf->dynamic = 0x5000;
  247. if (pf->dynamic == 0x5000) {
  248. int i, method;
  249. char data[2];
  250. /*
  251. * We're receiving a set of method identifiers.
  252. */
  253. if (pf->sockslen < 2)
  254. continue; /* no method count yet */
  255. if (pf->sockslen < 2 + (unsigned char)pf->socksbuf[1])
  256. continue; /* no methods yet */
  257. method = 0xFF; /* invalid */
  258. for (i = 0; i < (unsigned char)pf->socksbuf[1]; i++)
  259. if (pf->socksbuf[2+i] == 0) {
  260. method = 0;/* no auth */
  261. break;
  262. }
  263. data[0] = 5;
  264. data[1] = method;
  265. sk_write(pf->s, data, 2);
  266. pf->dynamic = 0x5001;
  267. pf->sockslen = 0; /* re-empty the buffer */
  268. continue;
  269. }
  270. if (pf->dynamic == 0x5001) {
  271. /*
  272. * We're receiving a SOCKS request.
  273. */
  274. unsigned char reply[10]; /* SOCKS5 atyp=1 reply */
  275. int atype, alen = 0;
  276. /*
  277. * Pre-fill reply packet.
  278. * In all cases, we set BND.{HOST,ADDR} to 0.0.0.0:0
  279. * (atyp=1) in the reply; if we succeed, we don't know
  280. * the right answers, and if we fail, they should be
  281. * ignored.
  282. */
  283. memset(reply, 0, lenof(reply));
  284. reply[0] = 5; /* VER */
  285. reply[3] = 1; /* ATYP = 1 (IPv4, 0.0.0.0:0) */
  286. if (pf->sockslen < 6) continue;
  287. atype = (unsigned char)pf->socksbuf[3];
  288. if (atype == 1) /* IPv4 address */
  289. alen = 4;
  290. if (atype == 4) /* IPv6 address */
  291. alen = 16;
  292. if (atype == 3) /* domain name has leading length */
  293. alen = 1 + (unsigned char)pf->socksbuf[4];
  294. if (pf->sockslen < 6 + alen) continue;
  295. if (pf->socksbuf[1] != 1 || pf->socksbuf[2] != 0) {
  296. /* Not CONNECT or reserved field nonzero - error */
  297. reply[1] = 1; /* generic failure */
  298. sk_write(pf->s, (char *) reply, lenof(reply));
  299. pfd_close(pf);
  300. return 1;
  301. }
  302. /*
  303. * Now we have a viable connect request. Switch
  304. * on atype.
  305. */
  306. pf->port = GET_16BIT_MSB_FIRST(pf->socksbuf+4+alen);
  307. if (atype == 1) {
  308. /* REP=0 (success) already */
  309. sk_write(pf->s, (char *) reply, lenof(reply));
  310. pf->hostname = dupprintf("%d.%d.%d.%d",
  311. (unsigned char)pf->socksbuf[4],
  312. (unsigned char)pf->socksbuf[5],
  313. (unsigned char)pf->socksbuf[6],
  314. (unsigned char)pf->socksbuf[7]);
  315. goto connect;
  316. } else if (atype == 3) {
  317. /* REP=0 (success) already */
  318. sk_write(pf->s, (char *) reply, lenof(reply));
  319. pf->hostname = snewn(alen, char);
  320. pf->hostname[alen-1] = '\0';
  321. memcpy(pf->hostname, pf->socksbuf + 5, alen-1);
  322. goto connect;
  323. } else {
  324. /*
  325. * Unknown address type. (FIXME: support IPv6!)
  326. */
  327. reply[1] = 8; /* atype not supported */
  328. sk_write(pf->s, (char *) reply, lenof(reply));
  329. pfd_close(pf);
  330. return 1;
  331. }
  332. }
  333. }
  334. /*
  335. * If we get here without either having done `continue'
  336. * or `goto connect', it must be because there is no
  337. * sensible interpretation of what's in our buffer. So
  338. * close the connection rudely.
  339. */
  340. pfd_close(pf);
  341. return 1;
  342. }
  343. return 1;
  344. /*
  345. * We come here when we're ready to make an actual
  346. * connection.
  347. */
  348. connect:
  349. sfree(pf->socksbuf);
  350. pf->socksbuf = NULL;
  351. /*
  352. * Freeze the socket until the SSH server confirms the
  353. * connection.
  354. */
  355. sk_set_frozen(pf->s, 1);
  356. pf->c = new_sock_channel(pf->backhandle, pf);
  357. if (pf->c == NULL) {
  358. pfd_close(pf);
  359. return 1;
  360. } else {
  361. /* asks to forward to the specified host/port for this */
  362. wrap_send_port_open(pf->c, pf->hostname, pf->port, pf->s);
  363. }
  364. pf->dynamic = 0;
  365. /*
  366. * If there's any data remaining in our current buffer,
  367. * save it to be sent on pfd_confirm().
  368. */
  369. if (len > 0) {
  370. pf->buffer = snewn(len, char);
  371. memcpy(pf->buffer, data, len);
  372. pf->buflen = len;
  373. }
  374. }
  375. if (pf->ready) {
  376. if (sshfwd_write(pf->c, data, len) > 0) {
  377. pf->throttled = 1;
  378. sk_set_frozen(pf->s, 1);
  379. }
  380. }
  381. return 1;
  382. }
  383. static void pfd_sent(Plug plug, int bufsize)
  384. {
  385. struct PortForwarding *pf = (struct PortForwarding *) plug;
  386. if (pf->c)
  387. sshfwd_unthrottle(pf->c, bufsize);
  388. }
  389. /*
  390. * Called when receiving a PORT OPEN from the server to make a
  391. * connection to a destination host.
  392. *
  393. * On success, returns NULL and fills in *pf_ret. On error, returns a
  394. * dynamically allocated error message string.
  395. */
  396. char *pfd_connect(struct PortForwarding **pf_ret, char *hostname,int port,
  397. void *c, Conf *conf, int addressfamily)
  398. {
  399. static const struct plug_function_table fn_table = {
  400. pfd_log,
  401. pfd_closing,
  402. pfd_receive,
  403. pfd_sent,
  404. NULL
  405. };
  406. SockAddr addr;
  407. const char *err;
  408. char *dummy_realhost;
  409. struct PortForwarding *pf;
  410. /*
  411. * Try to find host.
  412. */
  413. addr = name_lookup(hostname, port, &dummy_realhost, conf, addressfamily,
  414. NULL, NULL);
  415. if ((err = sk_addr_error(addr)) != NULL) {
  416. char *err_ret = dupstr(err);
  417. sk_addr_free(addr);
  418. sfree(dummy_realhost);
  419. return err_ret;
  420. }
  421. /*
  422. * Open socket.
  423. */
  424. pf = *pf_ret = new_portfwd_state();
  425. pf->fn = &fn_table;
  426. pf->throttled = pf->throttle_override = 0;
  427. pf->ready = 1;
  428. pf->c = c;
  429. pf->backhandle = NULL; /* we shouldn't need this */
  430. pf->dynamic = 0;
  431. pf->s = new_connection(addr, dummy_realhost, port,
  432. 0, 1, 0, 0, (Plug) pf, conf);
  433. sfree(dummy_realhost);
  434. if ((err = sk_socket_error(pf->s)) != NULL) {
  435. char *err_ret = dupstr(err);
  436. sk_close(pf->s);
  437. free_portfwd_state(pf);
  438. *pf_ret = NULL;
  439. return err_ret;
  440. }
  441. return NULL;
  442. }
  443. /*
  444. called when someone connects to the local port
  445. */
  446. static int pfl_accepting(Plug p, accept_fn_t constructor, accept_ctx_t ctx)
  447. {
  448. static const struct plug_function_table fn_table = {
  449. pfd_log,
  450. pfd_closing,
  451. pfd_receive,
  452. pfd_sent,
  453. NULL
  454. };
  455. struct PortForwarding *pf;
  456. struct PortListener *pl;
  457. Socket s;
  458. const char *err;
  459. pl = (struct PortListener *)p;
  460. pf = new_portfwd_state();
  461. pf->fn = &fn_table;
  462. pf->c = NULL;
  463. pf->backhandle = pl->backhandle;
  464. pf->s = s = constructor(ctx, (Plug) pf);
  465. if ((err = sk_socket_error(s)) != NULL) {
  466. free_portfwd_state(pf);
  467. return err != NULL;
  468. }
  469. pf->throttled = pf->throttle_override = 0;
  470. pf->ready = 0;
  471. if (pl->dynamic) {
  472. pf->dynamic = 1;
  473. pf->port = 0; /* "hostname" buffer is so far empty */
  474. sk_set_frozen(s, 0); /* we want to receive SOCKS _now_! */
  475. } else {
  476. pf->dynamic = 0;
  477. pf->hostname = dupstr(pl->hostname);
  478. pf->port = pl->port;
  479. pf->c = new_sock_channel(pl->backhandle, pf);
  480. if (pf->c == NULL) {
  481. free_portfwd_state(pf);
  482. return 1;
  483. } else {
  484. /* asks to forward to the specified host/port for this */
  485. wrap_send_port_open(pf->c, pf->hostname, pf->port, s);
  486. }
  487. }
  488. return 0;
  489. }
  490. /*
  491. * Add a new port-forwarding listener from srcaddr:port -> desthost:destport.
  492. *
  493. * On success, returns NULL and fills in *pl_ret. On error, returns a
  494. * dynamically allocated error message string.
  495. */
  496. char *pfl_listen(char *desthost, int destport, char *srcaddr,
  497. int port, void *backhandle, Conf *conf,
  498. struct PortListener **pl_ret, int address_family)
  499. {
  500. static const struct plug_function_table fn_table = {
  501. pfl_log,
  502. pfl_closing,
  503. NULL, /* recv */
  504. NULL, /* send */
  505. pfl_accepting
  506. };
  507. const char *err;
  508. struct PortListener *pl;
  509. /*
  510. * Open socket.
  511. */
  512. pl = *pl_ret = new_portlistener_state();
  513. pl->fn = &fn_table;
  514. if (desthost) {
  515. pl->hostname = dupstr(desthost);
  516. pl->port = destport;
  517. pl->dynamic = 0;
  518. } else
  519. pl->dynamic = 1;
  520. pl->backhandle = backhandle;
  521. pl->s = new_listener(srcaddr, port, (Plug) pl,
  522. !conf_get_int(conf, CONF_lport_acceptall),
  523. conf, address_family);
  524. if ((err = sk_socket_error(pl->s)) != NULL) {
  525. char *err_ret = dupstr(err);
  526. sk_close(pl->s);
  527. free_portlistener_state(pl);
  528. *pl_ret = NULL;
  529. return err_ret;
  530. }
  531. return NULL;
  532. }
  533. void pfd_close(struct PortForwarding *pf)
  534. {
  535. if (!pf)
  536. return;
  537. sk_close(pf->s);
  538. free_portfwd_state(pf);
  539. }
  540. /*
  541. * Terminate a listener.
  542. */
  543. void pfl_terminate(struct PortListener *pl)
  544. {
  545. if (!pl)
  546. return;
  547. sk_close(pl->s);
  548. free_portlistener_state(pl);
  549. }
  550. void pfd_unthrottle(struct PortForwarding *pf)
  551. {
  552. if (!pf)
  553. return;
  554. pf->throttled = 0;
  555. sk_set_frozen(pf->s, pf->throttled || pf->throttle_override);
  556. }
  557. void pfd_override_throttle(struct PortForwarding *pf, int enable)
  558. {
  559. if (!pf)
  560. return;
  561. pf->throttle_override = enable;
  562. sk_set_frozen(pf->s, pf->throttled || pf->throttle_override);
  563. }
  564. /*
  565. * Called to send data down the raw connection.
  566. */
  567. int pfd_send(struct PortForwarding *pf, char *data, int len)
  568. {
  569. if (pf == NULL)
  570. return 0;
  571. return sk_write(pf->s, data, len);
  572. }
  573. void pfd_send_eof(struct PortForwarding *pf)
  574. {
  575. sk_write_eof(pf->s);
  576. }
  577. void pfd_confirm(struct PortForwarding *pf)
  578. {
  579. if (pf == NULL)
  580. return;
  581. pf->ready = 1;
  582. sk_set_frozen(pf->s, 0);
  583. sk_write(pf->s, NULL, 0);
  584. if (pf->buffer) {
  585. sshfwd_write(pf->c, pf->buffer, pf->buflen);
  586. sfree(pf->buffer);
  587. pf->buffer = NULL;
  588. }
  589. }