blob: a16bf14eb027cd4e765320f98b0fb378b5a5e7c4 [file] [log] [blame]
Sage Weilec0994e2010-02-02 16:25:35 -08001
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002#include <linux/ceph/ceph_debug.h>
Sage Weilec0994e2010-02-02 16:25:35 -08003
4#include <linux/err.h>
5#include <linux/module.h>
6#include <linux/random.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
Sage Weilec0994e2010-02-02 16:25:35 -08008
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07009#include <linux/ceph/decode.h>
10#include <linux/ceph/auth.h>
11
12#include "crypto.h"
Sage Weilec0994e2010-02-02 16:25:35 -080013#include "auth_x.h"
14#include "auth_x_protocol.h"
Sage Weilec0994e2010-02-02 16:25:35 -080015
Sage Weilec0994e2010-02-02 16:25:35 -080016#define TEMP_TICKET_BUF_LEN 256
17
18static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed);
19
20static int ceph_x_is_authenticated(struct ceph_auth_client *ac)
21{
22 struct ceph_x_info *xi = ac->private;
23 int need;
24
25 ceph_x_validate_tickets(ac, &need);
26 dout("ceph_x_is_authenticated want=%d need=%d have=%d\n",
27 ac->want_keys, need, xi->have_keys);
28 return (ac->want_keys & xi->have_keys) == ac->want_keys;
29}
30
Sage Weila41359f2010-05-25 15:39:06 -070031static int ceph_x_should_authenticate(struct ceph_auth_client *ac)
32{
33 struct ceph_x_info *xi = ac->private;
34 int need;
35
36 ceph_x_validate_tickets(ac, &need);
37 dout("ceph_x_should_authenticate want=%d need=%d have=%d\n",
38 ac->want_keys, need, xi->have_keys);
39 return need != 0;
40}
41
Sage Weil807c86e2010-03-15 15:52:17 -070042static int ceph_x_encrypt_buflen(int ilen)
43{
44 return sizeof(struct ceph_x_encrypt_header) + ilen + 16 +
45 sizeof(u32);
46}
47
Sage Weilec0994e2010-02-02 16:25:35 -080048static int ceph_x_encrypt(struct ceph_crypto_key *secret,
49 void *ibuf, int ilen, void *obuf, size_t olen)
50{
51 struct ceph_x_encrypt_header head = {
52 .struct_v = 1,
53 .magic = cpu_to_le64(CEPHX_ENC_MAGIC)
54 };
55 size_t len = olen - sizeof(u32);
56 int ret;
57
58 ret = ceph_encrypt2(secret, obuf + sizeof(u32), &len,
59 &head, sizeof(head), ibuf, ilen);
60 if (ret)
61 return ret;
62 ceph_encode_32(&obuf, len);
63 return len + sizeof(u32);
64}
65
66static int ceph_x_decrypt(struct ceph_crypto_key *secret,
67 void **p, void *end, void *obuf, size_t olen)
68{
69 struct ceph_x_encrypt_header head;
70 size_t head_len = sizeof(head);
71 int len, ret;
72
73 len = ceph_decode_32(p);
74 if (*p + len > end)
75 return -EINVAL;
76
77 dout("ceph_x_decrypt len %d\n", len);
78 ret = ceph_decrypt2(secret, &head, &head_len, obuf, &olen,
79 *p, len);
80 if (ret)
81 return ret;
82 if (head.struct_v != 1 || le64_to_cpu(head.magic) != CEPHX_ENC_MAGIC)
83 return -EPERM;
84 *p += len;
85 return olen;
86}
87
88/*
89 * get existing (or insert new) ticket handler
90 */
Yehuda Sadehcd84db62010-06-11 16:58:48 -070091static struct ceph_x_ticket_handler *
92get_ticket_handler(struct ceph_auth_client *ac, int service)
Sage Weilec0994e2010-02-02 16:25:35 -080093{
94 struct ceph_x_ticket_handler *th;
95 struct ceph_x_info *xi = ac->private;
96 struct rb_node *parent = NULL, **p = &xi->ticket_handlers.rb_node;
97
98 while (*p) {
99 parent = *p;
100 th = rb_entry(parent, struct ceph_x_ticket_handler, node);
101 if (service < th->service)
102 p = &(*p)->rb_left;
103 else if (service > th->service)
104 p = &(*p)->rb_right;
105 else
106 return th;
107 }
108
109 /* add it */
110 th = kzalloc(sizeof(*th), GFP_NOFS);
111 if (!th)
112 return ERR_PTR(-ENOMEM);
113 th->service = service;
114 rb_link_node(&th->node, parent, p);
115 rb_insert_color(&th->node, &xi->ticket_handlers);
116 return th;
117}
118
119static void remove_ticket_handler(struct ceph_auth_client *ac,
120 struct ceph_x_ticket_handler *th)
121{
122 struct ceph_x_info *xi = ac->private;
123
124 dout("remove_ticket_handler %p %d\n", th, th->service);
125 rb_erase(&th->node, &xi->ticket_handlers);
126 ceph_crypto_key_destroy(&th->session_key);
127 if (th->ticket_blob)
128 ceph_buffer_put(th->ticket_blob);
129 kfree(th);
130}
131
132static int ceph_x_proc_ticket_reply(struct ceph_auth_client *ac,
133 struct ceph_crypto_key *secret,
134 void *buf, void *end)
135{
136 struct ceph_x_info *xi = ac->private;
137 int num;
138 void *p = buf;
139 int ret;
140 char *dbuf;
141 char *ticket_buf;
Sage Weilb736b3d2010-04-30 12:45:02 -0700142 u8 reply_struct_v;
Sage Weilec0994e2010-02-02 16:25:35 -0800143
Sage Weilb0930f82010-04-29 13:26:53 -0700144 dbuf = kmalloc(TEMP_TICKET_BUF_LEN, GFP_NOFS);
Sage Weilec0994e2010-02-02 16:25:35 -0800145 if (!dbuf)
146 return -ENOMEM;
147
148 ret = -ENOMEM;
Sage Weilb0930f82010-04-29 13:26:53 -0700149 ticket_buf = kmalloc(TEMP_TICKET_BUF_LEN, GFP_NOFS);
Sage Weilec0994e2010-02-02 16:25:35 -0800150 if (!ticket_buf)
151 goto out_dbuf;
152
153 ceph_decode_need(&p, end, 1 + sizeof(u32), bad);
Sage Weilb736b3d2010-04-30 12:45:02 -0700154 reply_struct_v = ceph_decode_8(&p);
155 if (reply_struct_v != 1)
Sage Weilec0994e2010-02-02 16:25:35 -0800156 goto bad;
157 num = ceph_decode_32(&p);
158 dout("%d tickets\n", num);
159 while (num--) {
160 int type;
Sage Weilb736b3d2010-04-30 12:45:02 -0700161 u8 tkt_struct_v, blob_struct_v;
Sage Weilec0994e2010-02-02 16:25:35 -0800162 struct ceph_x_ticket_handler *th;
163 void *dp, *dend;
164 int dlen;
165 char is_enc;
166 struct timespec validity;
167 struct ceph_crypto_key old_key;
168 void *tp, *tpend;
Sage Weil0a990e72010-03-15 16:12:25 -0700169 struct ceph_timespec new_validity;
170 struct ceph_crypto_key new_session_key;
Sage Weil5b3dbb42010-03-15 15:57:04 -0700171 struct ceph_buffer *new_ticket_blob;
Sage Weil0a990e72010-03-15 16:12:25 -0700172 unsigned long new_expires, new_renew_after;
173 u64 new_secret_id;
Sage Weilec0994e2010-02-02 16:25:35 -0800174
175 ceph_decode_need(&p, end, sizeof(u32) + 1, bad);
176
177 type = ceph_decode_32(&p);
178 dout(" ticket type %d %s\n", type, ceph_entity_type_name(type));
179
Sage Weilb736b3d2010-04-30 12:45:02 -0700180 tkt_struct_v = ceph_decode_8(&p);
181 if (tkt_struct_v != 1)
Sage Weilec0994e2010-02-02 16:25:35 -0800182 goto bad;
183
184 th = get_ticket_handler(ac, type);
185 if (IS_ERR(th)) {
186 ret = PTR_ERR(th);
187 goto out;
188 }
189
190 /* blob for me */
191 dlen = ceph_x_decrypt(secret, &p, end, dbuf,
192 TEMP_TICKET_BUF_LEN);
193 if (dlen <= 0) {
194 ret = dlen;
195 goto out;
196 }
197 dout(" decrypted %d bytes\n", dlen);
198 dend = dbuf + dlen;
199 dp = dbuf;
200
Sage Weilb736b3d2010-04-30 12:45:02 -0700201 tkt_struct_v = ceph_decode_8(&dp);
202 if (tkt_struct_v != 1)
Sage Weilec0994e2010-02-02 16:25:35 -0800203 goto bad;
204
205 memcpy(&old_key, &th->session_key, sizeof(old_key));
Sage Weil0a990e72010-03-15 16:12:25 -0700206 ret = ceph_crypto_key_decode(&new_session_key, &dp, dend);
Sage Weilec0994e2010-02-02 16:25:35 -0800207 if (ret)
208 goto out;
209
Sage Weil0a990e72010-03-15 16:12:25 -0700210 ceph_decode_copy(&dp, &new_validity, sizeof(new_validity));
211 ceph_decode_timespec(&validity, &new_validity);
212 new_expires = get_seconds() + validity.tv_sec;
213 new_renew_after = new_expires - (validity.tv_sec / 4);
214 dout(" expires=%lu renew_after=%lu\n", new_expires,
215 new_renew_after);
Sage Weilec0994e2010-02-02 16:25:35 -0800216
217 /* ticket blob for service */
218 ceph_decode_8_safe(&p, end, is_enc, bad);
219 tp = ticket_buf;
220 if (is_enc) {
221 /* encrypted */
222 dout(" encrypted ticket\n");
223 dlen = ceph_x_decrypt(&old_key, &p, end, ticket_buf,
224 TEMP_TICKET_BUF_LEN);
225 if (dlen < 0) {
226 ret = dlen;
227 goto out;
228 }
229 dlen = ceph_decode_32(&tp);
230 } else {
231 /* unencrypted */
232 ceph_decode_32_safe(&p, end, dlen, bad);
233 ceph_decode_need(&p, end, dlen, bad);
234 ceph_decode_copy(&p, ticket_buf, dlen);
235 }
236 tpend = tp + dlen;
237 dout(" ticket blob is %d bytes\n", dlen);
238 ceph_decode_need(&tp, tpend, 1 + sizeof(u64), bad);
Sage Weilb736b3d2010-04-30 12:45:02 -0700239 blob_struct_v = ceph_decode_8(&tp);
Sage Weil0a990e72010-03-15 16:12:25 -0700240 new_secret_id = ceph_decode_64(&tp);
Sage Weil5b3dbb42010-03-15 15:57:04 -0700241 ret = ceph_decode_buffer(&new_ticket_blob, &tp, tpend);
Sage Weilec0994e2010-02-02 16:25:35 -0800242 if (ret)
243 goto out;
Sage Weil0a990e72010-03-15 16:12:25 -0700244
245 /* all is well, update our ticket */
246 ceph_crypto_key_destroy(&th->session_key);
Sage Weil5b3dbb42010-03-15 15:57:04 -0700247 if (th->ticket_blob)
248 ceph_buffer_put(th->ticket_blob);
Sage Weil0a990e72010-03-15 16:12:25 -0700249 th->session_key = new_session_key;
Sage Weil5b3dbb42010-03-15 15:57:04 -0700250 th->ticket_blob = new_ticket_blob;
Sage Weil0a990e72010-03-15 16:12:25 -0700251 th->validity = new_validity;
252 th->secret_id = new_secret_id;
253 th->expires = new_expires;
254 th->renew_after = new_renew_after;
Sage Weilec0994e2010-02-02 16:25:35 -0800255 dout(" got ticket service %d (%s) secret_id %lld len %d\n",
256 type, ceph_entity_type_name(type), th->secret_id,
257 (int)th->ticket_blob->vec.iov_len);
258 xi->have_keys |= th->service;
259 }
260
261 ret = 0;
262out:
Sage Weilb0930f82010-04-29 13:26:53 -0700263 kfree(ticket_buf);
Sage Weilec0994e2010-02-02 16:25:35 -0800264out_dbuf:
Sage Weilb0930f82010-04-29 13:26:53 -0700265 kfree(dbuf);
Sage Weilec0994e2010-02-02 16:25:35 -0800266 return ret;
267
268bad:
269 ret = -EINVAL;
270 goto out;
271}
272
273static int ceph_x_build_authorizer(struct ceph_auth_client *ac,
274 struct ceph_x_ticket_handler *th,
275 struct ceph_x_authorizer *au)
276{
Sage Weil807c86e2010-03-15 15:52:17 -0700277 int maxlen;
Sage Weilec0994e2010-02-02 16:25:35 -0800278 struct ceph_x_authorize_a *msg_a;
279 struct ceph_x_authorize_b msg_b;
280 void *p, *end;
281 int ret;
282 int ticket_blob_len =
283 (th->ticket_blob ? th->ticket_blob->vec.iov_len : 0);
284
285 dout("build_authorizer for %s %p\n",
286 ceph_entity_type_name(th->service), au);
287
Sage Weil807c86e2010-03-15 15:52:17 -0700288 maxlen = sizeof(*msg_a) + sizeof(msg_b) +
289 ceph_x_encrypt_buflen(ticket_blob_len);
290 dout(" need len %d\n", maxlen);
291 if (au->buf && au->buf->alloc_len < maxlen) {
Sage Weilec0994e2010-02-02 16:25:35 -0800292 ceph_buffer_put(au->buf);
293 au->buf = NULL;
294 }
295 if (!au->buf) {
Sage Weil807c86e2010-03-15 15:52:17 -0700296 au->buf = ceph_buffer_new(maxlen, GFP_NOFS);
Sage Weilec0994e2010-02-02 16:25:35 -0800297 if (!au->buf)
298 return -ENOMEM;
299 }
300 au->service = th->service;
301
302 msg_a = au->buf->vec.iov_base;
303 msg_a->struct_v = 1;
304 msg_a->global_id = cpu_to_le64(ac->global_id);
305 msg_a->service_id = cpu_to_le32(th->service);
306 msg_a->ticket_blob.struct_v = 1;
307 msg_a->ticket_blob.secret_id = cpu_to_le64(th->secret_id);
308 msg_a->ticket_blob.blob_len = cpu_to_le32(ticket_blob_len);
309 if (ticket_blob_len) {
310 memcpy(msg_a->ticket_blob.blob, th->ticket_blob->vec.iov_base,
311 th->ticket_blob->vec.iov_len);
312 }
313 dout(" th %p secret_id %lld %lld\n", th, th->secret_id,
314 le64_to_cpu(msg_a->ticket_blob.secret_id));
315
316 p = msg_a + 1;
317 p += ticket_blob_len;
318 end = au->buf->vec.iov_base + au->buf->vec.iov_len;
319
320 get_random_bytes(&au->nonce, sizeof(au->nonce));
321 msg_b.struct_v = 1;
322 msg_b.nonce = cpu_to_le64(au->nonce);
323 ret = ceph_x_encrypt(&th->session_key, &msg_b, sizeof(msg_b),
324 p, end - p);
325 if (ret < 0)
326 goto out_buf;
327 p += ret;
328 au->buf->vec.iov_len = p - au->buf->vec.iov_base;
329 dout(" built authorizer nonce %llx len %d\n", au->nonce,
330 (int)au->buf->vec.iov_len);
Sage Weil807c86e2010-03-15 15:52:17 -0700331 BUG_ON(au->buf->vec.iov_len > maxlen);
Sage Weilec0994e2010-02-02 16:25:35 -0800332 return 0;
333
334out_buf:
335 ceph_buffer_put(au->buf);
336 au->buf = NULL;
337 return ret;
338}
339
340static int ceph_x_encode_ticket(struct ceph_x_ticket_handler *th,
341 void **p, void *end)
342{
343 ceph_decode_need(p, end, 1 + sizeof(u64), bad);
344 ceph_encode_8(p, 1);
345 ceph_encode_64(p, th->secret_id);
346 if (th->ticket_blob) {
347 const char *buf = th->ticket_blob->vec.iov_base;
348 u32 len = th->ticket_blob->vec.iov_len;
349
350 ceph_encode_32_safe(p, end, len, bad);
351 ceph_encode_copy_safe(p, end, buf, len, bad);
352 } else {
353 ceph_encode_32_safe(p, end, 0, bad);
354 }
355
356 return 0;
357bad:
358 return -ERANGE;
359}
360
361static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed)
362{
363 int want = ac->want_keys;
364 struct ceph_x_info *xi = ac->private;
365 int service;
366
367 *pneed = ac->want_keys & ~(xi->have_keys);
368
369 for (service = 1; service <= want; service <<= 1) {
370 struct ceph_x_ticket_handler *th;
371
372 if (!(ac->want_keys & service))
373 continue;
374
375 if (*pneed & service)
376 continue;
377
378 th = get_ticket_handler(ac, service);
379
Dan Carpenterb5457872010-08-26 11:12:38 +0200380 if (IS_ERR(th)) {
Sage Weilec0994e2010-02-02 16:25:35 -0800381 *pneed |= service;
382 continue;
383 }
384
385 if (get_seconds() >= th->renew_after)
386 *pneed |= service;
387 if (get_seconds() >= th->expires)
388 xi->have_keys &= ~service;
389 }
390}
391
392
393static int ceph_x_build_request(struct ceph_auth_client *ac,
394 void *buf, void *end)
395{
396 struct ceph_x_info *xi = ac->private;
397 int need;
398 struct ceph_x_request_header *head = buf;
399 int ret;
400 struct ceph_x_ticket_handler *th =
401 get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
402
Dan Carpenterb5457872010-08-26 11:12:38 +0200403 if (IS_ERR(th))
404 return PTR_ERR(th);
405
Sage Weilec0994e2010-02-02 16:25:35 -0800406 ceph_x_validate_tickets(ac, &need);
407
408 dout("build_request want %x have %x need %x\n",
409 ac->want_keys, xi->have_keys, need);
410
411 if (need & CEPH_ENTITY_TYPE_AUTH) {
412 struct ceph_x_authenticate *auth = (void *)(head + 1);
413 void *p = auth + 1;
414 struct ceph_x_challenge_blob tmp;
415 char tmp_enc[40];
416 u64 *u;
417
418 if (p > end)
419 return -ERANGE;
420
421 dout(" get_auth_session_key\n");
422 head->op = cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY);
423
424 /* encrypt and hash */
425 get_random_bytes(&auth->client_challenge, sizeof(u64));
426 tmp.client_challenge = auth->client_challenge;
427 tmp.server_challenge = cpu_to_le64(xi->server_challenge);
428 ret = ceph_x_encrypt(&xi->secret, &tmp, sizeof(tmp),
429 tmp_enc, sizeof(tmp_enc));
430 if (ret < 0)
431 return ret;
432
433 auth->struct_v = 1;
434 auth->key = 0;
435 for (u = (u64 *)tmp_enc; u + 1 <= (u64 *)(tmp_enc + ret); u++)
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700436 auth->key ^= *(__le64 *)u;
Sage Weilec0994e2010-02-02 16:25:35 -0800437 dout(" server_challenge %llx client_challenge %llx key %llx\n",
438 xi->server_challenge, le64_to_cpu(auth->client_challenge),
439 le64_to_cpu(auth->key));
440
441 /* now encode the old ticket if exists */
442 ret = ceph_x_encode_ticket(th, &p, end);
443 if (ret < 0)
444 return ret;
445
446 return p - buf;
447 }
448
449 if (need) {
450 void *p = head + 1;
451 struct ceph_x_service_ticket_request *req;
452
453 if (p > end)
454 return -ERANGE;
455 head->op = cpu_to_le16(CEPHX_GET_PRINCIPAL_SESSION_KEY);
456
Sage Weilec0994e2010-02-02 16:25:35 -0800457 ret = ceph_x_build_authorizer(ac, th, &xi->auth_authorizer);
458 if (ret)
459 return ret;
460 ceph_encode_copy(&p, xi->auth_authorizer.buf->vec.iov_base,
461 xi->auth_authorizer.buf->vec.iov_len);
462
463 req = p;
464 req->keys = cpu_to_le32(need);
465 p += sizeof(*req);
466 return p - buf;
467 }
468
469 return 0;
470}
471
472static int ceph_x_handle_reply(struct ceph_auth_client *ac, int result,
473 void *buf, void *end)
474{
475 struct ceph_x_info *xi = ac->private;
476 struct ceph_x_reply_header *head = buf;
477 struct ceph_x_ticket_handler *th;
478 int len = end - buf;
479 int op;
480 int ret;
481
482 if (result)
483 return result; /* XXX hmm? */
484
485 if (xi->starting) {
486 /* it's a hello */
487 struct ceph_x_server_challenge *sc = buf;
488
489 if (len != sizeof(*sc))
490 return -EINVAL;
491 xi->server_challenge = le64_to_cpu(sc->server_challenge);
492 dout("handle_reply got server challenge %llx\n",
493 xi->server_challenge);
494 xi->starting = false;
495 xi->have_keys &= ~CEPH_ENTITY_TYPE_AUTH;
496 return -EAGAIN;
497 }
498
Yehuda Sadeh0cf55372010-06-11 15:57:06 -0700499 op = le16_to_cpu(head->op);
Sage Weilec0994e2010-02-02 16:25:35 -0800500 result = le32_to_cpu(head->result);
501 dout("handle_reply op %d result %d\n", op, result);
502 switch (op) {
503 case CEPHX_GET_AUTH_SESSION_KEY:
504 /* verify auth key */
505 ret = ceph_x_proc_ticket_reply(ac, &xi->secret,
506 buf + sizeof(*head), end);
507 break;
508
509 case CEPHX_GET_PRINCIPAL_SESSION_KEY:
510 th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
Dan Carpenterb5457872010-08-26 11:12:38 +0200511 if (IS_ERR(th))
512 return PTR_ERR(th);
Sage Weilec0994e2010-02-02 16:25:35 -0800513 ret = ceph_x_proc_ticket_reply(ac, &th->session_key,
514 buf + sizeof(*head), end);
515 break;
516
517 default:
518 return -EINVAL;
519 }
520 if (ret)
521 return ret;
522 if (ac->want_keys == xi->have_keys)
523 return 0;
524 return -EAGAIN;
525}
526
527static int ceph_x_create_authorizer(
528 struct ceph_auth_client *ac, int peer_type,
Alex Elder74f18692012-05-16 15:16:39 -0500529 struct ceph_auth_handshake *auth)
Sage Weilec0994e2010-02-02 16:25:35 -0800530{
531 struct ceph_x_authorizer *au;
532 struct ceph_x_ticket_handler *th;
533 int ret;
534
535 th = get_ticket_handler(ac, peer_type);
536 if (IS_ERR(th))
537 return PTR_ERR(th);
538
539 au = kzalloc(sizeof(*au), GFP_NOFS);
540 if (!au)
541 return -ENOMEM;
542
543 ret = ceph_x_build_authorizer(ac, th, au);
544 if (ret) {
545 kfree(au);
546 return ret;
547 }
548
Alex Elder74f18692012-05-16 15:16:39 -0500549 auth->authorizer = (struct ceph_authorizer *) au;
550 auth->authorizer_buf = au->buf->vec.iov_base;
551 auth->authorizer_buf_len = au->buf->vec.iov_len;
552 auth->authorizer_reply_buf = au->reply_buf;
553 auth->authorizer_reply_buf_len = sizeof (au->reply_buf);
554
Sage Weilec0994e2010-02-02 16:25:35 -0800555 return 0;
556}
557
558static int ceph_x_verify_authorizer_reply(struct ceph_auth_client *ac,
559 struct ceph_authorizer *a, size_t len)
560{
561 struct ceph_x_authorizer *au = (void *)a;
562 struct ceph_x_ticket_handler *th;
563 int ret = 0;
564 struct ceph_x_authorize_reply reply;
565 void *p = au->reply_buf;
566 void *end = p + sizeof(au->reply_buf);
567
568 th = get_ticket_handler(ac, au->service);
Dan Carpenterb5457872010-08-26 11:12:38 +0200569 if (IS_ERR(th))
570 return PTR_ERR(th);
Sage Weilec0994e2010-02-02 16:25:35 -0800571 ret = ceph_x_decrypt(&th->session_key, &p, end, &reply, sizeof(reply));
572 if (ret < 0)
573 return ret;
574 if (ret != sizeof(reply))
575 return -EPERM;
576
577 if (au->nonce + 1 != le64_to_cpu(reply.nonce_plus_one))
578 ret = -EPERM;
579 else
580 ret = 0;
581 dout("verify_authorizer_reply nonce %llx got %llx ret %d\n",
582 au->nonce, le64_to_cpu(reply.nonce_plus_one), ret);
583 return ret;
584}
585
586static void ceph_x_destroy_authorizer(struct ceph_auth_client *ac,
587 struct ceph_authorizer *a)
588{
589 struct ceph_x_authorizer *au = (void *)a;
590
591 ceph_buffer_put(au->buf);
592 kfree(au);
593}
594
595
596static void ceph_x_reset(struct ceph_auth_client *ac)
597{
598 struct ceph_x_info *xi = ac->private;
599
600 dout("reset\n");
601 xi->starting = true;
602 xi->server_challenge = 0;
603}
604
605static void ceph_x_destroy(struct ceph_auth_client *ac)
606{
607 struct ceph_x_info *xi = ac->private;
608 struct rb_node *p;
609
610 dout("ceph_x_destroy %p\n", ac);
611 ceph_crypto_key_destroy(&xi->secret);
612
613 while ((p = rb_first(&xi->ticket_handlers)) != NULL) {
614 struct ceph_x_ticket_handler *th =
615 rb_entry(p, struct ceph_x_ticket_handler, node);
616 remove_ticket_handler(ac, th);
617 }
618
Sage Weil22b1de02010-07-05 15:36:49 -0700619 if (xi->auth_authorizer.buf)
620 ceph_buffer_put(xi->auth_authorizer.buf);
621
Sage Weilec0994e2010-02-02 16:25:35 -0800622 kfree(ac->private);
623 ac->private = NULL;
624}
625
626static void ceph_x_invalidate_authorizer(struct ceph_auth_client *ac,
627 int peer_type)
628{
629 struct ceph_x_ticket_handler *th;
630
631 th = get_ticket_handler(ac, peer_type);
Dan Carpenterb5457872010-08-26 11:12:38 +0200632 if (!IS_ERR(th))
Sage Weilec0994e2010-02-02 16:25:35 -0800633 remove_ticket_handler(ac, th);
634}
635
636
637static const struct ceph_auth_client_ops ceph_x_ops = {
Sage Weil559c1e02010-05-14 09:55:18 -0700638 .name = "x",
Sage Weilec0994e2010-02-02 16:25:35 -0800639 .is_authenticated = ceph_x_is_authenticated,
Sage Weila41359f2010-05-25 15:39:06 -0700640 .should_authenticate = ceph_x_should_authenticate,
Sage Weilec0994e2010-02-02 16:25:35 -0800641 .build_request = ceph_x_build_request,
642 .handle_reply = ceph_x_handle_reply,
643 .create_authorizer = ceph_x_create_authorizer,
644 .verify_authorizer_reply = ceph_x_verify_authorizer_reply,
645 .destroy_authorizer = ceph_x_destroy_authorizer,
646 .invalidate_authorizer = ceph_x_invalidate_authorizer,
647 .reset = ceph_x_reset,
648 .destroy = ceph_x_destroy,
649};
650
651
652int ceph_x_init(struct ceph_auth_client *ac)
653{
654 struct ceph_x_info *xi;
655 int ret;
656
657 dout("ceph_x_init %p\n", ac);
Sage Weilb0930f82010-04-29 13:26:53 -0700658 ret = -ENOMEM;
Sage Weilec0994e2010-02-02 16:25:35 -0800659 xi = kzalloc(sizeof(*xi), GFP_NOFS);
660 if (!xi)
Sage Weilb0930f82010-04-29 13:26:53 -0700661 goto out;
Sage Weilec0994e2010-02-02 16:25:35 -0800662
Sage Weilec0994e2010-02-02 16:25:35 -0800663 ret = -EINVAL;
Tommi Virtanen8323c3a2011-03-25 16:32:57 -0700664 if (!ac->key) {
Sage Weilec0994e2010-02-02 16:25:35 -0800665 pr_err("no secret set (for auth_x protocol)\n");
Sage Weilb0930f82010-04-29 13:26:53 -0700666 goto out_nomem;
Sage Weilec0994e2010-02-02 16:25:35 -0800667 }
668
Tommi Virtanen8323c3a2011-03-25 16:32:57 -0700669 ret = ceph_crypto_key_clone(&xi->secret, ac->key);
670 if (ret < 0) {
671 pr_err("cannot clone key: %d\n", ret);
Sage Weilb0930f82010-04-29 13:26:53 -0700672 goto out_nomem;
Tommi Virtanen8323c3a2011-03-25 16:32:57 -0700673 }
Sage Weilec0994e2010-02-02 16:25:35 -0800674
675 xi->starting = true;
676 xi->ticket_handlers = RB_ROOT;
677
678 ac->protocol = CEPH_AUTH_CEPHX;
679 ac->private = xi;
680 ac->ops = &ceph_x_ops;
681 return 0;
682
Sage Weilb0930f82010-04-29 13:26:53 -0700683out_nomem:
Sage Weilec0994e2010-02-02 16:25:35 -0800684 kfree(xi);
Sage Weilb0930f82010-04-29 13:26:53 -0700685out:
Sage Weilec0994e2010-02-02 16:25:35 -0800686 return ret;
687}
688
689