tcp-accept.c 4.2 KB

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