portfwd.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  1. /*
  2. * SSH port forwarding.
  3. */
  4. #include <assert.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include "putty.h"
  8. #include "ssh.h"
  9. #include "sshchan.h"
  10. static void logeventf(Frontend *frontend, const char *fmt, ...)
  11. {
  12. va_list ap;
  13. char *buf;
  14. va_start(ap, fmt);
  15. buf = dupvprintf(fmt, ap);
  16. va_end(ap);
  17. logevent(frontend, buf);
  18. sfree(buf);
  19. }
  20. /*
  21. * Enumeration of values that live in the 'socks_state' field of
  22. * struct PortForwarding.
  23. */
  24. typedef enum {
  25. SOCKS_NONE, /* direct connection (no SOCKS, or SOCKS already done) */
  26. SOCKS_INITIAL, /* don't know if we're SOCKS 4 or 5 yet */
  27. SOCKS_4, /* expect a SOCKS 4 (or 4A) connection message */
  28. SOCKS_5_INITIAL, /* expect a SOCKS 5 preliminary message */
  29. SOCKS_5_CONNECT /* expect a SOCKS 5 connection message */
  30. } SocksState;
  31. typedef struct PortForwarding {
  32. SshChannel *c; /* channel structure held by SSH connection layer */
  33. ConnectionLayer *cl; /* the connection layer itself */
  34. /* Note that ssh need not be filled in if c is non-NULL */
  35. Socket *s;
  36. int input_wanted;
  37. int ready;
  38. SocksState socks_state;
  39. /*
  40. * `hostname' and `port' are the real hostname and port, once
  41. * we know what we're connecting to.
  42. */
  43. char *hostname;
  44. int port;
  45. /*
  46. * `socksbuf' is the buffer we use to accumulate the initial SOCKS
  47. * segment of the incoming data, plus anything after that that we
  48. * receive before we're ready to send data to the SSH server.
  49. */
  50. strbuf *socksbuf;
  51. size_t socksbuf_consumed;
  52. const Plug_vtable *plugvt;
  53. Channel chan;
  54. } PortForwarding;
  55. struct PortListener {
  56. ConnectionLayer *cl;
  57. Socket *s;
  58. int is_dynamic;
  59. /*
  60. * `hostname' and `port' are the real hostname and port, for
  61. * ordinary forwardings.
  62. */
  63. char *hostname;
  64. int port;
  65. const Plug_vtable *plugvt;
  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. return pf;
  73. }
  74. static void free_portfwd_state(struct PortForwarding *pf)
  75. {
  76. if (!pf)
  77. return;
  78. sfree(pf->hostname);
  79. if (pf->socksbuf)
  80. strbuf_free(pf->socksbuf);
  81. sfree(pf);
  82. }
  83. static struct PortListener *new_portlistener_state(void)
  84. {
  85. struct PortListener *pl = snew(struct PortListener);
  86. pl->hostname = NULL;
  87. return pl;
  88. }
  89. static void free_portlistener_state(struct PortListener *pl)
  90. {
  91. if (!pl)
  92. return;
  93. sfree(pl->hostname);
  94. sfree(pl);
  95. }
  96. static void pfd_log(Plug *plug, int type, SockAddr *addr, int port,
  97. const char *error_msg, int error_code)
  98. {
  99. /* we have to dump these since we have no interface to logging.c */
  100. }
  101. static void pfl_log(Plug *plug, int type, SockAddr *addr, int port,
  102. const char *error_msg, int error_code)
  103. {
  104. /* we have to dump these since we have no interface to logging.c */
  105. }
  106. static void pfd_close(struct PortForwarding *pf);
  107. static void pfd_closing(Plug *plug, const char *error_msg, int error_code,
  108. int calling_back)
  109. {
  110. struct PortForwarding *pf = FROMFIELD(plug, struct PortForwarding, plugvt);
  111. if (error_msg) {
  112. /*
  113. * Socket error. Slam the connection instantly shut.
  114. */
  115. if (pf->c) {
  116. sshfwd_unclean_close(pf->c, error_msg);
  117. } else {
  118. /*
  119. * We might not have an SSH channel, if a socket error
  120. * occurred during SOCKS negotiation. If not, we must
  121. * clean ourself up without sshfwd_unclean_close's call
  122. * back to pfd_close.
  123. */
  124. pfd_close(pf);
  125. }
  126. } else {
  127. /*
  128. * Ordinary EOF received on socket. Send an EOF on the SSH
  129. * channel.
  130. */
  131. if (pf->c)
  132. sshfwd_write_eof(pf->c);
  133. }
  134. }
  135. static void pfl_terminate(struct PortListener *pl);
  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 SshChannel *wrap_lportfwd_open(
  143. ConnectionLayer *cl, const char *hostname, int port,
  144. Socket *s, Channel *chan)
  145. {
  146. char *peerinfo, *description;
  147. SshChannel *toret;
  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. toret = ssh_lportfwd_open(cl, hostname, port, description, chan);
  156. sfree(description);
  157. return toret;
  158. }
  159. static char *ipv4_to_string(unsigned ipv4)
  160. {
  161. return dupprintf("%u.%u.%u.%u",
  162. (ipv4 >> 24) & 0xFF, (ipv4 >> 16) & 0xFF,
  163. (ipv4 >> 8) & 0xFF, (ipv4 ) & 0xFF);
  164. }
  165. static char *ipv6_to_string(ptrlen ipv6)
  166. {
  167. const unsigned char *addr = ipv6.ptr;
  168. assert(ipv6.len == 16);
  169. return dupprintf("%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x",
  170. (unsigned)GET_16BIT_MSB_FIRST(addr + 0),
  171. (unsigned)GET_16BIT_MSB_FIRST(addr + 2),
  172. (unsigned)GET_16BIT_MSB_FIRST(addr + 4),
  173. (unsigned)GET_16BIT_MSB_FIRST(addr + 6),
  174. (unsigned)GET_16BIT_MSB_FIRST(addr + 8),
  175. (unsigned)GET_16BIT_MSB_FIRST(addr + 10),
  176. (unsigned)GET_16BIT_MSB_FIRST(addr + 12),
  177. (unsigned)GET_16BIT_MSB_FIRST(addr + 14));
  178. }
  179. static void pfd_receive(Plug *plug, int urgent, char *data, int len)
  180. {
  181. struct PortForwarding *pf = FROMFIELD(plug, struct PortForwarding, plugvt);
  182. if (len == 0)
  183. return;
  184. if (pf->socks_state != SOCKS_NONE) {
  185. BinarySource src[1];
  186. /*
  187. * Store all the data we've got in socksbuf.
  188. */
  189. put_data(pf->socksbuf, data, len);
  190. /*
  191. * Check the start of socksbuf to see if it's a valid and
  192. * complete message in the SOCKS exchange.
  193. */
  194. if (pf->socks_state == SOCKS_INITIAL) {
  195. /* Preliminary: check the first byte of the data (which we
  196. * _must_ have by now) to find out which SOCKS major
  197. * version we're speaking. */
  198. switch (pf->socksbuf->u[0]) {
  199. case 4:
  200. pf->socks_state = SOCKS_4;
  201. break;
  202. case 5:
  203. pf->socks_state = SOCKS_5_INITIAL;
  204. break;
  205. default:
  206. pfd_close(pf); /* unrecognised version */
  207. return;
  208. }
  209. }
  210. BinarySource_BARE_INIT(src, pf->socksbuf->u, pf->socksbuf->len);
  211. get_data(src, pf->socksbuf_consumed);
  212. while (pf->socks_state != SOCKS_NONE) {
  213. unsigned socks_version, message_type, reserved_byte;
  214. unsigned reply_code, port, ipv4, method;
  215. ptrlen methods;
  216. const char *socks4_hostname;
  217. strbuf *output;
  218. switch (pf->socks_state) {
  219. case SOCKS_INITIAL:
  220. case SOCKS_NONE:
  221. assert(0 && "These case values cannot appear");
  222. case SOCKS_4:
  223. /* SOCKS 4/4A connect message */
  224. socks_version = get_byte(src);
  225. message_type = get_byte(src);
  226. if (get_err(src) == BSE_OUT_OF_DATA)
  227. return;
  228. if (socks_version == 4 && message_type == 1) {
  229. /* CONNECT message */
  230. int name_based = FALSE;
  231. port = get_uint16(src);
  232. ipv4 = get_uint32(src);
  233. if (ipv4 > 0x00000000 && ipv4 < 0x00000100) {
  234. /*
  235. * Addresses in this range indicate the SOCKS 4A
  236. * extension to specify a hostname, which comes
  237. * after the username.
  238. */
  239. name_based = TRUE;
  240. }
  241. get_asciz(src); /* skip username */
  242. socks4_hostname = name_based ? get_asciz(src) : NULL;
  243. if (get_err(src) == BSE_OUT_OF_DATA)
  244. return;
  245. if (get_err(src))
  246. goto socks4_reject;
  247. pf->port = port;
  248. if (name_based) {
  249. pf->hostname = dupstr(socks4_hostname);
  250. } else {
  251. pf->hostname = ipv4_to_string(ipv4);
  252. }
  253. output = strbuf_new();
  254. put_byte(output, 0); /* reply version */
  255. put_byte(output, 90); /* SOCKS 4 'request granted' */
  256. put_uint16(output, 0); /* null port field */
  257. put_uint32(output, 0); /* null address field */
  258. sk_write(pf->s, output->u, output->len);
  259. strbuf_free(output);
  260. pf->socks_state = SOCKS_NONE;
  261. pf->socksbuf_consumed = src->pos;
  262. break;
  263. }
  264. socks4_reject:
  265. output = strbuf_new();
  266. put_byte(output, 0); /* reply version */
  267. put_byte(output, 91); /* SOCKS 4 'request rejected' */
  268. put_uint16(output, 0); /* null port field */
  269. put_uint32(output, 0); /* null address field */
  270. sk_write(pf->s, output->u, output->len);
  271. strbuf_free(output);
  272. pfd_close(pf);
  273. return;
  274. case SOCKS_5_INITIAL:
  275. /* SOCKS 5 initial method list */
  276. socks_version = get_byte(src);
  277. methods = get_pstring(src);
  278. method = 0xFF; /* means 'no usable method found' */
  279. {
  280. int i;
  281. for (i = 0; i < methods.len; i++) {
  282. if (((const unsigned char *)methods.ptr)[i] == 0 ) {
  283. method = 0; /* no auth */
  284. break;
  285. }
  286. }
  287. }
  288. if (get_err(src) == BSE_OUT_OF_DATA)
  289. return;
  290. if (get_err(src))
  291. method = 0xFF;
  292. output = strbuf_new();
  293. put_byte(output, 5); /* SOCKS version */
  294. put_byte(output, method); /* selected auth method */
  295. sk_write(pf->s, output->u, output->len);
  296. strbuf_free(output);
  297. if (method == 0xFF) {
  298. pfd_close(pf);
  299. return;
  300. }
  301. pf->socks_state = SOCKS_5_CONNECT;
  302. pf->socksbuf_consumed = src->pos;
  303. break;
  304. case SOCKS_5_CONNECT:
  305. /* SOCKS 5 connect message */
  306. socks_version = get_byte(src);
  307. message_type = get_byte(src);
  308. reserved_byte = get_byte(src);
  309. if (socks_version == 5 && message_type == 1 &&
  310. reserved_byte == 0) {
  311. reply_code = 0; /* success */
  312. switch (get_byte(src)) {
  313. case 1: /* IPv4 */
  314. pf->hostname = ipv4_to_string(get_uint32(src));
  315. break;
  316. case 4: /* IPv6 */
  317. pf->hostname = ipv6_to_string(get_data(src, 16));
  318. break;
  319. case 3: /* unresolved domain name */
  320. pf->hostname = mkstr(get_pstring(src));
  321. break;
  322. default:
  323. pf->hostname = NULL;
  324. reply_code = 8; /* address type not supported */
  325. break;
  326. }
  327. pf->port = get_uint16(src);
  328. } else {
  329. reply_code = 7; /* command not supported */
  330. }
  331. if (get_err(src) == BSE_OUT_OF_DATA)
  332. return;
  333. if (get_err(src))
  334. reply_code = 1; /* general server failure */
  335. output = strbuf_new();
  336. put_byte(output, 5); /* SOCKS version */
  337. put_byte(output, reply_code);
  338. put_byte(output, 0); /* reserved */
  339. put_byte(output, 1); /* IPv4 address follows */
  340. put_uint32(output, 0); /* bound IPv4 address (unused) */
  341. put_uint16(output, 0); /* bound port number (unused) */
  342. sk_write(pf->s, output->u, output->len);
  343. strbuf_free(output);
  344. if (reply_code != 0) {
  345. pfd_close(pf);
  346. return;
  347. }
  348. pf->socks_state = SOCKS_NONE;
  349. pf->socksbuf_consumed = src->pos;
  350. break;
  351. }
  352. }
  353. /*
  354. * We come here when we're ready to make an actual
  355. * connection.
  356. */
  357. /*
  358. * Freeze the socket until the SSH server confirms the
  359. * connection.
  360. */
  361. sk_set_frozen(pf->s, 1);
  362. pf->c = wrap_lportfwd_open(pf->cl, pf->hostname, pf->port, pf->s,
  363. &pf->chan);
  364. }
  365. if (pf->ready)
  366. sshfwd_write(pf->c, data, len);
  367. }
  368. static void pfd_sent(Plug *plug, int bufsize)
  369. {
  370. struct PortForwarding *pf = FROMFIELD(plug, struct PortForwarding, plugvt);
  371. if (pf->c)
  372. sshfwd_unthrottle(pf->c, bufsize);
  373. }
  374. static const Plug_vtable PortForwarding_plugvt = {
  375. pfd_log,
  376. pfd_closing,
  377. pfd_receive,
  378. pfd_sent,
  379. NULL
  380. };
  381. static void pfd_chan_free(Channel *chan);
  382. static void pfd_open_confirmation(Channel *chan);
  383. static void pfd_open_failure(Channel *chan, const char *errtext);
  384. static int pfd_send(Channel *chan, int is_stderr, const void *data, int len);
  385. static void pfd_send_eof(Channel *chan);
  386. static void pfd_set_input_wanted(Channel *chan, int wanted);
  387. static char *pfd_log_close_msg(Channel *chan);
  388. static const struct ChannelVtable PortForwarding_channelvt = {
  389. pfd_chan_free,
  390. pfd_open_confirmation,
  391. pfd_open_failure,
  392. pfd_send,
  393. pfd_send_eof,
  394. pfd_set_input_wanted,
  395. pfd_log_close_msg,
  396. chan_no_eager_close,
  397. };
  398. /*
  399. called when someone connects to the local port
  400. */
  401. static int pfl_accepting(Plug *p, accept_fn_t constructor, accept_ctx_t ctx)
  402. {
  403. struct PortForwarding *pf;
  404. struct PortListener *pl;
  405. Socket *s;
  406. const char *err;
  407. pl = FROMFIELD(p, struct PortListener, plugvt);
  408. pf = new_portfwd_state();
  409. pf->plugvt = &PortForwarding_plugvt;
  410. pf->chan.initial_fixed_window_size = 0;
  411. pf->chan.vt = &PortForwarding_channelvt;
  412. pf->input_wanted = TRUE;
  413. pf->c = NULL;
  414. pf->cl = pl->cl;
  415. pf->s = s = constructor(ctx, &pf->plugvt);
  416. if ((err = sk_socket_error(s)) != NULL) {
  417. free_portfwd_state(pf);
  418. return err != NULL;
  419. }
  420. pf->input_wanted = TRUE;
  421. pf->ready = 0;
  422. if (pl->is_dynamic) {
  423. pf->socks_state = SOCKS_INITIAL;
  424. pf->socksbuf = strbuf_new();
  425. pf->socksbuf_consumed = 0;
  426. pf->port = 0; /* "hostname" buffer is so far empty */
  427. sk_set_frozen(s, 0); /* we want to receive SOCKS _now_! */
  428. } else {
  429. pf->socks_state = SOCKS_NONE;
  430. pf->hostname = dupstr(pl->hostname);
  431. pf->port = pl->port;
  432. pf->c = wrap_lportfwd_open(pl->cl, pf->hostname, pf->port,
  433. s, &pf->chan);
  434. }
  435. return 0;
  436. }
  437. static const Plug_vtable PortListener_plugvt = {
  438. pfl_log,
  439. pfl_closing,
  440. NULL, /* recv */
  441. NULL, /* send */
  442. pfl_accepting
  443. };
  444. /*
  445. * Add a new port-forwarding listener from srcaddr:port -> desthost:destport.
  446. *
  447. * desthost == NULL indicates dynamic SOCKS port forwarding.
  448. *
  449. * On success, returns NULL and fills in *pl_ret. On error, returns a
  450. * dynamically allocated error message string.
  451. */
  452. static char *pfl_listen(char *desthost, int destport, char *srcaddr,
  453. int port, ConnectionLayer *cl, Conf *conf,
  454. struct PortListener **pl_ret, int address_family)
  455. {
  456. const char *err;
  457. struct PortListener *pl;
  458. /*
  459. * Open socket.
  460. */
  461. pl = *pl_ret = new_portlistener_state();
  462. pl->plugvt = &PortListener_plugvt;
  463. if (desthost) {
  464. pl->hostname = dupstr(desthost);
  465. pl->port = destport;
  466. pl->is_dynamic = FALSE;
  467. } else
  468. pl->is_dynamic = TRUE;
  469. pl->cl = cl;
  470. pl->s = new_listener(srcaddr, port, &pl->plugvt,
  471. !conf_get_int(conf, CONF_lport_acceptall),
  472. conf, address_family);
  473. if ((err = sk_socket_error(pl->s)) != NULL) {
  474. char *err_ret = dupstr(err);
  475. sk_close(pl->s);
  476. free_portlistener_state(pl);
  477. *pl_ret = NULL;
  478. return err_ret;
  479. }
  480. return NULL;
  481. }
  482. static char *pfd_log_close_msg(Channel *chan)
  483. {
  484. return dupstr("Forwarded port closed");
  485. }
  486. static void pfd_close(struct PortForwarding *pf)
  487. {
  488. if (!pf)
  489. return;
  490. sk_close(pf->s);
  491. free_portfwd_state(pf);
  492. }
  493. /*
  494. * Terminate a listener.
  495. */
  496. static void pfl_terminate(struct PortListener *pl)
  497. {
  498. if (!pl)
  499. return;
  500. sk_close(pl->s);
  501. free_portlistener_state(pl);
  502. }
  503. static void pfd_set_input_wanted(Channel *chan, int wanted)
  504. {
  505. assert(chan->vt == &PortForwarding_channelvt);
  506. PortForwarding *pf = FROMFIELD(chan, PortForwarding, chan);
  507. pf->input_wanted = wanted;
  508. sk_set_frozen(pf->s, !pf->input_wanted);
  509. }
  510. static void pfd_chan_free(Channel *chan)
  511. {
  512. assert(chan->vt == &PortForwarding_channelvt);
  513. PortForwarding *pf = FROMFIELD(chan, PortForwarding, chan);
  514. pfd_close(pf);
  515. }
  516. /*
  517. * Called to send data down the raw connection.
  518. */
  519. static int pfd_send(Channel *chan, int is_stderr, const void *data, int len)
  520. {
  521. assert(chan->vt == &PortForwarding_channelvt);
  522. PortForwarding *pf = FROMFIELD(chan, PortForwarding, chan);
  523. return sk_write(pf->s, data, len);
  524. }
  525. static void pfd_send_eof(Channel *chan)
  526. {
  527. assert(chan->vt == &PortForwarding_channelvt);
  528. PortForwarding *pf = FROMFIELD(chan, PortForwarding, chan);
  529. sk_write_eof(pf->s);
  530. }
  531. static void pfd_open_confirmation(Channel *chan)
  532. {
  533. assert(chan->vt == &PortForwarding_channelvt);
  534. PortForwarding *pf = FROMFIELD(chan, PortForwarding, chan);
  535. pf->ready = 1;
  536. sk_set_frozen(pf->s, 0);
  537. sk_write(pf->s, NULL, 0);
  538. if (pf->socksbuf) {
  539. sshfwd_write(pf->c, pf->socksbuf->u + pf->socksbuf_consumed,
  540. pf->socksbuf->len - pf->socksbuf_consumed);
  541. strbuf_free(pf->socksbuf);
  542. pf->socksbuf = NULL;
  543. }
  544. }
  545. static void pfd_open_failure(Channel *chan, const char *errtext)
  546. {
  547. assert(chan->vt == &PortForwarding_channelvt);
  548. PortForwarding *pf = FROMFIELD(chan, PortForwarding, chan);
  549. logeventf(pf->cl->frontend,
  550. "Forwarded connection refused by server%s%s",
  551. errtext ? ": " : "", errtext ? errtext : "");
  552. }
  553. /* ----------------------------------------------------------------------
  554. * Code to manage the complete set of currently active port
  555. * forwardings, and update it from Conf.
  556. */
  557. struct PortFwdRecord {
  558. enum { DESTROY, KEEP, CREATE } status;
  559. int type;
  560. unsigned sport, dport;
  561. char *saddr, *daddr;
  562. char *sserv, *dserv;
  563. struct ssh_rportfwd *remote;
  564. int addressfamily;
  565. struct PortListener *local;
  566. };
  567. static int pfr_cmp(void *av, void *bv)
  568. {
  569. PortFwdRecord *a = (PortFwdRecord *) av;
  570. PortFwdRecord *b = (PortFwdRecord *) bv;
  571. int i;
  572. if (a->type > b->type)
  573. return +1;
  574. if (a->type < b->type)
  575. return -1;
  576. if (a->addressfamily > b->addressfamily)
  577. return +1;
  578. if (a->addressfamily < b->addressfamily)
  579. return -1;
  580. if ( (i = nullstrcmp(a->saddr, b->saddr)) != 0)
  581. return i < 0 ? -1 : +1;
  582. if (a->sport > b->sport)
  583. return +1;
  584. if (a->sport < b->sport)
  585. return -1;
  586. if (a->type != 'D') {
  587. if ( (i = nullstrcmp(a->daddr, b->daddr)) != 0)
  588. return i < 0 ? -1 : +1;
  589. if (a->dport > b->dport)
  590. return +1;
  591. if (a->dport < b->dport)
  592. return -1;
  593. }
  594. return 0;
  595. }
  596. void pfr_free(PortFwdRecord *pfr)
  597. {
  598. /* Dispose of any listening socket. */
  599. if (pfr->local)
  600. pfl_terminate(pfr->local);
  601. sfree(pfr->saddr);
  602. sfree(pfr->daddr);
  603. sfree(pfr->sserv);
  604. sfree(pfr->dserv);
  605. sfree(pfr);
  606. }
  607. struct PortFwdManager {
  608. ConnectionLayer *cl;
  609. Conf *conf;
  610. tree234 *forwardings;
  611. };
  612. PortFwdManager *portfwdmgr_new(ConnectionLayer *cl)
  613. {
  614. PortFwdManager *mgr = snew(PortFwdManager);
  615. mgr->cl = cl;
  616. mgr->conf = NULL;
  617. mgr->forwardings = newtree234(pfr_cmp);
  618. return mgr;
  619. }
  620. void portfwdmgr_close(PortFwdManager *mgr, PortFwdRecord *pfr)
  621. {
  622. PortFwdRecord *realpfr = del234(mgr->forwardings, pfr);
  623. if (realpfr == pfr)
  624. pfr_free(pfr);
  625. }
  626. void portfwdmgr_close_all(PortFwdManager *mgr)
  627. {
  628. PortFwdRecord *pfr;
  629. while ((pfr = delpos234(mgr->forwardings, 0)) != NULL)
  630. pfr_free(pfr);
  631. }
  632. void portfwdmgr_free(PortFwdManager *mgr)
  633. {
  634. portfwdmgr_close_all(mgr);
  635. freetree234(mgr->forwardings);
  636. if (mgr->conf)
  637. conf_free(mgr->conf);
  638. sfree(mgr);
  639. }
  640. void portfwdmgr_config(PortFwdManager *mgr, Conf *conf)
  641. {
  642. PortFwdRecord *pfr;
  643. int i;
  644. char *key, *val;
  645. if (mgr->conf)
  646. conf_free(mgr->conf);
  647. mgr->conf = conf_copy(conf);
  648. /*
  649. * Go through the existing port forwardings and tag them
  650. * with status==DESTROY. Any that we want to keep will be
  651. * re-enabled (status==KEEP) as we go through the
  652. * configuration and find out which bits are the same as
  653. * they were before.
  654. */
  655. for (i = 0; (pfr = index234(mgr->forwardings, i)) != NULL; i++)
  656. pfr->status = DESTROY;
  657. for (val = conf_get_str_strs(conf, CONF_portfwd, NULL, &key);
  658. val != NULL;
  659. val = conf_get_str_strs(conf, CONF_portfwd, key, &key)) {
  660. char *kp, *kp2, *vp, *vp2;
  661. char address_family, type;
  662. int sport, dport, sserv, dserv;
  663. char *sports, *dports, *saddr, *host;
  664. kp = key;
  665. address_family = 'A';
  666. type = 'L';
  667. if (*kp == 'A' || *kp == '4' || *kp == '6')
  668. address_family = *kp++;
  669. if (*kp == 'L' || *kp == 'R')
  670. type = *kp++;
  671. if ((kp2 = host_strchr(kp, ':')) != NULL) {
  672. /*
  673. * There's a colon in the middle of the source port
  674. * string, which means that the part before it is
  675. * actually a source address.
  676. */
  677. char *saddr_tmp = dupprintf("%.*s", (int)(kp2 - kp), kp);
  678. saddr = host_strduptrim(saddr_tmp);
  679. sfree(saddr_tmp);
  680. sports = kp2+1;
  681. } else {
  682. saddr = NULL;
  683. sports = kp;
  684. }
  685. sport = atoi(sports);
  686. sserv = 0;
  687. if (sport == 0) {
  688. sserv = 1;
  689. sport = net_service_lookup(sports);
  690. if (!sport) {
  691. logeventf(mgr->cl->frontend, "Service lookup failed for source"
  692. " port \"%s\"", sports);
  693. }
  694. }
  695. if (type == 'L' && !strcmp(val, "D")) {
  696. /* dynamic forwarding */
  697. host = NULL;
  698. dports = NULL;
  699. dport = -1;
  700. dserv = 0;
  701. type = 'D';
  702. } else {
  703. /* ordinary forwarding */
  704. vp = val;
  705. vp2 = vp + host_strcspn(vp, ":");
  706. host = dupprintf("%.*s", (int)(vp2 - vp), vp);
  707. if (*vp2)
  708. vp2++;
  709. dports = vp2;
  710. dport = atoi(dports);
  711. dserv = 0;
  712. if (dport == 0) {
  713. dserv = 1;
  714. dport = net_service_lookup(dports);
  715. if (!dport) {
  716. logeventf(mgr->cl->frontend,
  717. "Service lookup failed for destination"
  718. " port \"%s\"", dports);
  719. }
  720. }
  721. }
  722. if (sport && dport) {
  723. /* Set up a description of the source port. */
  724. pfr = snew(PortFwdRecord);
  725. pfr->type = type;
  726. pfr->saddr = saddr;
  727. pfr->sserv = sserv ? dupstr(sports) : NULL;
  728. pfr->sport = sport;
  729. pfr->daddr = host;
  730. pfr->dserv = dserv ? dupstr(dports) : NULL;
  731. pfr->dport = dport;
  732. pfr->local = NULL;
  733. pfr->remote = NULL;
  734. pfr->addressfamily = (address_family == '4' ? ADDRTYPE_IPV4 :
  735. address_family == '6' ? ADDRTYPE_IPV6 :
  736. ADDRTYPE_UNSPEC);
  737. PortFwdRecord *existing = add234(mgr->forwardings, pfr);
  738. if (existing != pfr) {
  739. if (existing->status == DESTROY) {
  740. /*
  741. * We already have a port forwarding up and running
  742. * with precisely these parameters. Hence, no need
  743. * to do anything; simply re-tag the existing one
  744. * as KEEP.
  745. */
  746. existing->status = KEEP;
  747. }
  748. /*
  749. * Anything else indicates that there was a duplicate
  750. * in our input, which we'll silently ignore.
  751. */
  752. pfr_free(pfr);
  753. } else {
  754. pfr->status = CREATE;
  755. }
  756. } else {
  757. sfree(saddr);
  758. sfree(host);
  759. }
  760. }
  761. /*
  762. * Now go through and destroy any port forwardings which were
  763. * not re-enabled.
  764. */
  765. for (i = 0; (pfr = index234(mgr->forwardings, i)) != NULL; i++) {
  766. if (pfr->status == DESTROY) {
  767. char *message;
  768. message = dupprintf("%s port forwarding from %s%s%d",
  769. pfr->type == 'L' ? "local" :
  770. pfr->type == 'R' ? "remote" : "dynamic",
  771. pfr->saddr ? pfr->saddr : "",
  772. pfr->saddr ? ":" : "",
  773. pfr->sport);
  774. if (pfr->type != 'D') {
  775. char *msg2 = dupprintf("%s to %s:%d", message,
  776. pfr->daddr, pfr->dport);
  777. sfree(message);
  778. message = msg2;
  779. }
  780. logeventf(mgr->cl->frontend, "Cancelling %s", message);
  781. sfree(message);
  782. /* pfr->remote or pfr->local may be NULL if setting up a
  783. * forwarding failed. */
  784. if (pfr->remote) {
  785. /*
  786. * Cancel the port forwarding at the server
  787. * end.
  788. *
  789. * Actually closing the listening port on the server
  790. * side may fail - because in SSH-1 there's no message
  791. * in the protocol to request it!
  792. *
  793. * Instead, we simply remove the record of the
  794. * forwarding from our local end, so that any
  795. * connections the server tries to make on it are
  796. * rejected.
  797. */
  798. ssh_rportfwd_remove(mgr->cl, pfr->remote);
  799. } else if (pfr->local) {
  800. pfl_terminate(pfr->local);
  801. }
  802. delpos234(mgr->forwardings, i);
  803. pfr_free(pfr);
  804. i--; /* so we don't skip one in the list */
  805. }
  806. }
  807. /*
  808. * And finally, set up any new port forwardings (status==CREATE).
  809. */
  810. for (i = 0; (pfr = index234(mgr->forwardings, i)) != NULL; i++) {
  811. if (pfr->status == CREATE) {
  812. char *sportdesc, *dportdesc;
  813. sportdesc = dupprintf("%s%s%s%s%d%s",
  814. pfr->saddr ? pfr->saddr : "",
  815. pfr->saddr ? ":" : "",
  816. pfr->sserv ? pfr->sserv : "",
  817. pfr->sserv ? "(" : "",
  818. pfr->sport,
  819. pfr->sserv ? ")" : "");
  820. if (pfr->type == 'D') {
  821. dportdesc = NULL;
  822. } else {
  823. dportdesc = dupprintf("%s:%s%s%d%s",
  824. pfr->daddr,
  825. pfr->dserv ? pfr->dserv : "",
  826. pfr->dserv ? "(" : "",
  827. pfr->dport,
  828. pfr->dserv ? ")" : "");
  829. }
  830. if (pfr->type == 'L') {
  831. char *err = pfl_listen(pfr->daddr, pfr->dport,
  832. pfr->saddr, pfr->sport,
  833. mgr->cl, conf, &pfr->local,
  834. pfr->addressfamily);
  835. logeventf(mgr->cl->frontend,
  836. "Local %sport %s forwarding to %s%s%s",
  837. pfr->addressfamily == ADDRTYPE_IPV4 ? "IPv4 " :
  838. pfr->addressfamily == ADDRTYPE_IPV6 ? "IPv6 " : "",
  839. sportdesc, dportdesc,
  840. err ? " failed: " : "", err ? err : "");
  841. if (err)
  842. sfree(err);
  843. } else if (pfr->type == 'D') {
  844. char *err = pfl_listen(NULL, -1, pfr->saddr, pfr->sport,
  845. mgr->cl, conf, &pfr->local,
  846. pfr->addressfamily);
  847. logeventf(mgr->cl->frontend,
  848. "Local %sport %s SOCKS dynamic forwarding%s%s",
  849. pfr->addressfamily == ADDRTYPE_IPV4 ? "IPv4 " :
  850. pfr->addressfamily == ADDRTYPE_IPV6 ? "IPv6 " : "",
  851. sportdesc,
  852. err ? " failed: " : "", err ? err : "");
  853. if (err)
  854. sfree(err);
  855. } else {
  856. const char *shost;
  857. if (pfr->saddr) {
  858. shost = pfr->saddr;
  859. } else if (conf_get_int(conf, CONF_rport_acceptall)) {
  860. shost = "";
  861. } else {
  862. shost = "localhost";
  863. }
  864. pfr->remote = ssh_rportfwd_alloc(
  865. mgr->cl, shost, pfr->sport, pfr->daddr, pfr->dport,
  866. pfr->addressfamily, sportdesc, pfr, NULL);
  867. if (!pfr->remote) {
  868. logeventf(mgr->cl->frontend,
  869. "Duplicate remote port forwarding to %s:%d",
  870. pfr->daddr, pfr->dport);
  871. pfr_free(pfr);
  872. } else {
  873. logeventf(mgr->cl->frontend, "Requesting remote port %s"
  874. " forward to %s", sportdesc, dportdesc);
  875. }
  876. }
  877. sfree(sportdesc);
  878. sfree(dportdesc);
  879. }
  880. }
  881. }
  882. /*
  883. * Called when receiving a PORT OPEN from the server to make a
  884. * connection to a destination host.
  885. *
  886. * On success, returns NULL and fills in *pf_ret. On error, returns a
  887. * dynamically allocated error message string.
  888. */
  889. char *portfwdmgr_connect(PortFwdManager *mgr, Channel **chan_ret,
  890. char *hostname, int port, SshChannel *c,
  891. int addressfamily)
  892. {
  893. SockAddr *addr;
  894. const char *err;
  895. char *dummy_realhost = NULL;
  896. struct PortForwarding *pf;
  897. /*
  898. * Try to find host.
  899. */
  900. addr = name_lookup(hostname, port, &dummy_realhost, mgr->conf,
  901. addressfamily, NULL, NULL);
  902. if ((err = sk_addr_error(addr)) != NULL) {
  903. char *err_ret = dupstr(err);
  904. sk_addr_free(addr);
  905. sfree(dummy_realhost);
  906. return err_ret;
  907. }
  908. /*
  909. * Open socket.
  910. */
  911. pf = new_portfwd_state();
  912. *chan_ret = &pf->chan;
  913. pf->plugvt = &PortForwarding_plugvt;
  914. pf->chan.initial_fixed_window_size = 0;
  915. pf->chan.vt = &PortForwarding_channelvt;
  916. pf->input_wanted = TRUE;
  917. pf->ready = 1;
  918. pf->c = c;
  919. pf->cl = mgr->cl;
  920. pf->socks_state = SOCKS_NONE;
  921. pf->s = new_connection(addr, dummy_realhost, port,
  922. 0, 1, 0, 0, &pf->plugvt, mgr->conf);
  923. sfree(dummy_realhost);
  924. if ((err = sk_socket_error(pf->s)) != NULL) {
  925. char *err_ret = dupstr(err);
  926. sk_close(pf->s);
  927. free_portfwd_state(pf);
  928. *chan_ret = NULL;
  929. return err_ret;
  930. }
  931. return NULL;
  932. }