auth.c 7.6 KB

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