auth_x.c 17 KB

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