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