auth.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #include <linux/ceph/ceph_debug.h>
  2. #include <linux/module.h>
  3. #include <linux/err.h>
  4. #include <linux/slab.h>
  5. #include <linux/ceph/types.h>
  6. #include <linux/ceph/decode.h>
  7. #include <linux/ceph/libceph.h>
  8. #include <linux/ceph/messenger.h>
  9. #include "auth_none.h"
  10. #include "auth_x.h"
  11. /*
  12. * get protocol handler
  13. */
  14. static u32 supported_protocols[] = {
  15. CEPH_AUTH_NONE,
  16. CEPH_AUTH_CEPHX
  17. };
  18. static int ceph_auth_init_protocol(struct ceph_auth_client *ac, int protocol)
  19. {
  20. switch (protocol) {
  21. case CEPH_AUTH_NONE:
  22. return ceph_auth_none_init(ac);
  23. case CEPH_AUTH_CEPHX:
  24. return ceph_x_init(ac);
  25. default:
  26. return -ENOENT;
  27. }
  28. }
  29. /*
  30. * setup, teardown.
  31. */
  32. struct ceph_auth_client *ceph_auth_init(const char *name, const struct ceph_crypto_key *key)
  33. {
  34. struct ceph_auth_client *ac;
  35. int ret;
  36. dout("auth_init name '%s'\n", name);
  37. ret = -ENOMEM;
  38. ac = kzalloc(sizeof(*ac), GFP_NOFS);
  39. if (!ac)
  40. goto out;
  41. ac->negotiating = true;
  42. if (name)
  43. ac->name = name;
  44. else
  45. ac->name = CEPH_AUTH_NAME_DEFAULT;
  46. dout("auth_init name %s\n", ac->name);
  47. ac->key = key;
  48. return ac;
  49. out:
  50. return ERR_PTR(ret);
  51. }
  52. void ceph_auth_destroy(struct ceph_auth_client *ac)
  53. {
  54. dout("auth_destroy %p\n", ac);
  55. if (ac->ops)
  56. ac->ops->destroy(ac);
  57. kfree(ac);
  58. }
  59. /*
  60. * Reset occurs when reconnecting to the monitor.
  61. */
  62. void ceph_auth_reset(struct ceph_auth_client *ac)
  63. {
  64. dout("auth_reset %p\n", ac);
  65. if (ac->ops && !ac->negotiating)
  66. ac->ops->reset(ac);
  67. ac->negotiating = true;
  68. }
  69. int ceph_entity_name_encode(const char *name, void **p, void *end)
  70. {
  71. int len = strlen(name);
  72. if (*p + 2*sizeof(u32) + len > end)
  73. return -ERANGE;
  74. ceph_encode_32(p, CEPH_ENTITY_TYPE_CLIENT);
  75. ceph_encode_32(p, len);
  76. ceph_encode_copy(p, name, len);
  77. return 0;
  78. }
  79. /*
  80. * Initiate protocol negotiation with monitor. Include entity name
  81. * and list supported protocols.
  82. */
  83. int ceph_auth_build_hello(struct ceph_auth_client *ac, void *buf, size_t len)
  84. {
  85. struct ceph_mon_request_header *monhdr = buf;
  86. void *p = monhdr + 1, *end = buf + len, *lenp;
  87. int i, num;
  88. int ret;
  89. dout("auth_build_hello\n");
  90. monhdr->have_version = 0;
  91. monhdr->session_mon = cpu_to_le16(-1);
  92. monhdr->session_mon_tid = 0;
  93. ceph_encode_32(&p, 0); /* no protocol, yet */
  94. lenp = p;
  95. p += sizeof(u32);
  96. ceph_decode_need(&p, end, 1 + sizeof(u32), bad);
  97. ceph_encode_8(&p, 1);
  98. num = ARRAY_SIZE(supported_protocols);
  99. ceph_encode_32(&p, num);
  100. ceph_decode_need(&p, end, num * sizeof(u32), bad);
  101. for (i = 0; i < num; i++)
  102. ceph_encode_32(&p, supported_protocols[i]);
  103. ret = ceph_entity_name_encode(ac->name, &p, end);
  104. if (ret < 0)
  105. return ret;
  106. ceph_decode_need(&p, end, sizeof(u64), bad);
  107. ceph_encode_64(&p, ac->global_id);
  108. ceph_encode_32(&lenp, p - lenp - sizeof(u32));
  109. return p - buf;
  110. bad:
  111. return -ERANGE;
  112. }
  113. static int ceph_build_auth_request(struct ceph_auth_client *ac,
  114. void *msg_buf, size_t msg_len)
  115. {
  116. struct ceph_mon_request_header *monhdr = msg_buf;
  117. void *p = monhdr + 1;
  118. void *end = msg_buf + msg_len;
  119. int ret;
  120. monhdr->have_version = 0;
  121. monhdr->session_mon = cpu_to_le16(-1);
  122. monhdr->session_mon_tid = 0;
  123. ceph_encode_32(&p, ac->protocol);
  124. ret = ac->ops->build_request(ac, p + sizeof(u32), end);
  125. if (ret < 0) {
  126. pr_err("error %d building auth method %s request\n", ret,
  127. ac->ops->name);
  128. return ret;
  129. }
  130. dout(" built request %d bytes\n", ret);
  131. ceph_encode_32(&p, ret);
  132. return p + ret - msg_buf;
  133. }
  134. /*
  135. * Handle auth message from monitor.
  136. */
  137. int ceph_handle_auth_reply(struct ceph_auth_client *ac,
  138. void *buf, size_t len,
  139. void *reply_buf, size_t reply_len)
  140. {
  141. void *p = buf;
  142. void *end = buf + len;
  143. int protocol;
  144. s32 result;
  145. u64 global_id;
  146. void *payload, *payload_end;
  147. int payload_len;
  148. char *result_msg;
  149. int result_msg_len;
  150. int ret = -EINVAL;
  151. dout("handle_auth_reply %p %p\n", p, end);
  152. ceph_decode_need(&p, end, sizeof(u32) * 3 + sizeof(u64), bad);
  153. protocol = ceph_decode_32(&p);
  154. result = ceph_decode_32(&p);
  155. global_id = ceph_decode_64(&p);
  156. payload_len = ceph_decode_32(&p);
  157. payload = p;
  158. p += payload_len;
  159. ceph_decode_need(&p, end, sizeof(u32), bad);
  160. result_msg_len = ceph_decode_32(&p);
  161. result_msg = p;
  162. p += result_msg_len;
  163. if (p != end)
  164. goto bad;
  165. dout(" result %d '%.*s' gid %llu len %d\n", result, result_msg_len,
  166. result_msg, global_id, payload_len);
  167. payload_end = payload + payload_len;
  168. if (global_id && ac->global_id != global_id) {
  169. dout(" set global_id %lld -> %lld\n", ac->global_id, global_id);
  170. ac->global_id = global_id;
  171. }
  172. if (ac->negotiating) {
  173. /* server does not support our protocols? */
  174. if (!protocol && result < 0) {
  175. ret = result;
  176. goto out;
  177. }
  178. /* set up (new) protocol handler? */
  179. if (ac->protocol && ac->protocol != protocol) {
  180. ac->ops->destroy(ac);
  181. ac->protocol = 0;
  182. ac->ops = NULL;
  183. }
  184. if (ac->protocol != protocol) {
  185. ret = ceph_auth_init_protocol(ac, protocol);
  186. if (ret) {
  187. pr_err("error %d on auth protocol %d init\n",
  188. ret, protocol);
  189. goto out;
  190. }
  191. }
  192. ac->negotiating = false;
  193. }
  194. ret = ac->ops->handle_reply(ac, result, payload, payload_end);
  195. if (ret == -EAGAIN) {
  196. return ceph_build_auth_request(ac, reply_buf, reply_len);
  197. } else if (ret) {
  198. pr_err("auth method '%s' error %d\n", ac->ops->name, ret);
  199. return ret;
  200. }
  201. return 0;
  202. bad:
  203. pr_err("failed to decode auth msg\n");
  204. out:
  205. return ret;
  206. }
  207. int ceph_build_auth(struct ceph_auth_client *ac,
  208. void *msg_buf, size_t msg_len)
  209. {
  210. if (!ac->protocol)
  211. return ceph_auth_build_hello(ac, msg_buf, msg_len);
  212. BUG_ON(!ac->ops);
  213. if (ac->ops->should_authenticate(ac))
  214. return ceph_build_auth_request(ac, msg_buf, msg_len);
  215. return 0;
  216. }
  217. int ceph_auth_is_authenticated(struct ceph_auth_client *ac)
  218. {
  219. if (!ac->ops)
  220. return 0;
  221. return ac->ops->is_authenticated(ac);
  222. }