pt_tls_dispatcher.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (C) 2012 Martin Willi
  3. * Copyright (C) 2012 revosec AG
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. */
  15. #include "pt_tls_dispatcher.h"
  16. #include "pt_tls_server.h"
  17. #include <threading/thread.h>
  18. #include <utils/debug.h>
  19. #include <processing/jobs/callback_job.h>
  20. #include <errno.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. typedef struct private_pt_tls_dispatcher_t private_pt_tls_dispatcher_t;
  24. /**
  25. * Private data of an pt_tls_dispatcher_t object.
  26. */
  27. struct private_pt_tls_dispatcher_t {
  28. /**
  29. * Public pt_tls_dispatcher_t interface.
  30. */
  31. pt_tls_dispatcher_t public;
  32. /**
  33. * Listening socket
  34. */
  35. int fd;
  36. /**
  37. * Client authentication requirements
  38. */
  39. pt_tls_auth_t auth;
  40. /**
  41. * Server identity
  42. */
  43. identification_t *server;
  44. /**
  45. * Peer identity
  46. */
  47. identification_t *peer;
  48. /**
  49. * TNCCS protocol handler constructor
  50. */
  51. pt_tls_tnccs_constructor_t *create;
  52. };
  53. /**
  54. * Open listening server socket
  55. */
  56. static bool open_socket(private_pt_tls_dispatcher_t *this, host_t *host)
  57. {
  58. this->fd = socket(AF_INET, SOCK_STREAM, 0);
  59. if (this->fd == -1)
  60. {
  61. DBG1(DBG_TNC, "opening PT-TLS socket failed: %s", strerror(errno));
  62. return FALSE;
  63. }
  64. if (bind(this->fd, host->get_sockaddr(host),
  65. *host->get_sockaddr_len(host)) == -1)
  66. {
  67. DBG1(DBG_TNC, "binding to PT-TLS socket failed: %s", strerror(errno));
  68. return FALSE;
  69. }
  70. if (listen(this->fd, 5) == -1)
  71. {
  72. DBG1(DBG_TNC, "listen on PT-TLS socket failed: %s", strerror(errno));
  73. return FALSE;
  74. }
  75. return TRUE;
  76. }
  77. /**
  78. * Handle a single PT-TLS client connection
  79. */
  80. static job_requeue_t handle(pt_tls_server_t *connection)
  81. {
  82. while (TRUE)
  83. {
  84. switch (connection->handle(connection))
  85. {
  86. case NEED_MORE:
  87. continue;
  88. case FAILED:
  89. case SUCCESS:
  90. default:
  91. break;
  92. }
  93. break;
  94. }
  95. return JOB_REQUEUE_NONE;
  96. }
  97. /**
  98. * Clean up connection state
  99. */
  100. static void cleanup(pt_tls_server_t *connection)
  101. {
  102. int fd;
  103. fd = connection->get_fd(connection);
  104. connection->destroy(connection);
  105. close(fd);
  106. }
  107. METHOD(pt_tls_dispatcher_t, dispatch, void,
  108. private_pt_tls_dispatcher_t *this,
  109. pt_tls_tnccs_constructor_t *create)
  110. {
  111. while (TRUE)
  112. {
  113. pt_tls_server_t *connection;
  114. tnccs_t *tnccs;
  115. bool old;
  116. int fd;
  117. old = thread_cancelability(TRUE);
  118. fd = accept(this->fd, NULL, NULL);
  119. thread_cancelability(old);
  120. if (fd == -1)
  121. {
  122. DBG1(DBG_TNC, "accepting PT-TLS failed: %s", strerror(errno));
  123. continue;
  124. }
  125. tnccs = create(this->server, this->peer);
  126. if (!tnccs)
  127. {
  128. close(fd);
  129. continue;
  130. }
  131. connection = pt_tls_server_create(this->server, fd, this->auth, tnccs);
  132. if (!connection)
  133. {
  134. close(fd);
  135. continue;
  136. }
  137. lib->processor->queue_job(lib->processor,
  138. (job_t*)callback_job_create_with_prio((callback_job_cb_t)handle,
  139. connection, (void*)cleanup,
  140. (callback_job_cancel_t)return_false,
  141. JOB_PRIO_CRITICAL));
  142. }
  143. }
  144. METHOD(pt_tls_dispatcher_t, destroy, void,
  145. private_pt_tls_dispatcher_t *this)
  146. {
  147. if (this->fd != -1)
  148. {
  149. close(this->fd);
  150. }
  151. this->server->destroy(this->server);
  152. this->peer->destroy(this->peer);
  153. free(this);
  154. }
  155. /**
  156. * See header
  157. */
  158. pt_tls_dispatcher_t *pt_tls_dispatcher_create(host_t *address,
  159. identification_t *id, pt_tls_auth_t auth)
  160. {
  161. private_pt_tls_dispatcher_t *this;
  162. INIT(this,
  163. .public = {
  164. .dispatch = _dispatch,
  165. .destroy = _destroy,
  166. },
  167. .server = id->clone(id),
  168. /* we currently don't authenticate the peer, use %any identity */
  169. .peer = identification_create_from_encoding(ID_ANY, chunk_empty),
  170. .fd = -1,
  171. .auth = auth,
  172. );
  173. if (!open_socket(this, address))
  174. {
  175. destroy(this);
  176. return NULL;
  177. }
  178. return &this->public;
  179. }