auth.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #include "ceph_debug.h"
  2. #include <linux/module.h>
  3. #include <linux/slab.h>
  4. #include <linux/err.h>
  5. #include "types.h"
  6. #include "auth_none.h"
  7. #include "auth_x.h"
  8. #include "decode.h"
  9. #include "super.h"
  10. #include "messenger.h"
  11. /*
  12. * get protocol handler
  13. */
  14. static u32 supported_protocols[] = {
  15. CEPH_AUTH_NONE,
  16. CEPH_AUTH_CEPHX
  17. };
  18. 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 char *secret)
  33. {
  34. struct ceph_auth_client *ac;
  35. int ret;
  36. dout("auth_init name '%s' secret '%s'\n", name, secret);
  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 secret %s\n", ac->name, secret);
  47. ac->secret = secret;
  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. 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 request\n", ret);
  127. return ret;
  128. }
  129. dout(" built request %d bytes\n", ret);
  130. ceph_encode_32(&p, ret);
  131. return p + ret - msg_buf;
  132. }
  133. /*
  134. * Handle auth message from monitor.
  135. */
  136. int ceph_handle_auth_reply(struct ceph_auth_client *ac,
  137. void *buf, size_t len,
  138. void *reply_buf, size_t reply_len)
  139. {
  140. void *p = buf;
  141. void *end = buf + len;
  142. int protocol;
  143. s32 result;
  144. u64 global_id;
  145. void *payload, *payload_end;
  146. int payload_len;
  147. char *result_msg;
  148. int result_msg_len;
  149. int ret = -EINVAL;
  150. dout("handle_auth_reply %p %p\n", p, end);
  151. ceph_decode_need(&p, end, sizeof(u32) * 3 + sizeof(u64), bad);
  152. protocol = ceph_decode_32(&p);
  153. result = ceph_decode_32(&p);
  154. global_id = ceph_decode_64(&p);
  155. payload_len = ceph_decode_32(&p);
  156. payload = p;
  157. p += payload_len;
  158. ceph_decode_need(&p, end, sizeof(u32), bad);
  159. result_msg_len = ceph_decode_32(&p);
  160. result_msg = p;
  161. p += result_msg_len;
  162. if (p != end)
  163. goto bad;
  164. dout(" result %d '%.*s' gid %llu len %d\n", result, result_msg_len,
  165. result_msg, global_id, payload_len);
  166. payload_end = payload + payload_len;
  167. if (global_id && ac->global_id != global_id) {
  168. dout(" set global_id %lld -> %lld\n", ac->global_id, global_id);
  169. ac->global_id = global_id;
  170. }
  171. if (ac->negotiating) {
  172. /* server does not support our protocols? */
  173. if (!protocol && result < 0) {
  174. ret = result;
  175. goto out;
  176. }
  177. /* set up (new) protocol handler? */
  178. if (ac->protocol && ac->protocol != protocol) {
  179. ac->ops->destroy(ac);
  180. ac->protocol = 0;
  181. ac->ops = NULL;
  182. }
  183. if (ac->protocol != protocol) {
  184. ret = ceph_auth_init_protocol(ac, protocol);
  185. if (ret) {
  186. pr_err("error %d on auth protocol %d init\n",
  187. ret, protocol);
  188. goto out;
  189. }
  190. }
  191. ac->negotiating = false;
  192. }
  193. ret = ac->ops->handle_reply(ac, result, payload, payload_end);
  194. if (ret == -EAGAIN) {
  195. return ceph_build_auth_request(ac, reply_buf, reply_len);
  196. } else if (ret) {
  197. pr_err("authentication error %d\n", ret);
  198. return ret;
  199. }
  200. return 0;
  201. bad:
  202. pr_err("failed to decode auth msg\n");
  203. out:
  204. return ret;
  205. }
  206. int ceph_build_auth(struct ceph_auth_client *ac,
  207. void *msg_buf, size_t msg_len)
  208. {
  209. if (!ac->protocol)
  210. return ceph_auth_build_hello(ac, msg_buf, msg_len);
  211. BUG_ON(!ac->ops);
  212. if (!ac->ops->is_authenticated(ac))
  213. return ceph_build_auth_request(ac, msg_buf, msg_len);
  214. return 0;
  215. }
  216. int ceph_auth_is_authenticated(struct ceph_auth_client *ac)
  217. {
  218. if (!ac->ops)
  219. return 0;
  220. return ac->ops->is_authenticated(ac);
  221. }