tcp-accept.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 "ssh.h"
  26. #include "tcpfwd.h"
  27. #include "dbutil.h"
  28. #include "session.h"
  29. #include "buffer.h"
  30. #include "packet.h"
  31. #include "listener.h"
  32. #include "listener.h"
  33. #include "runopts.h"
  34. #if DROPBEAR_TCP_ACCEPT
  35. static void cleanup_tcp(const struct Listener *listener) {
  36. struct TCPListener *tcpinfo = (struct TCPListener*)(listener->typedata);
  37. m_free(tcpinfo->sendaddr);
  38. m_free(tcpinfo->listenaddr);
  39. m_free(tcpinfo->request_listenaddr);
  40. m_free(tcpinfo);
  41. }
  42. static void tcp_acceptor(const struct Listener *listener, int sock) {
  43. int fd;
  44. struct sockaddr_storage sa;
  45. socklen_t len;
  46. char ipstring[NI_MAXHOST], portstring[NI_MAXSERV];
  47. struct TCPListener *tcpinfo = (struct TCPListener*)(listener->typedata);
  48. len = sizeof(sa);
  49. fd = accept(sock, (struct sockaddr*)&sa, &len);
  50. if (fd < 0) {
  51. return;
  52. }
  53. if (getnameinfo((struct sockaddr*)&sa, len, ipstring, sizeof(ipstring),
  54. portstring, sizeof(portstring),
  55. NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
  56. m_close(fd);
  57. return;
  58. }
  59. if (send_msg_channel_open_init(fd, tcpinfo->chantype) == DROPBEAR_SUCCESS) {
  60. char* addr = NULL;
  61. unsigned int port = 0;
  62. if (tcpinfo->tcp_type == direct) {
  63. /* "direct-tcpip" */
  64. /* host to connect, port to connect */
  65. addr = tcpinfo->sendaddr;
  66. port = tcpinfo->sendport;
  67. } else {
  68. dropbear_assert(tcpinfo->tcp_type == forwarded);
  69. /* "forwarded-tcpip" */
  70. /* address that was connected, port that was connected */
  71. addr = tcpinfo->request_listenaddr;
  72. port = tcpinfo->listenport;
  73. }
  74. if (addr == NULL) {
  75. addr = "localhost";
  76. }
  77. buf_putstring(ses.writepayload, addr, strlen(addr));
  78. buf_putint(ses.writepayload, port);
  79. /* originator ip */
  80. buf_putstring(ses.writepayload, ipstring, strlen(ipstring));
  81. /* originator port */
  82. buf_putint(ses.writepayload, atol(portstring));
  83. encrypt_packet();
  84. } else {
  85. /* XXX debug? */
  86. close(fd);
  87. }
  88. }
  89. int listen_tcpfwd(struct TCPListener* tcpinfo, struct Listener **ret_listener) {
  90. char portstring[NI_MAXSERV];
  91. int socks[DROPBEAR_MAX_SOCKS];
  92. int nsocks;
  93. struct Listener *listener;
  94. char* errstring = NULL;
  95. TRACE(("enter listen_tcpfwd"))
  96. /* first we try to bind, so don't need to do so much cleanup on failure */
  97. snprintf(portstring, sizeof(portstring), "%u", tcpinfo->listenport);
  98. nsocks = dropbear_listen(tcpinfo->listenaddr, portstring, socks,
  99. DROPBEAR_MAX_SOCKS, &errstring, &ses.maxfd);
  100. if (nsocks < 0) {
  101. dropbear_log(LOG_INFO, "TCP forward failed: %s", errstring);
  102. m_free(errstring);
  103. TRACE(("leave listen_tcpfwd: dropbear_listen failed"))
  104. return DROPBEAR_FAILURE;
  105. }
  106. m_free(errstring);
  107. /* new_listener will close the socks if it fails */
  108. listener = new_listener(socks, nsocks, CHANNEL_ID_TCPFORWARDED, tcpinfo,
  109. tcp_acceptor, cleanup_tcp);
  110. if (listener == NULL) {
  111. TRACE(("leave listen_tcpfwd: listener failed"))
  112. return DROPBEAR_FAILURE;
  113. }
  114. if (ret_listener) {
  115. *ret_listener = listener;
  116. }
  117. TRACE(("leave listen_tcpfwd: success"))
  118. return DROPBEAR_SUCCESS;
  119. }
  120. #endif /* DROPBEAR_TCP_ACCEPT */