cli-tcpfwd.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Dropbear SSH
  3. *
  4. * Copyright (c) 2002,2003 Matt Johnston
  5. * All rights reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE. */
  24. #include "includes.h"
  25. #include "options.h"
  26. #include "dbutil.h"
  27. #include "tcpfwd.h"
  28. #include "channel.h"
  29. #include "runopts.h"
  30. #include "session.h"
  31. #include "ssh.h"
  32. #include "netio.h"
  33. #ifdef ENABLE_CLI_REMOTETCPFWD
  34. static int newtcpforwarded(struct Channel * channel);
  35. const struct ChanType cli_chan_tcpremote = {
  36. 1, /* sepfds */
  37. "forwarded-tcpip",
  38. newtcpforwarded,
  39. NULL,
  40. NULL,
  41. NULL
  42. };
  43. #endif
  44. #ifdef ENABLE_CLI_LOCALTCPFWD
  45. static int cli_localtcp(const char* listenaddr,
  46. unsigned int listenport,
  47. const char* remoteaddr,
  48. unsigned int remoteport);
  49. static const struct ChanType cli_chan_tcplocal = {
  50. 1, /* sepfds */
  51. "direct-tcpip",
  52. tcp_prio_inithandler,
  53. NULL,
  54. NULL,
  55. NULL
  56. };
  57. #endif
  58. #ifdef ENABLE_CLI_ANYTCPFWD
  59. static void fwd_failed(const char* format, ...) ATTRIB_PRINTF(1,2);
  60. static void fwd_failed(const char* format, ...)
  61. {
  62. va_list param;
  63. va_start(param, format);
  64. if (cli_opts.exit_on_fwd_failure) {
  65. _dropbear_exit(EXIT_FAILURE, format, param);
  66. } else {
  67. _dropbear_log(LOG_WARNING, format, param);
  68. }
  69. va_end(param);
  70. }
  71. #endif
  72. #ifdef ENABLE_CLI_LOCALTCPFWD
  73. void setup_localtcp() {
  74. m_list_elem *iter;
  75. int ret;
  76. TRACE(("enter setup_localtcp"))
  77. for (iter = cli_opts.localfwds->first; iter; iter = iter->next) {
  78. struct TCPFwdEntry * fwd = (struct TCPFwdEntry*)iter->item;
  79. ret = cli_localtcp(
  80. fwd->listenaddr,
  81. fwd->listenport,
  82. fwd->connectaddr,
  83. fwd->connectport);
  84. if (ret == DROPBEAR_FAILURE) {
  85. fwd_failed("Failed local port forward %s:%d:%s:%d",
  86. fwd->listenaddr,
  87. fwd->listenport,
  88. fwd->connectaddr,
  89. fwd->connectport);
  90. }
  91. }
  92. TRACE(("leave setup_localtcp"))
  93. }
  94. static int cli_localtcp(const char* listenaddr,
  95. unsigned int listenport,
  96. const char* remoteaddr,
  97. unsigned int remoteport) {
  98. struct TCPListener* tcpinfo = NULL;
  99. int ret;
  100. TRACE(("enter cli_localtcp: %d %s %d", listenport, remoteaddr,
  101. remoteport));
  102. tcpinfo = (struct TCPListener*)m_malloc(sizeof(struct TCPListener));
  103. tcpinfo->sendaddr = m_strdup(remoteaddr);
  104. tcpinfo->sendport = remoteport;
  105. if (listenaddr)
  106. {
  107. tcpinfo->listenaddr = m_strdup(listenaddr);
  108. }
  109. else
  110. {
  111. if (opts.listen_fwd_all) {
  112. tcpinfo->listenaddr = m_strdup("");
  113. } else {
  114. tcpinfo->listenaddr = m_strdup("localhost");
  115. }
  116. }
  117. tcpinfo->listenport = listenport;
  118. tcpinfo->chantype = &cli_chan_tcplocal;
  119. tcpinfo->tcp_type = direct;
  120. ret = listen_tcpfwd(tcpinfo);
  121. if (ret == DROPBEAR_FAILURE) {
  122. m_free(tcpinfo);
  123. }
  124. TRACE(("leave cli_localtcp: %d", ret))
  125. return ret;
  126. }
  127. #endif /* ENABLE_CLI_LOCALTCPFWD */
  128. #ifdef ENABLE_CLI_REMOTETCPFWD
  129. static void send_msg_global_request_remotetcp(const char *addr, int port) {
  130. TRACE(("enter send_msg_global_request_remotetcp"))
  131. CHECKCLEARTOWRITE();
  132. buf_putbyte(ses.writepayload, SSH_MSG_GLOBAL_REQUEST);
  133. buf_putstring(ses.writepayload, "tcpip-forward", 13);
  134. buf_putbyte(ses.writepayload, 1); /* want_reply */
  135. buf_putstring(ses.writepayload, addr, strlen(addr));
  136. buf_putint(ses.writepayload, port);
  137. encrypt_packet();
  138. TRACE(("leave send_msg_global_request_remotetcp"))
  139. }
  140. /* The only global success/failure messages are for remotetcp.
  141. * Since there isn't any identifier in these messages, we have to rely on them
  142. * being in the same order as we sent the requests. This is the ordering
  143. * of the cli_opts.remotefwds list.
  144. * If the requested remote port is 0 the listen port will be
  145. * dynamically allocated by the server and the port number will be returned
  146. * to client and the port number reported to the user. */
  147. void cli_recv_msg_request_success() {
  148. /* We just mark off that we have received the reply,
  149. * so that we can report failure for later ones. */
  150. m_list_elem * iter = NULL;
  151. for (iter = cli_opts.remotefwds->first; iter; iter = iter->next) {
  152. struct TCPFwdEntry *fwd = (struct TCPFwdEntry*)iter->item;
  153. if (!fwd->have_reply) {
  154. fwd->have_reply = 1;
  155. if (fwd->listenport == 0) {
  156. /* The server should let us know which port was allocated if we requested port 0 */
  157. int allocport = buf_getint(ses.payload);
  158. if (allocport > 0) {
  159. fwd->listenport = allocport;
  160. dropbear_log(LOG_INFO, "Allocated port %d for remote forward to %s:%d",
  161. allocport, fwd->connectaddr, fwd->connectport);
  162. }
  163. }
  164. return;
  165. }
  166. }
  167. }
  168. void cli_recv_msg_request_failure() {
  169. m_list_elem *iter;
  170. for (iter = cli_opts.remotefwds->first; iter; iter = iter->next) {
  171. struct TCPFwdEntry *fwd = (struct TCPFwdEntry*)iter->item;
  172. if (!fwd->have_reply) {
  173. fwd->have_reply = 1;
  174. fwd_failed("Remote TCP forward request failed (port %d -> %s:%d)",
  175. fwd->listenport,
  176. fwd->connectaddr,
  177. fwd->connectport);
  178. return;
  179. }
  180. }
  181. }
  182. void setup_remotetcp() {
  183. m_list_elem *iter;
  184. TRACE(("enter setup_remotetcp"))
  185. for (iter = cli_opts.remotefwds->first; iter; iter = iter->next) {
  186. struct TCPFwdEntry *fwd = (struct TCPFwdEntry*)iter->item;
  187. if (!fwd->listenaddr)
  188. {
  189. /* we store the addresses so that we can compare them
  190. when the server sends them back */
  191. if (opts.listen_fwd_all) {
  192. fwd->listenaddr = m_strdup("");
  193. } else {
  194. fwd->listenaddr = m_strdup("localhost");
  195. }
  196. }
  197. send_msg_global_request_remotetcp(fwd->listenaddr, fwd->listenport);
  198. }
  199. TRACE(("leave setup_remotetcp"))
  200. }
  201. static int newtcpforwarded(struct Channel * channel) {
  202. char *origaddr = NULL;
  203. unsigned int origport;
  204. m_list_elem * iter = NULL;
  205. struct TCPFwdEntry *fwd;
  206. char portstring[NI_MAXSERV];
  207. int err = SSH_OPEN_ADMINISTRATIVELY_PROHIBITED;
  208. origaddr = buf_getstring(ses.payload, NULL);
  209. origport = buf_getint(ses.payload);
  210. /* Find which port corresponds. First try and match address as well as port,
  211. in case they want to forward different ports separately ... */
  212. for (iter = cli_opts.remotefwds->first; iter; iter = iter->next) {
  213. fwd = (struct TCPFwdEntry*)iter->item;
  214. if (origport == fwd->listenport
  215. && strcmp(origaddr, fwd->listenaddr) == 0) {
  216. break;
  217. }
  218. }
  219. if (!iter)
  220. {
  221. /* ... otherwise try to generically match the only forwarded port
  222. without address (also handles ::1 vs 127.0.0.1 vs localhost case).
  223. rfc4254 is vague about the definition of "address that was connected" */
  224. for (iter = cli_opts.remotefwds->first; iter; iter = iter->next) {
  225. fwd = (struct TCPFwdEntry*)iter->item;
  226. if (origport == fwd->listenport) {
  227. break;
  228. }
  229. }
  230. }
  231. if (iter == NULL) {
  232. /* We didn't request forwarding on that port */
  233. cleantext(origaddr);
  234. dropbear_log(LOG_INFO, "Server sent unrequested forward from \"%s:%d\"",
  235. origaddr, origport);
  236. goto out;
  237. }
  238. snprintf(portstring, sizeof(portstring), "%u", fwd->connectport);
  239. channel->conn_pending = connect_remote(fwd->connectaddr, portstring, channel_connect_done, channel);
  240. channel->prio = DROPBEAR_CHANNEL_PRIO_UNKNOWABLE;
  241. err = SSH_OPEN_IN_PROGRESS;
  242. out:
  243. m_free(origaddr);
  244. TRACE(("leave newtcpdirect: err %d", err))
  245. return err;
  246. }
  247. #endif /* ENABLE_CLI_REMOTETCPFWD */