test_security_gssapi.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. Copyright (c) 2007-2017 Contributors as noted in the AUTHORS file
  3. This file is part of libzmq, the ZeroMQ core engine in C++.
  4. libzmq is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU Lesser General Public License (LGPL) as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. As a special exception, the Contributors give you permission to link
  9. this library with independent modules to produce an executable,
  10. regardless of the license terms of these independent modules, and to
  11. copy and distribute the resulting executable under terms of your choice,
  12. provided that you also meet, for each linked independent module, the
  13. terms and conditions of the license of that module. An independent
  14. module is a module which is not derived from or based on this library.
  15. If you modify this library, you must extend this exception to your
  16. version of the library.
  17. libzmq is distributed in the hope that it will be useful, but WITHOUT
  18. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  20. License for more details.
  21. You should have received a copy of the GNU Lesser General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #include "testutil.hpp"
  25. #include "testutil_monitoring.hpp"
  26. #include "testutil_unity.hpp"
  27. #include <stdlib.h>
  28. #include <string.h>
  29. // This test requires a KRB5 environment with the following
  30. // service principal (substitute your host.domain and REALM):
  31. //
  32. // zmqtest2/host.domain@REALM (host.domain should be host running test)
  33. //
  34. // Export keys for this principal to a keytab file and set the environment
  35. // variables KRB5_KTNAME and KRB5_CLIENT_KTNAME to FILE:/path/to/your/keytab.
  36. // The test will use it both for client and server roles.
  37. //
  38. // The test is derived in large part from test_security_curve.cpp
  39. const char *name = "zmqtest2";
  40. static volatile int zap_deny_all = 0;
  41. // --------------------------------------------------------------------------
  42. // This methods receives and validates ZAP requestes (allowing or denying
  43. // each client connection).
  44. // N.B. on failure, each crypto type in keytab will be tried
  45. static void zap_handler (void *handler_)
  46. {
  47. // Process ZAP requests forever
  48. while (true) {
  49. char *version = s_recv (handler_);
  50. if (!version)
  51. break; // Terminating
  52. char *sequence = s_recv (handler_);
  53. char *domain = s_recv (handler_);
  54. char *address = s_recv (handler_);
  55. char *routing_id = s_recv (handler_);
  56. char *mechanism = s_recv (handler_);
  57. char *principal = s_recv (handler_);
  58. TEST_ASSERT_EQUAL_STRING ("1.0", version);
  59. TEST_ASSERT_EQUAL_STRING ("GSSAPI", mechanism);
  60. send_string_expect_success (handler_, version, ZMQ_SNDMORE);
  61. send_string_expect_success (handler_, sequence, ZMQ_SNDMORE);
  62. if (!zap_deny_all) {
  63. send_string_expect_success (handler_, "200", ZMQ_SNDMORE);
  64. send_string_expect_success (handler_, "OK", ZMQ_SNDMORE);
  65. send_string_expect_success (handler_, "anonymous", ZMQ_SNDMORE);
  66. send_string_expect_success (handler_, "", 0);
  67. //fprintf (stderr, "ALLOW %s\n", principal);
  68. } else {
  69. send_string_expect_success (handler_, "400", ZMQ_SNDMORE);
  70. send_string_expect_success (handler_, "Denied", ZMQ_SNDMORE);
  71. send_string_expect_success (handler_, "", ZMQ_SNDMORE);
  72. send_string_expect_success (handler_, "", 0);
  73. //fprintf (stderr, "DENY %s\n", principal);
  74. }
  75. free (version);
  76. free (sequence);
  77. free (domain);
  78. free (address);
  79. free (routing_id);
  80. free (mechanism);
  81. free (principal);
  82. }
  83. zmq_close (handler_);
  84. }
  85. static char my_endpoint[MAX_SOCKET_STRING];
  86. static void *zap_thread;
  87. static void *server;
  88. static void *server_mon;
  89. void check_krb_available ()
  90. {
  91. if (!getenv ("KRB5_KTNAME") || !getenv ("KRB5_CLIENT_KTNAME")) {
  92. TEST_IGNORE_MESSAGE ("KRB5 environment unavailable, skipping test");
  93. }
  94. }
  95. void setUp ()
  96. {
  97. setup_test_context ();
  98. zap_thread = 0;
  99. server = NULL;
  100. server_mon = NULL;
  101. check_krb_available ();
  102. // Spawn ZAP handler
  103. // We create and bind ZAP socket in main thread to avoid case
  104. // where child thread does not start up fast enough.
  105. void *handler = zmq_socket (get_test_context (), ZMQ_REP);
  106. TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (handler, "inproc://zeromq.zap.01"));
  107. zap_thread = zmq_threadstart (&zap_handler, handler);
  108. // Server socket will accept connections
  109. server = test_context_socket (ZMQ_DEALER);
  110. int as_server = 1;
  111. TEST_ASSERT_SUCCESS_ERRNO (
  112. zmq_setsockopt (server, ZMQ_GSSAPI_SERVER, &as_server, sizeof (int)));
  113. TEST_ASSERT_SUCCESS_ERRNO (
  114. zmq_setsockopt (server, ZMQ_GSSAPI_PRINCIPAL, name, strlen (name) + 1));
  115. int name_type = ZMQ_GSSAPI_NT_HOSTBASED;
  116. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
  117. server, ZMQ_GSSAPI_PRINCIPAL_NAMETYPE, &name_type, sizeof (name_type)));
  118. bind_loopback_ipv4 (server, my_endpoint, sizeof my_endpoint);
  119. // Monitor handshake events on the server
  120. TEST_ASSERT_SUCCESS_ERRNO (zmq_socket_monitor (
  121. server, "inproc://monitor-server",
  122. ZMQ_EVENT_HANDSHAKE_SUCCEEDED | ZMQ_EVENT_HANDSHAKE_FAILED_AUTH
  123. | ZMQ_EVENT_HANDSHAKE_FAILED_PROTOCOL));
  124. // Create socket for collecting monitor events
  125. server_mon = test_context_socket (ZMQ_PAIR);
  126. // Connect it to the inproc endpoints so they'll get events
  127. TEST_ASSERT_SUCCESS_ERRNO (
  128. zmq_connect (server_mon, "inproc://monitor-server"));
  129. }
  130. void tearDown ()
  131. {
  132. // Shutdown
  133. if (server_mon)
  134. test_context_socket_close_zero_linger (server_mon);
  135. if (server)
  136. test_context_socket_close (server);
  137. teardown_test_context ();
  138. // Wait until ZAP handler terminates
  139. if (zap_thread)
  140. zmq_threadclose (zap_thread);
  141. }
  142. void test_valid_creds ()
  143. {
  144. void *client = test_context_socket (ZMQ_DEALER);
  145. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
  146. client, ZMQ_GSSAPI_SERVICE_PRINCIPAL, name, strlen (name) + 1));
  147. TEST_ASSERT_SUCCESS_ERRNO (
  148. zmq_setsockopt (client, ZMQ_GSSAPI_PRINCIPAL, name, strlen (name) + 1));
  149. int name_type = ZMQ_GSSAPI_NT_HOSTBASED;
  150. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
  151. client, ZMQ_GSSAPI_PRINCIPAL_NAMETYPE, &name_type, sizeof (name_type)));
  152. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client, my_endpoint));
  153. bounce (server, client);
  154. test_context_socket_close (client);
  155. int event = get_monitor_event (server_mon, NULL, NULL);
  156. TEST_ASSERT_EQUAL_INT (ZMQ_EVENT_HANDSHAKE_SUCCEEDED, event);
  157. }
  158. // Check security with valid but unauthorized credentials
  159. // Note: ZAP may see multiple requests - after a failure, client will
  160. // fall back to other crypto types for principal, if available.
  161. void test_unauth_creds ()
  162. {
  163. void *client = test_context_socket (ZMQ_DEALER);
  164. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
  165. client, ZMQ_GSSAPI_SERVICE_PRINCIPAL, name, strlen (name) + 1));
  166. TEST_ASSERT_SUCCESS_ERRNO (
  167. zmq_setsockopt (client, ZMQ_GSSAPI_PRINCIPAL, name, strlen (name) + 1));
  168. int name_type = ZMQ_GSSAPI_NT_HOSTBASED;
  169. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
  170. client, ZMQ_GSSAPI_PRINCIPAL_NAMETYPE, &name_type, sizeof (name_type)));
  171. zap_deny_all = 1;
  172. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client, my_endpoint));
  173. expect_bounce_fail (server, client);
  174. test_context_socket_close_zero_linger (client);
  175. int event = get_monitor_event (server_mon, NULL, NULL);
  176. TEST_ASSERT_EQUAL_INT (ZMQ_EVENT_HANDSHAKE_FAILED_AUTH, event);
  177. }
  178. // Check GSSAPI security with NULL client credentials
  179. // This must be caught by the gssapi_server class, not passed to ZAP
  180. void test_null_creds ()
  181. {
  182. void *client = test_context_socket (ZMQ_DEALER);
  183. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client, my_endpoint));
  184. expect_bounce_fail (server, client);
  185. test_context_socket_close_zero_linger (client);
  186. int error;
  187. int event = get_monitor_event (server_mon, &error, NULL);
  188. TEST_ASSERT_EQUAL_INT (ZMQ_EVENT_HANDSHAKE_FAILED_PROTOCOL, event);
  189. TEST_ASSERT_EQUAL_INT (ZMQ_PROTOCOL_ERROR_ZMTP_MECHANISM_MISMATCH, error);
  190. }
  191. // Check GSSAPI security with PLAIN client credentials
  192. // This must be caught by the curve_server class, not passed to ZAP
  193. void test_plain_creds ()
  194. {
  195. void *client = test_context_socket (ZMQ_DEALER);
  196. TEST_ASSERT_SUCCESS_ERRNO (
  197. zmq_setsockopt (client, ZMQ_PLAIN_USERNAME, "admin", 5));
  198. TEST_ASSERT_SUCCESS_ERRNO (
  199. zmq_setsockopt (client, ZMQ_PLAIN_PASSWORD, "password", 8));
  200. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client, my_endpoint));
  201. expect_bounce_fail (server, client);
  202. test_context_socket_close_zero_linger (client);
  203. }
  204. // Unauthenticated messages from a vanilla socket shouldn't be received
  205. void test_vanilla_socket ()
  206. {
  207. fd_t s = connect_socket (my_endpoint);
  208. // send anonymous ZMTP/1.0 greeting
  209. send (s, "\x01\x00", 2, 0);
  210. // send sneaky message that shouldn't be received
  211. send (s, "\x08\x00sneaky\0", 9, 0);
  212. int timeout = 250;
  213. zmq_setsockopt (server, ZMQ_RCVTIMEO, &timeout, sizeof (timeout));
  214. char *buf = s_recv (server);
  215. if (buf != NULL) {
  216. printf ("Received unauthenticated message: %s\n", buf);
  217. TEST_ASSERT_NULL (buf);
  218. }
  219. close (s);
  220. }
  221. int main (void)
  222. {
  223. // Avoid entanglements with user's credential cache
  224. setenv ("KRB5CCNAME", "MEMORY", 1);
  225. setup_test_environment ();
  226. UNITY_BEGIN ();
  227. RUN_TEST (test_valid_creds);
  228. RUN_TEST (test_null_creds);
  229. RUN_TEST (test_plain_creds);
  230. RUN_TEST (test_vanilla_socket);
  231. RUN_TEST (test_unauth_creds);
  232. return UNITY_END ();
  233. }