auth.c 5.5 KB

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