auth.c 7.9 KB

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