auth.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. mutex_init(&ac->mutex);
  42. ac->negotiating = true;
  43. if (name)
  44. ac->name = name;
  45. else
  46. ac->name = CEPH_AUTH_NAME_DEFAULT;
  47. dout("auth_init name %s\n", ac->name);
  48. ac->key = key;
  49. return ac;
  50. out:
  51. return ERR_PTR(ret);
  52. }
  53. void ceph_auth_destroy(struct ceph_auth_client *ac)
  54. {
  55. dout("auth_destroy %p\n", ac);
  56. if (ac->ops)
  57. ac->ops->destroy(ac);
  58. kfree(ac);
  59. }
  60. /*
  61. * Reset occurs when reconnecting to the monitor.
  62. */
  63. void ceph_auth_reset(struct ceph_auth_client *ac)
  64. {
  65. mutex_lock(&ac->mutex);
  66. dout("auth_reset %p\n", ac);
  67. if (ac->ops && !ac->negotiating)
  68. ac->ops->reset(ac);
  69. ac->negotiating = true;
  70. mutex_unlock(&ac->mutex);
  71. }
  72. /*
  73. * EntityName, not to be confused with entity_name_t
  74. */
  75. int ceph_auth_entity_name_encode(const char *name, void **p, void *end)
  76. {
  77. int len = strlen(name);
  78. if (*p + 2*sizeof(u32) + len > end)
  79. return -ERANGE;
  80. ceph_encode_32(p, CEPH_ENTITY_TYPE_CLIENT);
  81. ceph_encode_32(p, len);
  82. ceph_encode_copy(p, name, len);
  83. return 0;
  84. }
  85. /*
  86. * Initiate protocol negotiation with monitor. Include entity name
  87. * and list supported protocols.
  88. */
  89. int ceph_auth_build_hello(struct ceph_auth_client *ac, void *buf, size_t len)
  90. {
  91. struct ceph_mon_request_header *monhdr = buf;
  92. void *p = monhdr + 1, *end = buf + len, *lenp;
  93. int i, num;
  94. int ret;
  95. mutex_lock(&ac->mutex);
  96. dout("auth_build_hello\n");
  97. monhdr->have_version = 0;
  98. monhdr->session_mon = cpu_to_le16(-1);
  99. monhdr->session_mon_tid = 0;
  100. ceph_encode_32(&p, CEPH_AUTH_UNKNOWN); /* no protocol, yet */
  101. lenp = p;
  102. p += sizeof(u32);
  103. ceph_decode_need(&p, end, 1 + sizeof(u32), bad);
  104. ceph_encode_8(&p, 1);
  105. num = ARRAY_SIZE(supported_protocols);
  106. ceph_encode_32(&p, num);
  107. ceph_decode_need(&p, end, num * sizeof(u32), bad);
  108. for (i = 0; i < num; i++)
  109. ceph_encode_32(&p, supported_protocols[i]);
  110. ret = ceph_auth_entity_name_encode(ac->name, &p, end);
  111. if (ret < 0)
  112. goto out;
  113. ceph_decode_need(&p, end, sizeof(u64), bad);
  114. ceph_encode_64(&p, ac->global_id);
  115. ceph_encode_32(&lenp, p - lenp - sizeof(u32));
  116. ret = p - buf;
  117. out:
  118. mutex_unlock(&ac->mutex);
  119. return ret;
  120. bad:
  121. ret = -ERANGE;
  122. goto out;
  123. }
  124. static int ceph_build_auth_request(struct ceph_auth_client *ac,
  125. void *msg_buf, size_t msg_len)
  126. {
  127. struct ceph_mon_request_header *monhdr = msg_buf;
  128. void *p = monhdr + 1;
  129. void *end = msg_buf + msg_len;
  130. int ret;
  131. monhdr->have_version = 0;
  132. monhdr->session_mon = cpu_to_le16(-1);
  133. monhdr->session_mon_tid = 0;
  134. ceph_encode_32(&p, ac->protocol);
  135. ret = ac->ops->build_request(ac, p + sizeof(u32), end);
  136. if (ret < 0) {
  137. pr_err("error %d building auth method %s request\n", ret,
  138. ac->ops->name);
  139. goto out;
  140. }
  141. dout(" built request %d bytes\n", ret);
  142. ceph_encode_32(&p, ret);
  143. ret = p + ret - msg_buf;
  144. out:
  145. return ret;
  146. }
  147. /*
  148. * Handle auth message from monitor.
  149. */
  150. int ceph_handle_auth_reply(struct ceph_auth_client *ac,
  151. void *buf, size_t len,
  152. void *reply_buf, size_t reply_len)
  153. {
  154. void *p = buf;
  155. void *end = buf + len;
  156. int protocol;
  157. s32 result;
  158. u64 global_id;
  159. void *payload, *payload_end;
  160. int payload_len;
  161. char *result_msg;
  162. int result_msg_len;
  163. int ret = -EINVAL;
  164. mutex_lock(&ac->mutex);
  165. dout("handle_auth_reply %p %p\n", p, end);
  166. ceph_decode_need(&p, end, sizeof(u32) * 3 + sizeof(u64), bad);
  167. protocol = ceph_decode_32(&p);
  168. result = ceph_decode_32(&p);
  169. global_id = ceph_decode_64(&p);
  170. payload_len = ceph_decode_32(&p);
  171. payload = p;
  172. p += payload_len;
  173. ceph_decode_need(&p, end, sizeof(u32), bad);
  174. result_msg_len = ceph_decode_32(&p);
  175. result_msg = p;
  176. p += result_msg_len;
  177. if (p != end)
  178. goto bad;
  179. dout(" result %d '%.*s' gid %llu len %d\n", result, result_msg_len,
  180. result_msg, global_id, payload_len);
  181. payload_end = payload + payload_len;
  182. if (global_id && ac->global_id != global_id) {
  183. dout(" set global_id %lld -> %lld\n", ac->global_id, global_id);
  184. ac->global_id = global_id;
  185. }
  186. if (ac->negotiating) {
  187. /* server does not support our protocols? */
  188. if (!protocol && result < 0) {
  189. ret = result;
  190. goto out;
  191. }
  192. /* set up (new) protocol handler? */
  193. if (ac->protocol && ac->protocol != protocol) {
  194. ac->ops->destroy(ac);
  195. ac->protocol = 0;
  196. ac->ops = NULL;
  197. }
  198. if (ac->protocol != protocol) {
  199. ret = ceph_auth_init_protocol(ac, protocol);
  200. if (ret) {
  201. pr_err("error %d on auth protocol %d init\n",
  202. ret, protocol);
  203. goto out;
  204. }
  205. }
  206. ac->negotiating = false;
  207. }
  208. ret = ac->ops->handle_reply(ac, result, payload, payload_end);
  209. if (ret == -EAGAIN) {
  210. ret = ceph_build_auth_request(ac, reply_buf, reply_len);
  211. } else if (ret) {
  212. pr_err("auth method '%s' error %d\n", ac->ops->name, ret);
  213. }
  214. out:
  215. mutex_unlock(&ac->mutex);
  216. return ret;
  217. bad:
  218. pr_err("failed to decode auth msg\n");
  219. ret = -EINVAL;
  220. goto out;
  221. }
  222. int ceph_build_auth(struct ceph_auth_client *ac,
  223. void *msg_buf, size_t msg_len)
  224. {
  225. int ret = 0;
  226. mutex_lock(&ac->mutex);
  227. if (ac->ops->should_authenticate(ac))
  228. ret = ceph_build_auth_request(ac, msg_buf, msg_len);
  229. mutex_unlock(&ac->mutex);
  230. return ret;
  231. }
  232. int ceph_auth_is_authenticated(struct ceph_auth_client *ac)
  233. {
  234. int ret = 0;
  235. mutex_lock(&ac->mutex);
  236. if (ac->ops)
  237. ret = ac->ops->is_authenticated(ac);
  238. mutex_unlock(&ac->mutex);
  239. return ret;
  240. }
  241. EXPORT_SYMBOL(ceph_auth_is_authenticated);
  242. int ceph_auth_create_authorizer(struct ceph_auth_client *ac,
  243. int peer_type,
  244. struct ceph_auth_handshake *auth)
  245. {
  246. int ret = 0;
  247. mutex_lock(&ac->mutex);
  248. if (ac->ops && ac->ops->create_authorizer)
  249. ret = ac->ops->create_authorizer(ac, peer_type, auth);
  250. mutex_unlock(&ac->mutex);
  251. return ret;
  252. }
  253. EXPORT_SYMBOL(ceph_auth_create_authorizer);
  254. void ceph_auth_destroy_authorizer(struct ceph_authorizer *a)
  255. {
  256. a->destroy(a);
  257. }
  258. EXPORT_SYMBOL(ceph_auth_destroy_authorizer);
  259. int ceph_auth_update_authorizer(struct ceph_auth_client *ac,
  260. int peer_type,
  261. struct ceph_auth_handshake *a)
  262. {
  263. int ret = 0;
  264. mutex_lock(&ac->mutex);
  265. if (ac->ops && ac->ops->update_authorizer)
  266. ret = ac->ops->update_authorizer(ac, peer_type, a);
  267. mutex_unlock(&ac->mutex);
  268. return ret;
  269. }
  270. EXPORT_SYMBOL(ceph_auth_update_authorizer);
  271. int ceph_auth_verify_authorizer_reply(struct ceph_auth_client *ac,
  272. struct ceph_authorizer *a, size_t len)
  273. {
  274. int ret = 0;
  275. mutex_lock(&ac->mutex);
  276. if (ac->ops && ac->ops->verify_authorizer_reply)
  277. ret = ac->ops->verify_authorizer_reply(ac, a, len);
  278. mutex_unlock(&ac->mutex);
  279. return ret;
  280. }
  281. EXPORT_SYMBOL(ceph_auth_verify_authorizer_reply);
  282. void ceph_auth_invalidate_authorizer(struct ceph_auth_client *ac, int peer_type)
  283. {
  284. mutex_lock(&ac->mutex);
  285. if (ac->ops && ac->ops->invalidate_authorizer)
  286. ac->ops->invalidate_authorizer(ac, peer_type);
  287. mutex_unlock(&ac->mutex);
  288. }
  289. EXPORT_SYMBOL(ceph_auth_invalidate_authorizer);