auth_x.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. #include "ceph_debug.h"
  2. #include <linux/err.h>
  3. #include <linux/module.h>
  4. #include <linux/random.h>
  5. #include <linux/slab.h>
  6. #include "auth_x.h"
  7. #include "auth_x_protocol.h"
  8. #include "crypto.h"
  9. #include "auth.h"
  10. #include "decode.h"
  11. struct kmem_cache *ceph_x_ticketbuf_cachep;
  12. #define TEMP_TICKET_BUF_LEN 256
  13. static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed);
  14. static int ceph_x_is_authenticated(struct ceph_auth_client *ac)
  15. {
  16. struct ceph_x_info *xi = ac->private;
  17. int need;
  18. ceph_x_validate_tickets(ac, &need);
  19. dout("ceph_x_is_authenticated want=%d need=%d have=%d\n",
  20. ac->want_keys, need, xi->have_keys);
  21. return (ac->want_keys & xi->have_keys) == ac->want_keys;
  22. }
  23. static int ceph_x_encrypt_buflen(int ilen)
  24. {
  25. return sizeof(struct ceph_x_encrypt_header) + ilen + 16 +
  26. sizeof(u32);
  27. }
  28. static int ceph_x_encrypt(struct ceph_crypto_key *secret,
  29. void *ibuf, int ilen, void *obuf, size_t olen)
  30. {
  31. struct ceph_x_encrypt_header head = {
  32. .struct_v = 1,
  33. .magic = cpu_to_le64(CEPHX_ENC_MAGIC)
  34. };
  35. size_t len = olen - sizeof(u32);
  36. int ret;
  37. ret = ceph_encrypt2(secret, obuf + sizeof(u32), &len,
  38. &head, sizeof(head), ibuf, ilen);
  39. if (ret)
  40. return ret;
  41. ceph_encode_32(&obuf, len);
  42. return len + sizeof(u32);
  43. }
  44. static int ceph_x_decrypt(struct ceph_crypto_key *secret,
  45. void **p, void *end, void *obuf, size_t olen)
  46. {
  47. struct ceph_x_encrypt_header head;
  48. size_t head_len = sizeof(head);
  49. int len, ret;
  50. len = ceph_decode_32(p);
  51. if (*p + len > end)
  52. return -EINVAL;
  53. dout("ceph_x_decrypt len %d\n", len);
  54. ret = ceph_decrypt2(secret, &head, &head_len, obuf, &olen,
  55. *p, len);
  56. if (ret)
  57. return ret;
  58. if (head.struct_v != 1 || le64_to_cpu(head.magic) != CEPHX_ENC_MAGIC)
  59. return -EPERM;
  60. *p += len;
  61. return olen;
  62. }
  63. /*
  64. * get existing (or insert new) ticket handler
  65. */
  66. struct ceph_x_ticket_handler *get_ticket_handler(struct ceph_auth_client *ac,
  67. int service)
  68. {
  69. struct ceph_x_ticket_handler *th;
  70. struct ceph_x_info *xi = ac->private;
  71. struct rb_node *parent = NULL, **p = &xi->ticket_handlers.rb_node;
  72. while (*p) {
  73. parent = *p;
  74. th = rb_entry(parent, struct ceph_x_ticket_handler, node);
  75. if (service < th->service)
  76. p = &(*p)->rb_left;
  77. else if (service > th->service)
  78. p = &(*p)->rb_right;
  79. else
  80. return th;
  81. }
  82. /* add it */
  83. th = kzalloc(sizeof(*th), GFP_NOFS);
  84. if (!th)
  85. return ERR_PTR(-ENOMEM);
  86. th->service = service;
  87. rb_link_node(&th->node, parent, p);
  88. rb_insert_color(&th->node, &xi->ticket_handlers);
  89. return th;
  90. }
  91. static void remove_ticket_handler(struct ceph_auth_client *ac,
  92. struct ceph_x_ticket_handler *th)
  93. {
  94. struct ceph_x_info *xi = ac->private;
  95. dout("remove_ticket_handler %p %d\n", th, th->service);
  96. rb_erase(&th->node, &xi->ticket_handlers);
  97. ceph_crypto_key_destroy(&th->session_key);
  98. if (th->ticket_blob)
  99. ceph_buffer_put(th->ticket_blob);
  100. kfree(th);
  101. }
  102. static int ceph_x_proc_ticket_reply(struct ceph_auth_client *ac,
  103. struct ceph_crypto_key *secret,
  104. void *buf, void *end)
  105. {
  106. struct ceph_x_info *xi = ac->private;
  107. int num;
  108. void *p = buf;
  109. int ret;
  110. char *dbuf;
  111. char *ticket_buf;
  112. u8 struct_v;
  113. dbuf = kmem_cache_alloc(ceph_x_ticketbuf_cachep, GFP_NOFS | GFP_ATOMIC);
  114. if (!dbuf)
  115. return -ENOMEM;
  116. ret = -ENOMEM;
  117. ticket_buf = kmem_cache_alloc(ceph_x_ticketbuf_cachep,
  118. GFP_NOFS | GFP_ATOMIC);
  119. if (!ticket_buf)
  120. goto out_dbuf;
  121. ceph_decode_need(&p, end, 1 + sizeof(u32), bad);
  122. struct_v = ceph_decode_8(&p);
  123. if (struct_v != 1)
  124. goto bad;
  125. num = ceph_decode_32(&p);
  126. dout("%d tickets\n", num);
  127. while (num--) {
  128. int type;
  129. u8 struct_v;
  130. struct ceph_x_ticket_handler *th;
  131. void *dp, *dend;
  132. int dlen;
  133. char is_enc;
  134. struct timespec validity;
  135. struct ceph_crypto_key old_key;
  136. void *tp, *tpend;
  137. struct ceph_timespec new_validity;
  138. struct ceph_crypto_key new_session_key;
  139. struct ceph_buffer *new_ticket_blob;
  140. unsigned long new_expires, new_renew_after;
  141. u64 new_secret_id;
  142. ceph_decode_need(&p, end, sizeof(u32) + 1, bad);
  143. type = ceph_decode_32(&p);
  144. dout(" ticket type %d %s\n", type, ceph_entity_type_name(type));
  145. struct_v = ceph_decode_8(&p);
  146. if (struct_v != 1)
  147. goto bad;
  148. th = get_ticket_handler(ac, type);
  149. if (IS_ERR(th)) {
  150. ret = PTR_ERR(th);
  151. goto out;
  152. }
  153. /* blob for me */
  154. dlen = ceph_x_decrypt(secret, &p, end, dbuf,
  155. TEMP_TICKET_BUF_LEN);
  156. if (dlen <= 0) {
  157. ret = dlen;
  158. goto out;
  159. }
  160. dout(" decrypted %d bytes\n", dlen);
  161. dend = dbuf + dlen;
  162. dp = dbuf;
  163. struct_v = ceph_decode_8(&dp);
  164. if (struct_v != 1)
  165. goto bad;
  166. memcpy(&old_key, &th->session_key, sizeof(old_key));
  167. ret = ceph_crypto_key_decode(&new_session_key, &dp, dend);
  168. if (ret)
  169. goto out;
  170. ceph_decode_copy(&dp, &new_validity, sizeof(new_validity));
  171. ceph_decode_timespec(&validity, &new_validity);
  172. new_expires = get_seconds() + validity.tv_sec;
  173. new_renew_after = new_expires - (validity.tv_sec / 4);
  174. dout(" expires=%lu renew_after=%lu\n", new_expires,
  175. new_renew_after);
  176. /* ticket blob for service */
  177. ceph_decode_8_safe(&p, end, is_enc, bad);
  178. tp = ticket_buf;
  179. if (is_enc) {
  180. /* encrypted */
  181. dout(" encrypted ticket\n");
  182. dlen = ceph_x_decrypt(&old_key, &p, end, ticket_buf,
  183. TEMP_TICKET_BUF_LEN);
  184. if (dlen < 0) {
  185. ret = dlen;
  186. goto out;
  187. }
  188. dlen = ceph_decode_32(&tp);
  189. } else {
  190. /* unencrypted */
  191. ceph_decode_32_safe(&p, end, dlen, bad);
  192. ceph_decode_need(&p, end, dlen, bad);
  193. ceph_decode_copy(&p, ticket_buf, dlen);
  194. }
  195. tpend = tp + dlen;
  196. dout(" ticket blob is %d bytes\n", dlen);
  197. ceph_decode_need(&tp, tpend, 1 + sizeof(u64), bad);
  198. struct_v = ceph_decode_8(&tp);
  199. new_secret_id = ceph_decode_64(&tp);
  200. ret = ceph_decode_buffer(&new_ticket_blob, &tp, tpend);
  201. if (ret)
  202. goto out;
  203. /* all is well, update our ticket */
  204. ceph_crypto_key_destroy(&th->session_key);
  205. if (th->ticket_blob)
  206. ceph_buffer_put(th->ticket_blob);
  207. th->session_key = new_session_key;
  208. th->ticket_blob = new_ticket_blob;
  209. th->validity = new_validity;
  210. th->secret_id = new_secret_id;
  211. th->expires = new_expires;
  212. th->renew_after = new_renew_after;
  213. dout(" got ticket service %d (%s) secret_id %lld len %d\n",
  214. type, ceph_entity_type_name(type), th->secret_id,
  215. (int)th->ticket_blob->vec.iov_len);
  216. xi->have_keys |= th->service;
  217. }
  218. ret = 0;
  219. out:
  220. kmem_cache_free(ceph_x_ticketbuf_cachep, ticket_buf);
  221. out_dbuf:
  222. kmem_cache_free(ceph_x_ticketbuf_cachep, dbuf);
  223. return ret;
  224. bad:
  225. ret = -EINVAL;
  226. goto out;
  227. }
  228. static int ceph_x_build_authorizer(struct ceph_auth_client *ac,
  229. struct ceph_x_ticket_handler *th,
  230. struct ceph_x_authorizer *au)
  231. {
  232. int maxlen;
  233. struct ceph_x_authorize_a *msg_a;
  234. struct ceph_x_authorize_b msg_b;
  235. void *p, *end;
  236. int ret;
  237. int ticket_blob_len =
  238. (th->ticket_blob ? th->ticket_blob->vec.iov_len : 0);
  239. dout("build_authorizer for %s %p\n",
  240. ceph_entity_type_name(th->service), au);
  241. maxlen = sizeof(*msg_a) + sizeof(msg_b) +
  242. ceph_x_encrypt_buflen(ticket_blob_len);
  243. dout(" need len %d\n", maxlen);
  244. if (au->buf && au->buf->alloc_len < maxlen) {
  245. ceph_buffer_put(au->buf);
  246. au->buf = NULL;
  247. }
  248. if (!au->buf) {
  249. au->buf = ceph_buffer_new(maxlen, GFP_NOFS);
  250. if (!au->buf)
  251. return -ENOMEM;
  252. }
  253. au->service = th->service;
  254. msg_a = au->buf->vec.iov_base;
  255. msg_a->struct_v = 1;
  256. msg_a->global_id = cpu_to_le64(ac->global_id);
  257. msg_a->service_id = cpu_to_le32(th->service);
  258. msg_a->ticket_blob.struct_v = 1;
  259. msg_a->ticket_blob.secret_id = cpu_to_le64(th->secret_id);
  260. msg_a->ticket_blob.blob_len = cpu_to_le32(ticket_blob_len);
  261. if (ticket_blob_len) {
  262. memcpy(msg_a->ticket_blob.blob, th->ticket_blob->vec.iov_base,
  263. th->ticket_blob->vec.iov_len);
  264. }
  265. dout(" th %p secret_id %lld %lld\n", th, th->secret_id,
  266. le64_to_cpu(msg_a->ticket_blob.secret_id));
  267. p = msg_a + 1;
  268. p += ticket_blob_len;
  269. end = au->buf->vec.iov_base + au->buf->vec.iov_len;
  270. get_random_bytes(&au->nonce, sizeof(au->nonce));
  271. msg_b.struct_v = 1;
  272. msg_b.nonce = cpu_to_le64(au->nonce);
  273. ret = ceph_x_encrypt(&th->session_key, &msg_b, sizeof(msg_b),
  274. p, end - p);
  275. if (ret < 0)
  276. goto out_buf;
  277. p += ret;
  278. au->buf->vec.iov_len = p - au->buf->vec.iov_base;
  279. dout(" built authorizer nonce %llx len %d\n", au->nonce,
  280. (int)au->buf->vec.iov_len);
  281. BUG_ON(au->buf->vec.iov_len > maxlen);
  282. return 0;
  283. out_buf:
  284. ceph_buffer_put(au->buf);
  285. au->buf = NULL;
  286. return ret;
  287. }
  288. static int ceph_x_encode_ticket(struct ceph_x_ticket_handler *th,
  289. void **p, void *end)
  290. {
  291. ceph_decode_need(p, end, 1 + sizeof(u64), bad);
  292. ceph_encode_8(p, 1);
  293. ceph_encode_64(p, th->secret_id);
  294. if (th->ticket_blob) {
  295. const char *buf = th->ticket_blob->vec.iov_base;
  296. u32 len = th->ticket_blob->vec.iov_len;
  297. ceph_encode_32_safe(p, end, len, bad);
  298. ceph_encode_copy_safe(p, end, buf, len, bad);
  299. } else {
  300. ceph_encode_32_safe(p, end, 0, bad);
  301. }
  302. return 0;
  303. bad:
  304. return -ERANGE;
  305. }
  306. static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed)
  307. {
  308. int want = ac->want_keys;
  309. struct ceph_x_info *xi = ac->private;
  310. int service;
  311. *pneed = ac->want_keys & ~(xi->have_keys);
  312. for (service = 1; service <= want; service <<= 1) {
  313. struct ceph_x_ticket_handler *th;
  314. if (!(ac->want_keys & service))
  315. continue;
  316. if (*pneed & service)
  317. continue;
  318. th = get_ticket_handler(ac, service);
  319. if (!th) {
  320. *pneed |= service;
  321. continue;
  322. }
  323. if (get_seconds() >= th->renew_after)
  324. *pneed |= service;
  325. if (get_seconds() >= th->expires)
  326. xi->have_keys &= ~service;
  327. }
  328. }
  329. static int ceph_x_build_request(struct ceph_auth_client *ac,
  330. void *buf, void *end)
  331. {
  332. struct ceph_x_info *xi = ac->private;
  333. int need;
  334. struct ceph_x_request_header *head = buf;
  335. int ret;
  336. struct ceph_x_ticket_handler *th =
  337. get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
  338. ceph_x_validate_tickets(ac, &need);
  339. dout("build_request want %x have %x need %x\n",
  340. ac->want_keys, xi->have_keys, need);
  341. if (need & CEPH_ENTITY_TYPE_AUTH) {
  342. struct ceph_x_authenticate *auth = (void *)(head + 1);
  343. void *p = auth + 1;
  344. struct ceph_x_challenge_blob tmp;
  345. char tmp_enc[40];
  346. u64 *u;
  347. if (p > end)
  348. return -ERANGE;
  349. dout(" get_auth_session_key\n");
  350. head->op = cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY);
  351. /* encrypt and hash */
  352. get_random_bytes(&auth->client_challenge, sizeof(u64));
  353. tmp.client_challenge = auth->client_challenge;
  354. tmp.server_challenge = cpu_to_le64(xi->server_challenge);
  355. ret = ceph_x_encrypt(&xi->secret, &tmp, sizeof(tmp),
  356. tmp_enc, sizeof(tmp_enc));
  357. if (ret < 0)
  358. return ret;
  359. auth->struct_v = 1;
  360. auth->key = 0;
  361. for (u = (u64 *)tmp_enc; u + 1 <= (u64 *)(tmp_enc + ret); u++)
  362. auth->key ^= *u;
  363. dout(" server_challenge %llx client_challenge %llx key %llx\n",
  364. xi->server_challenge, le64_to_cpu(auth->client_challenge),
  365. le64_to_cpu(auth->key));
  366. /* now encode the old ticket if exists */
  367. ret = ceph_x_encode_ticket(th, &p, end);
  368. if (ret < 0)
  369. return ret;
  370. return p - buf;
  371. }
  372. if (need) {
  373. void *p = head + 1;
  374. struct ceph_x_service_ticket_request *req;
  375. if (p > end)
  376. return -ERANGE;
  377. head->op = cpu_to_le16(CEPHX_GET_PRINCIPAL_SESSION_KEY);
  378. BUG_ON(!th);
  379. ret = ceph_x_build_authorizer(ac, th, &xi->auth_authorizer);
  380. if (ret)
  381. return ret;
  382. ceph_encode_copy(&p, xi->auth_authorizer.buf->vec.iov_base,
  383. xi->auth_authorizer.buf->vec.iov_len);
  384. req = p;
  385. req->keys = cpu_to_le32(need);
  386. p += sizeof(*req);
  387. return p - buf;
  388. }
  389. return 0;
  390. }
  391. static int ceph_x_handle_reply(struct ceph_auth_client *ac, int result,
  392. void *buf, void *end)
  393. {
  394. struct ceph_x_info *xi = ac->private;
  395. struct ceph_x_reply_header *head = buf;
  396. struct ceph_x_ticket_handler *th;
  397. int len = end - buf;
  398. int op;
  399. int ret;
  400. if (result)
  401. return result; /* XXX hmm? */
  402. if (xi->starting) {
  403. /* it's a hello */
  404. struct ceph_x_server_challenge *sc = buf;
  405. if (len != sizeof(*sc))
  406. return -EINVAL;
  407. xi->server_challenge = le64_to_cpu(sc->server_challenge);
  408. dout("handle_reply got server challenge %llx\n",
  409. xi->server_challenge);
  410. xi->starting = false;
  411. xi->have_keys &= ~CEPH_ENTITY_TYPE_AUTH;
  412. return -EAGAIN;
  413. }
  414. op = le32_to_cpu(head->op);
  415. result = le32_to_cpu(head->result);
  416. dout("handle_reply op %d result %d\n", op, result);
  417. switch (op) {
  418. case CEPHX_GET_AUTH_SESSION_KEY:
  419. /* verify auth key */
  420. ret = ceph_x_proc_ticket_reply(ac, &xi->secret,
  421. buf + sizeof(*head), end);
  422. break;
  423. case CEPHX_GET_PRINCIPAL_SESSION_KEY:
  424. th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
  425. BUG_ON(!th);
  426. ret = ceph_x_proc_ticket_reply(ac, &th->session_key,
  427. buf + sizeof(*head), end);
  428. break;
  429. default:
  430. return -EINVAL;
  431. }
  432. if (ret)
  433. return ret;
  434. if (ac->want_keys == xi->have_keys)
  435. return 0;
  436. return -EAGAIN;
  437. }
  438. static int ceph_x_create_authorizer(
  439. struct ceph_auth_client *ac, int peer_type,
  440. struct ceph_authorizer **a,
  441. void **buf, size_t *len,
  442. void **reply_buf, size_t *reply_len)
  443. {
  444. struct ceph_x_authorizer *au;
  445. struct ceph_x_ticket_handler *th;
  446. int ret;
  447. th = get_ticket_handler(ac, peer_type);
  448. if (IS_ERR(th))
  449. return PTR_ERR(th);
  450. au = kzalloc(sizeof(*au), GFP_NOFS);
  451. if (!au)
  452. return -ENOMEM;
  453. ret = ceph_x_build_authorizer(ac, th, au);
  454. if (ret) {
  455. kfree(au);
  456. return ret;
  457. }
  458. *a = (struct ceph_authorizer *)au;
  459. *buf = au->buf->vec.iov_base;
  460. *len = au->buf->vec.iov_len;
  461. *reply_buf = au->reply_buf;
  462. *reply_len = sizeof(au->reply_buf);
  463. return 0;
  464. }
  465. static int ceph_x_verify_authorizer_reply(struct ceph_auth_client *ac,
  466. struct ceph_authorizer *a, size_t len)
  467. {
  468. struct ceph_x_authorizer *au = (void *)a;
  469. struct ceph_x_ticket_handler *th;
  470. int ret = 0;
  471. struct ceph_x_authorize_reply reply;
  472. void *p = au->reply_buf;
  473. void *end = p + sizeof(au->reply_buf);
  474. th = get_ticket_handler(ac, au->service);
  475. if (!th)
  476. return -EIO; /* hrm! */
  477. ret = ceph_x_decrypt(&th->session_key, &p, end, &reply, sizeof(reply));
  478. if (ret < 0)
  479. return ret;
  480. if (ret != sizeof(reply))
  481. return -EPERM;
  482. if (au->nonce + 1 != le64_to_cpu(reply.nonce_plus_one))
  483. ret = -EPERM;
  484. else
  485. ret = 0;
  486. dout("verify_authorizer_reply nonce %llx got %llx ret %d\n",
  487. au->nonce, le64_to_cpu(reply.nonce_plus_one), ret);
  488. return ret;
  489. }
  490. static void ceph_x_destroy_authorizer(struct ceph_auth_client *ac,
  491. struct ceph_authorizer *a)
  492. {
  493. struct ceph_x_authorizer *au = (void *)a;
  494. ceph_buffer_put(au->buf);
  495. kfree(au);
  496. }
  497. static void ceph_x_reset(struct ceph_auth_client *ac)
  498. {
  499. struct ceph_x_info *xi = ac->private;
  500. dout("reset\n");
  501. xi->starting = true;
  502. xi->server_challenge = 0;
  503. }
  504. static void ceph_x_destroy(struct ceph_auth_client *ac)
  505. {
  506. struct ceph_x_info *xi = ac->private;
  507. struct rb_node *p;
  508. dout("ceph_x_destroy %p\n", ac);
  509. ceph_crypto_key_destroy(&xi->secret);
  510. while ((p = rb_first(&xi->ticket_handlers)) != NULL) {
  511. struct ceph_x_ticket_handler *th =
  512. rb_entry(p, struct ceph_x_ticket_handler, node);
  513. remove_ticket_handler(ac, th);
  514. }
  515. kmem_cache_destroy(ceph_x_ticketbuf_cachep);
  516. kfree(ac->private);
  517. ac->private = NULL;
  518. }
  519. static void ceph_x_invalidate_authorizer(struct ceph_auth_client *ac,
  520. int peer_type)
  521. {
  522. struct ceph_x_ticket_handler *th;
  523. th = get_ticket_handler(ac, peer_type);
  524. if (th && !IS_ERR(th))
  525. remove_ticket_handler(ac, th);
  526. }
  527. static const struct ceph_auth_client_ops ceph_x_ops = {
  528. .is_authenticated = ceph_x_is_authenticated,
  529. .build_request = ceph_x_build_request,
  530. .handle_reply = ceph_x_handle_reply,
  531. .create_authorizer = ceph_x_create_authorizer,
  532. .verify_authorizer_reply = ceph_x_verify_authorizer_reply,
  533. .destroy_authorizer = ceph_x_destroy_authorizer,
  534. .invalidate_authorizer = ceph_x_invalidate_authorizer,
  535. .reset = ceph_x_reset,
  536. .destroy = ceph_x_destroy,
  537. };
  538. int ceph_x_init(struct ceph_auth_client *ac)
  539. {
  540. struct ceph_x_info *xi;
  541. int ret;
  542. dout("ceph_x_init %p\n", ac);
  543. xi = kzalloc(sizeof(*xi), GFP_NOFS);
  544. if (!xi)
  545. return -ENOMEM;
  546. ret = -ENOMEM;
  547. ceph_x_ticketbuf_cachep = kmem_cache_create("ceph_x_ticketbuf",
  548. TEMP_TICKET_BUF_LEN, 8,
  549. (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD),
  550. NULL);
  551. if (!ceph_x_ticketbuf_cachep)
  552. goto done_nomem;
  553. ret = -EINVAL;
  554. if (!ac->secret) {
  555. pr_err("no secret set (for auth_x protocol)\n");
  556. goto done_nomem;
  557. }
  558. ret = ceph_crypto_key_unarmor(&xi->secret, ac->secret);
  559. if (ret)
  560. goto done_nomem;
  561. xi->starting = true;
  562. xi->ticket_handlers = RB_ROOT;
  563. ac->protocol = CEPH_AUTH_CEPHX;
  564. ac->private = xi;
  565. ac->ops = &ceph_x_ops;
  566. return 0;
  567. done_nomem:
  568. kfree(xi);
  569. if (ceph_x_ticketbuf_cachep)
  570. kmem_cache_destroy(ceph_x_ticketbuf_cachep);
  571. return ret;
  572. }