server_secure_chttp2.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. *
  3. * Copyright 2015, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include <grpc/grpc.h>
  34. #include <string.h>
  35. #include "src/core/channel/channel_args.h"
  36. #include "src/core/channel/http_server_filter.h"
  37. #include "src/core/iomgr/endpoint.h"
  38. #include "src/core/iomgr/resolve_address.h"
  39. #include "src/core/iomgr/tcp_server.h"
  40. #include "src/core/security/auth_filters.h"
  41. #include "src/core/security/credentials.h"
  42. #include "src/core/security/security_connector.h"
  43. #include "src/core/security/secure_transport_setup.h"
  44. #include "src/core/surface/server.h"
  45. #include "src/core/transport/chttp2_transport.h"
  46. #include <grpc/support/alloc.h>
  47. #include <grpc/support/log.h>
  48. #include <grpc/support/sync.h>
  49. #include <grpc/support/useful.h>
  50. typedef struct grpc_server_secure_state {
  51. grpc_server *server;
  52. grpc_tcp_server *tcp;
  53. grpc_security_connector *sc;
  54. int is_shutdown;
  55. gpr_mu mu;
  56. gpr_refcount refcount;
  57. } grpc_server_secure_state;
  58. static void state_ref(grpc_server_secure_state *state) {
  59. gpr_ref(&state->refcount);
  60. }
  61. static void state_unref(grpc_server_secure_state *state) {
  62. if (gpr_unref(&state->refcount)) {
  63. /* ensure all threads have unlocked */
  64. gpr_mu_lock(&state->mu);
  65. gpr_mu_unlock(&state->mu);
  66. /* clean up */
  67. grpc_security_connector_unref(state->sc);
  68. gpr_free(state);
  69. }
  70. }
  71. static grpc_transport_setup_result setup_transport(void *statep,
  72. grpc_transport *transport,
  73. grpc_mdctx *mdctx) {
  74. static grpc_channel_filter const *extra_filters[] = {
  75. &grpc_server_auth_filter, &grpc_http_server_filter};
  76. grpc_server_secure_state *state = statep;
  77. grpc_transport_setup_result result;
  78. grpc_arg connector_arg = grpc_security_connector_to_arg(state->sc);
  79. grpc_channel_args *args_copy = grpc_channel_args_copy_and_add(
  80. grpc_server_get_channel_args(state->server), &connector_arg);
  81. result = grpc_server_setup_transport(state->server, transport, extra_filters,
  82. GPR_ARRAY_SIZE(extra_filters), mdctx,
  83. args_copy);
  84. grpc_channel_args_destroy(args_copy);
  85. return result;
  86. }
  87. static void on_secure_transport_setup_done(void *statep,
  88. grpc_security_status status,
  89. grpc_endpoint *secure_endpoint) {
  90. grpc_server_secure_state *state = statep;
  91. if (status == GRPC_SECURITY_OK) {
  92. gpr_mu_lock(&state->mu);
  93. if (!state->is_shutdown) {
  94. grpc_create_chttp2_transport(
  95. setup_transport, state, grpc_server_get_channel_args(state->server),
  96. secure_endpoint, NULL, 0, grpc_mdctx_create(), 0);
  97. } else {
  98. /* We need to consume this here, because the server may already have gone
  99. * away. */
  100. grpc_endpoint_destroy(secure_endpoint);
  101. }
  102. gpr_mu_unlock(&state->mu);
  103. } else {
  104. gpr_log(GPR_ERROR, "Secure transport failed with error %d", status);
  105. }
  106. state_unref(state);
  107. }
  108. static void on_accept(void *statep, grpc_endpoint *tcp) {
  109. grpc_server_secure_state *state = statep;
  110. state_ref(state);
  111. grpc_setup_secure_transport(state->sc, tcp, on_secure_transport_setup_done,
  112. state);
  113. }
  114. /* Server callback: start listening on our ports */
  115. static void start(grpc_server *server, void *statep, grpc_pollset **pollsets,
  116. size_t pollset_count) {
  117. grpc_server_secure_state *state = statep;
  118. grpc_tcp_server_start(state->tcp, pollsets, pollset_count, on_accept, state);
  119. }
  120. static void destroy_done(void *statep) {
  121. grpc_server_secure_state *state = statep;
  122. grpc_server_listener_destroy_done(state->server);
  123. state_unref(state);
  124. }
  125. /* Server callback: destroy the tcp listener (so we don't generate further
  126. callbacks) */
  127. static void destroy(grpc_server *server, void *statep) {
  128. grpc_server_secure_state *state = statep;
  129. grpc_tcp_server *tcp;
  130. gpr_mu_lock(&state->mu);
  131. state->is_shutdown = 1;
  132. tcp = state->tcp;
  133. gpr_mu_unlock(&state->mu);
  134. grpc_tcp_server_destroy(tcp, destroy_done, state);
  135. }
  136. int grpc_server_add_secure_http2_port(grpc_server *server, const char *addr,
  137. grpc_server_credentials *creds) {
  138. grpc_resolved_addresses *resolved = NULL;
  139. grpc_tcp_server *tcp = NULL;
  140. grpc_server_secure_state *state = NULL;
  141. size_t i;
  142. unsigned count = 0;
  143. int port_num = -1;
  144. int port_temp;
  145. grpc_security_status status = GRPC_SECURITY_ERROR;
  146. grpc_security_connector *sc = NULL;
  147. /* create security context */
  148. if (creds == NULL) goto error;
  149. status = grpc_server_credentials_create_security_connector(creds, &sc);
  150. if (status != GRPC_SECURITY_OK) {
  151. gpr_log(GPR_ERROR,
  152. "Unable to create secure server with credentials of type %s.",
  153. creds->type);
  154. goto error;
  155. }
  156. /* resolve address */
  157. resolved = grpc_blocking_resolve_address(addr, "https");
  158. if (!resolved) {
  159. goto error;
  160. }
  161. tcp = grpc_tcp_server_create();
  162. if (!tcp) {
  163. goto error;
  164. }
  165. for (i = 0; i < resolved->naddrs; i++) {
  166. port_temp = grpc_tcp_server_add_port(
  167. tcp, (struct sockaddr *)&resolved->addrs[i].addr,
  168. resolved->addrs[i].len);
  169. if (port_temp >= 0) {
  170. if (port_num == -1) {
  171. port_num = port_temp;
  172. } else {
  173. GPR_ASSERT(port_num == port_temp);
  174. }
  175. count++;
  176. }
  177. }
  178. if (count == 0) {
  179. gpr_log(GPR_ERROR, "No address added out of total %d resolved",
  180. resolved->naddrs);
  181. goto error;
  182. }
  183. if (count != resolved->naddrs) {
  184. gpr_log(GPR_ERROR, "Only %d addresses added out of total %d resolved",
  185. count, resolved->naddrs);
  186. /* if it's an error, don't we want to goto error; here ? */
  187. }
  188. grpc_resolved_addresses_destroy(resolved);
  189. state = gpr_malloc(sizeof(*state));
  190. state->server = server;
  191. state->tcp = tcp;
  192. state->sc = sc;
  193. state->is_shutdown = 0;
  194. gpr_mu_init(&state->mu);
  195. gpr_ref_init(&state->refcount, 1);
  196. /* Register with the server only upon success */
  197. grpc_server_add_listener(server, state, start, destroy);
  198. return port_num;
  199. /* Error path: cleanup and return */
  200. error:
  201. if (sc) {
  202. grpc_security_connector_unref(sc);
  203. }
  204. if (resolved) {
  205. grpc_resolved_addresses_destroy(resolved);
  206. }
  207. if (tcp) {
  208. grpc_tcp_server_destroy(tcp, NULL, NULL);
  209. }
  210. if (state) {
  211. gpr_free(state);
  212. }
  213. return 0;
  214. }