auth_x.c 17 KB

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