Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 1 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 2 | #include <linux/ceph/ceph_debug.h> |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 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 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 9 | #include <linux/ceph/decode.h> |
| 10 | #include <linux/ceph/auth.h> |
| 11 | |
| 12 | #include "crypto.h" |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 13 | #include "auth_x.h" |
| 14 | #include "auth_x_protocol.h" |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 15 | |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 16 | #define TEMP_TICKET_BUF_LEN 256 |
| 17 | |
| 18 | static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed); |
| 19 | |
| 20 | static 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 Weil | a41359f | 2010-05-25 15:39:06 -0700 | [diff] [blame] | 31 | static 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 Weil | 807c86e | 2010-03-15 15:52:17 -0700 | [diff] [blame] | 42 | static int ceph_x_encrypt_buflen(int ilen) |
| 43 | { |
| 44 | return sizeof(struct ceph_x_encrypt_header) + ilen + 16 + |
| 45 | sizeof(u32); |
| 46 | } |
| 47 | |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 48 | static 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 | |
| 66 | static 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 Sadeh | cd84db6 | 2010-06-11 16:58:48 -0700 | [diff] [blame] | 91 | static struct ceph_x_ticket_handler * |
| 92 | get_ticket_handler(struct ceph_auth_client *ac, int service) |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 93 | { |
| 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 | |
| 119 | static 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 | |
| 132 | static 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 Weil | b736b3d | 2010-04-30 12:45:02 -0700 | [diff] [blame] | 142 | u8 reply_struct_v; |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 143 | |
Sage Weil | b0930f8 | 2010-04-29 13:26:53 -0700 | [diff] [blame] | 144 | dbuf = kmalloc(TEMP_TICKET_BUF_LEN, GFP_NOFS); |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 145 | if (!dbuf) |
| 146 | return -ENOMEM; |
| 147 | |
| 148 | ret = -ENOMEM; |
Sage Weil | b0930f8 | 2010-04-29 13:26:53 -0700 | [diff] [blame] | 149 | ticket_buf = kmalloc(TEMP_TICKET_BUF_LEN, GFP_NOFS); |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 150 | if (!ticket_buf) |
| 151 | goto out_dbuf; |
| 152 | |
| 153 | ceph_decode_need(&p, end, 1 + sizeof(u32), bad); |
Sage Weil | b736b3d | 2010-04-30 12:45:02 -0700 | [diff] [blame] | 154 | reply_struct_v = ceph_decode_8(&p); |
| 155 | if (reply_struct_v != 1) |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 156 | goto bad; |
| 157 | num = ceph_decode_32(&p); |
| 158 | dout("%d tickets\n", num); |
| 159 | while (num--) { |
| 160 | int type; |
Sage Weil | b736b3d | 2010-04-30 12:45:02 -0700 | [diff] [blame] | 161 | u8 tkt_struct_v, blob_struct_v; |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 162 | 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 Weil | 0a990e7 | 2010-03-15 16:12:25 -0700 | [diff] [blame] | 169 | struct ceph_timespec new_validity; |
| 170 | struct ceph_crypto_key new_session_key; |
Sage Weil | 5b3dbb4 | 2010-03-15 15:57:04 -0700 | [diff] [blame] | 171 | struct ceph_buffer *new_ticket_blob; |
Sage Weil | 0a990e7 | 2010-03-15 16:12:25 -0700 | [diff] [blame] | 172 | unsigned long new_expires, new_renew_after; |
| 173 | u64 new_secret_id; |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 174 | |
| 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 Weil | b736b3d | 2010-04-30 12:45:02 -0700 | [diff] [blame] | 180 | tkt_struct_v = ceph_decode_8(&p); |
| 181 | if (tkt_struct_v != 1) |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 182 | 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 Weil | b736b3d | 2010-04-30 12:45:02 -0700 | [diff] [blame] | 201 | tkt_struct_v = ceph_decode_8(&dp); |
| 202 | if (tkt_struct_v != 1) |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 203 | goto bad; |
| 204 | |
| 205 | memcpy(&old_key, &th->session_key, sizeof(old_key)); |
Sage Weil | 0a990e7 | 2010-03-15 16:12:25 -0700 | [diff] [blame] | 206 | ret = ceph_crypto_key_decode(&new_session_key, &dp, dend); |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 207 | if (ret) |
| 208 | goto out; |
| 209 | |
Sage Weil | 0a990e7 | 2010-03-15 16:12:25 -0700 | [diff] [blame] | 210 | 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 Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 216 | |
| 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 Weil | b736b3d | 2010-04-30 12:45:02 -0700 | [diff] [blame] | 239 | blob_struct_v = ceph_decode_8(&tp); |
Sage Weil | 0a990e7 | 2010-03-15 16:12:25 -0700 | [diff] [blame] | 240 | new_secret_id = ceph_decode_64(&tp); |
Sage Weil | 5b3dbb4 | 2010-03-15 15:57:04 -0700 | [diff] [blame] | 241 | ret = ceph_decode_buffer(&new_ticket_blob, &tp, tpend); |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 242 | if (ret) |
| 243 | goto out; |
Sage Weil | 0a990e7 | 2010-03-15 16:12:25 -0700 | [diff] [blame] | 244 | |
| 245 | /* all is well, update our ticket */ |
| 246 | ceph_crypto_key_destroy(&th->session_key); |
Sage Weil | 5b3dbb4 | 2010-03-15 15:57:04 -0700 | [diff] [blame] | 247 | if (th->ticket_blob) |
| 248 | ceph_buffer_put(th->ticket_blob); |
Sage Weil | 0a990e7 | 2010-03-15 16:12:25 -0700 | [diff] [blame] | 249 | th->session_key = new_session_key; |
Sage Weil | 5b3dbb4 | 2010-03-15 15:57:04 -0700 | [diff] [blame] | 250 | th->ticket_blob = new_ticket_blob; |
Sage Weil | 0a990e7 | 2010-03-15 16:12:25 -0700 | [diff] [blame] | 251 | th->validity = new_validity; |
| 252 | th->secret_id = new_secret_id; |
| 253 | th->expires = new_expires; |
| 254 | th->renew_after = new_renew_after; |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 255 | 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; |
| 262 | out: |
Sage Weil | b0930f8 | 2010-04-29 13:26:53 -0700 | [diff] [blame] | 263 | kfree(ticket_buf); |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 264 | out_dbuf: |
Sage Weil | b0930f8 | 2010-04-29 13:26:53 -0700 | [diff] [blame] | 265 | kfree(dbuf); |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 266 | return ret; |
| 267 | |
| 268 | bad: |
| 269 | ret = -EINVAL; |
| 270 | goto out; |
| 271 | } |
| 272 | |
| 273 | static 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 Weil | 807c86e | 2010-03-15 15:52:17 -0700 | [diff] [blame] | 277 | int maxlen; |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 278 | 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 Weil | 807c86e | 2010-03-15 15:52:17 -0700 | [diff] [blame] | 288 | 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 Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 292 | ceph_buffer_put(au->buf); |
| 293 | au->buf = NULL; |
| 294 | } |
| 295 | if (!au->buf) { |
Sage Weil | 807c86e | 2010-03-15 15:52:17 -0700 | [diff] [blame] | 296 | au->buf = ceph_buffer_new(maxlen, GFP_NOFS); |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 297 | if (!au->buf) |
| 298 | return -ENOMEM; |
| 299 | } |
| 300 | au->service = th->service; |
Sage Weil | 0bed9b5 | 2013-03-25 10:26:01 -0700 | [diff] [blame] | 301 | au->secret_id = th->secret_id; |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 302 | |
| 303 | msg_a = au->buf->vec.iov_base; |
| 304 | msg_a->struct_v = 1; |
| 305 | msg_a->global_id = cpu_to_le64(ac->global_id); |
| 306 | msg_a->service_id = cpu_to_le32(th->service); |
| 307 | msg_a->ticket_blob.struct_v = 1; |
| 308 | msg_a->ticket_blob.secret_id = cpu_to_le64(th->secret_id); |
| 309 | msg_a->ticket_blob.blob_len = cpu_to_le32(ticket_blob_len); |
| 310 | if (ticket_blob_len) { |
| 311 | memcpy(msg_a->ticket_blob.blob, th->ticket_blob->vec.iov_base, |
| 312 | th->ticket_blob->vec.iov_len); |
| 313 | } |
| 314 | dout(" th %p secret_id %lld %lld\n", th, th->secret_id, |
| 315 | le64_to_cpu(msg_a->ticket_blob.secret_id)); |
| 316 | |
| 317 | p = msg_a + 1; |
| 318 | p += ticket_blob_len; |
| 319 | end = au->buf->vec.iov_base + au->buf->vec.iov_len; |
| 320 | |
| 321 | get_random_bytes(&au->nonce, sizeof(au->nonce)); |
| 322 | msg_b.struct_v = 1; |
| 323 | msg_b.nonce = cpu_to_le64(au->nonce); |
| 324 | ret = ceph_x_encrypt(&th->session_key, &msg_b, sizeof(msg_b), |
| 325 | p, end - p); |
| 326 | if (ret < 0) |
| 327 | goto out_buf; |
| 328 | p += ret; |
| 329 | au->buf->vec.iov_len = p - au->buf->vec.iov_base; |
| 330 | dout(" built authorizer nonce %llx len %d\n", au->nonce, |
| 331 | (int)au->buf->vec.iov_len); |
Sage Weil | 807c86e | 2010-03-15 15:52:17 -0700 | [diff] [blame] | 332 | BUG_ON(au->buf->vec.iov_len > maxlen); |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 333 | return 0; |
| 334 | |
| 335 | out_buf: |
| 336 | ceph_buffer_put(au->buf); |
| 337 | au->buf = NULL; |
| 338 | return ret; |
| 339 | } |
| 340 | |
| 341 | static int ceph_x_encode_ticket(struct ceph_x_ticket_handler *th, |
| 342 | void **p, void *end) |
| 343 | { |
| 344 | ceph_decode_need(p, end, 1 + sizeof(u64), bad); |
| 345 | ceph_encode_8(p, 1); |
| 346 | ceph_encode_64(p, th->secret_id); |
| 347 | if (th->ticket_blob) { |
| 348 | const char *buf = th->ticket_blob->vec.iov_base; |
| 349 | u32 len = th->ticket_blob->vec.iov_len; |
| 350 | |
| 351 | ceph_encode_32_safe(p, end, len, bad); |
| 352 | ceph_encode_copy_safe(p, end, buf, len, bad); |
| 353 | } else { |
| 354 | ceph_encode_32_safe(p, end, 0, bad); |
| 355 | } |
| 356 | |
| 357 | return 0; |
| 358 | bad: |
| 359 | return -ERANGE; |
| 360 | } |
| 361 | |
| 362 | static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed) |
| 363 | { |
| 364 | int want = ac->want_keys; |
| 365 | struct ceph_x_info *xi = ac->private; |
| 366 | int service; |
| 367 | |
| 368 | *pneed = ac->want_keys & ~(xi->have_keys); |
| 369 | |
| 370 | for (service = 1; service <= want; service <<= 1) { |
| 371 | struct ceph_x_ticket_handler *th; |
| 372 | |
| 373 | if (!(ac->want_keys & service)) |
| 374 | continue; |
| 375 | |
| 376 | if (*pneed & service) |
| 377 | continue; |
| 378 | |
| 379 | th = get_ticket_handler(ac, service); |
| 380 | |
Dan Carpenter | b545787 | 2010-08-26 11:12:38 +0200 | [diff] [blame] | 381 | if (IS_ERR(th)) { |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 382 | *pneed |= service; |
| 383 | continue; |
| 384 | } |
| 385 | |
| 386 | if (get_seconds() >= th->renew_after) |
| 387 | *pneed |= service; |
| 388 | if (get_seconds() >= th->expires) |
| 389 | xi->have_keys &= ~service; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | |
| 394 | static int ceph_x_build_request(struct ceph_auth_client *ac, |
| 395 | void *buf, void *end) |
| 396 | { |
| 397 | struct ceph_x_info *xi = ac->private; |
| 398 | int need; |
| 399 | struct ceph_x_request_header *head = buf; |
| 400 | int ret; |
| 401 | struct ceph_x_ticket_handler *th = |
| 402 | get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH); |
| 403 | |
Dan Carpenter | b545787 | 2010-08-26 11:12:38 +0200 | [diff] [blame] | 404 | if (IS_ERR(th)) |
| 405 | return PTR_ERR(th); |
| 406 | |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 407 | ceph_x_validate_tickets(ac, &need); |
| 408 | |
| 409 | dout("build_request want %x have %x need %x\n", |
| 410 | ac->want_keys, xi->have_keys, need); |
| 411 | |
| 412 | if (need & CEPH_ENTITY_TYPE_AUTH) { |
| 413 | struct ceph_x_authenticate *auth = (void *)(head + 1); |
| 414 | void *p = auth + 1; |
| 415 | struct ceph_x_challenge_blob tmp; |
| 416 | char tmp_enc[40]; |
| 417 | u64 *u; |
| 418 | |
| 419 | if (p > end) |
| 420 | return -ERANGE; |
| 421 | |
| 422 | dout(" get_auth_session_key\n"); |
| 423 | head->op = cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY); |
| 424 | |
| 425 | /* encrypt and hash */ |
| 426 | get_random_bytes(&auth->client_challenge, sizeof(u64)); |
| 427 | tmp.client_challenge = auth->client_challenge; |
| 428 | tmp.server_challenge = cpu_to_le64(xi->server_challenge); |
| 429 | ret = ceph_x_encrypt(&xi->secret, &tmp, sizeof(tmp), |
| 430 | tmp_enc, sizeof(tmp_enc)); |
| 431 | if (ret < 0) |
| 432 | return ret; |
| 433 | |
| 434 | auth->struct_v = 1; |
| 435 | auth->key = 0; |
| 436 | for (u = (u64 *)tmp_enc; u + 1 <= (u64 *)(tmp_enc + ret); u++) |
Yehuda Sadeh | cd84db6 | 2010-06-11 16:58:48 -0700 | [diff] [blame] | 437 | auth->key ^= *(__le64 *)u; |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 438 | dout(" server_challenge %llx client_challenge %llx key %llx\n", |
| 439 | xi->server_challenge, le64_to_cpu(auth->client_challenge), |
| 440 | le64_to_cpu(auth->key)); |
| 441 | |
| 442 | /* now encode the old ticket if exists */ |
| 443 | ret = ceph_x_encode_ticket(th, &p, end); |
| 444 | if (ret < 0) |
| 445 | return ret; |
| 446 | |
| 447 | return p - buf; |
| 448 | } |
| 449 | |
| 450 | if (need) { |
| 451 | void *p = head + 1; |
| 452 | struct ceph_x_service_ticket_request *req; |
| 453 | |
| 454 | if (p > end) |
| 455 | return -ERANGE; |
| 456 | head->op = cpu_to_le16(CEPHX_GET_PRINCIPAL_SESSION_KEY); |
| 457 | |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 458 | ret = ceph_x_build_authorizer(ac, th, &xi->auth_authorizer); |
| 459 | if (ret) |
| 460 | return ret; |
| 461 | ceph_encode_copy(&p, xi->auth_authorizer.buf->vec.iov_base, |
| 462 | xi->auth_authorizer.buf->vec.iov_len); |
| 463 | |
| 464 | req = p; |
| 465 | req->keys = cpu_to_le32(need); |
| 466 | p += sizeof(*req); |
| 467 | return p - buf; |
| 468 | } |
| 469 | |
| 470 | return 0; |
| 471 | } |
| 472 | |
| 473 | static int ceph_x_handle_reply(struct ceph_auth_client *ac, int result, |
| 474 | void *buf, void *end) |
| 475 | { |
| 476 | struct ceph_x_info *xi = ac->private; |
| 477 | struct ceph_x_reply_header *head = buf; |
| 478 | struct ceph_x_ticket_handler *th; |
| 479 | int len = end - buf; |
| 480 | int op; |
| 481 | int ret; |
| 482 | |
| 483 | if (result) |
| 484 | return result; /* XXX hmm? */ |
| 485 | |
| 486 | if (xi->starting) { |
| 487 | /* it's a hello */ |
| 488 | struct ceph_x_server_challenge *sc = buf; |
| 489 | |
| 490 | if (len != sizeof(*sc)) |
| 491 | return -EINVAL; |
| 492 | xi->server_challenge = le64_to_cpu(sc->server_challenge); |
| 493 | dout("handle_reply got server challenge %llx\n", |
| 494 | xi->server_challenge); |
| 495 | xi->starting = false; |
| 496 | xi->have_keys &= ~CEPH_ENTITY_TYPE_AUTH; |
| 497 | return -EAGAIN; |
| 498 | } |
| 499 | |
Yehuda Sadeh | 0cf5537 | 2010-06-11 15:57:06 -0700 | [diff] [blame] | 500 | op = le16_to_cpu(head->op); |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 501 | result = le32_to_cpu(head->result); |
| 502 | dout("handle_reply op %d result %d\n", op, result); |
| 503 | switch (op) { |
| 504 | case CEPHX_GET_AUTH_SESSION_KEY: |
| 505 | /* verify auth key */ |
| 506 | ret = ceph_x_proc_ticket_reply(ac, &xi->secret, |
| 507 | buf + sizeof(*head), end); |
| 508 | break; |
| 509 | |
| 510 | case CEPHX_GET_PRINCIPAL_SESSION_KEY: |
| 511 | th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH); |
Dan Carpenter | b545787 | 2010-08-26 11:12:38 +0200 | [diff] [blame] | 512 | if (IS_ERR(th)) |
| 513 | return PTR_ERR(th); |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 514 | ret = ceph_x_proc_ticket_reply(ac, &th->session_key, |
| 515 | buf + sizeof(*head), end); |
| 516 | break; |
| 517 | |
| 518 | default: |
| 519 | return -EINVAL; |
| 520 | } |
| 521 | if (ret) |
| 522 | return ret; |
| 523 | if (ac->want_keys == xi->have_keys) |
| 524 | return 0; |
| 525 | return -EAGAIN; |
| 526 | } |
| 527 | |
| 528 | static int ceph_x_create_authorizer( |
| 529 | struct ceph_auth_client *ac, int peer_type, |
Alex Elder | 74f1869 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 530 | struct ceph_auth_handshake *auth) |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 531 | { |
| 532 | struct ceph_x_authorizer *au; |
| 533 | struct ceph_x_ticket_handler *th; |
| 534 | int ret; |
| 535 | |
| 536 | th = get_ticket_handler(ac, peer_type); |
| 537 | if (IS_ERR(th)) |
| 538 | return PTR_ERR(th); |
| 539 | |
| 540 | au = kzalloc(sizeof(*au), GFP_NOFS); |
| 541 | if (!au) |
| 542 | return -ENOMEM; |
| 543 | |
| 544 | ret = ceph_x_build_authorizer(ac, th, au); |
| 545 | if (ret) { |
| 546 | kfree(au); |
| 547 | return ret; |
| 548 | } |
| 549 | |
Alex Elder | 74f1869 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 550 | auth->authorizer = (struct ceph_authorizer *) au; |
| 551 | auth->authorizer_buf = au->buf->vec.iov_base; |
| 552 | auth->authorizer_buf_len = au->buf->vec.iov_len; |
| 553 | auth->authorizer_reply_buf = au->reply_buf; |
| 554 | auth->authorizer_reply_buf_len = sizeof (au->reply_buf); |
| 555 | |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 556 | return 0; |
| 557 | } |
| 558 | |
Sage Weil | 0bed9b5 | 2013-03-25 10:26:01 -0700 | [diff] [blame] | 559 | static int ceph_x_update_authorizer( |
| 560 | struct ceph_auth_client *ac, int peer_type, |
| 561 | struct ceph_auth_handshake *auth) |
| 562 | { |
| 563 | struct ceph_x_authorizer *au; |
| 564 | struct ceph_x_ticket_handler *th; |
Sage Weil | 0bed9b5 | 2013-03-25 10:26:01 -0700 | [diff] [blame] | 565 | |
| 566 | th = get_ticket_handler(ac, peer_type); |
| 567 | if (IS_ERR(th)) |
| 568 | return PTR_ERR(th); |
| 569 | |
| 570 | au = (struct ceph_x_authorizer *)auth->authorizer; |
| 571 | if (au->secret_id < th->secret_id) { |
| 572 | dout("ceph_x_update_authorizer service %u secret %llu < %llu\n", |
| 573 | au->service, au->secret_id, th->secret_id); |
| 574 | return ceph_x_build_authorizer(ac, th, au); |
| 575 | } |
| 576 | return 0; |
| 577 | } |
| 578 | |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 579 | static int ceph_x_verify_authorizer_reply(struct ceph_auth_client *ac, |
| 580 | struct ceph_authorizer *a, size_t len) |
| 581 | { |
| 582 | struct ceph_x_authorizer *au = (void *)a; |
| 583 | struct ceph_x_ticket_handler *th; |
| 584 | int ret = 0; |
| 585 | struct ceph_x_authorize_reply reply; |
| 586 | void *p = au->reply_buf; |
| 587 | void *end = p + sizeof(au->reply_buf); |
| 588 | |
| 589 | th = get_ticket_handler(ac, au->service); |
Dan Carpenter | b545787 | 2010-08-26 11:12:38 +0200 | [diff] [blame] | 590 | if (IS_ERR(th)) |
| 591 | return PTR_ERR(th); |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 592 | ret = ceph_x_decrypt(&th->session_key, &p, end, &reply, sizeof(reply)); |
| 593 | if (ret < 0) |
| 594 | return ret; |
| 595 | if (ret != sizeof(reply)) |
| 596 | return -EPERM; |
| 597 | |
| 598 | if (au->nonce + 1 != le64_to_cpu(reply.nonce_plus_one)) |
| 599 | ret = -EPERM; |
| 600 | else |
| 601 | ret = 0; |
| 602 | dout("verify_authorizer_reply nonce %llx got %llx ret %d\n", |
| 603 | au->nonce, le64_to_cpu(reply.nonce_plus_one), ret); |
| 604 | return ret; |
| 605 | } |
| 606 | |
| 607 | static void ceph_x_destroy_authorizer(struct ceph_auth_client *ac, |
| 608 | struct ceph_authorizer *a) |
| 609 | { |
| 610 | struct ceph_x_authorizer *au = (void *)a; |
| 611 | |
| 612 | ceph_buffer_put(au->buf); |
| 613 | kfree(au); |
| 614 | } |
| 615 | |
| 616 | |
| 617 | static void ceph_x_reset(struct ceph_auth_client *ac) |
| 618 | { |
| 619 | struct ceph_x_info *xi = ac->private; |
| 620 | |
| 621 | dout("reset\n"); |
| 622 | xi->starting = true; |
| 623 | xi->server_challenge = 0; |
| 624 | } |
| 625 | |
| 626 | static void ceph_x_destroy(struct ceph_auth_client *ac) |
| 627 | { |
| 628 | struct ceph_x_info *xi = ac->private; |
| 629 | struct rb_node *p; |
| 630 | |
| 631 | dout("ceph_x_destroy %p\n", ac); |
| 632 | ceph_crypto_key_destroy(&xi->secret); |
| 633 | |
| 634 | while ((p = rb_first(&xi->ticket_handlers)) != NULL) { |
| 635 | struct ceph_x_ticket_handler *th = |
| 636 | rb_entry(p, struct ceph_x_ticket_handler, node); |
| 637 | remove_ticket_handler(ac, th); |
| 638 | } |
| 639 | |
Sage Weil | 22b1de0 | 2010-07-05 15:36:49 -0700 | [diff] [blame] | 640 | if (xi->auth_authorizer.buf) |
| 641 | ceph_buffer_put(xi->auth_authorizer.buf); |
| 642 | |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 643 | kfree(ac->private); |
| 644 | ac->private = NULL; |
| 645 | } |
| 646 | |
| 647 | static void ceph_x_invalidate_authorizer(struct ceph_auth_client *ac, |
| 648 | int peer_type) |
| 649 | { |
| 650 | struct ceph_x_ticket_handler *th; |
| 651 | |
| 652 | th = get_ticket_handler(ac, peer_type); |
Dan Carpenter | b545787 | 2010-08-26 11:12:38 +0200 | [diff] [blame] | 653 | if (!IS_ERR(th)) |
Sage Weil | 4b8e8b5 | 2013-03-25 10:25:49 -0700 | [diff] [blame] | 654 | memset(&th->validity, 0, sizeof(th->validity)); |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | |
| 658 | static const struct ceph_auth_client_ops ceph_x_ops = { |
Sage Weil | 559c1e0 | 2010-05-14 09:55:18 -0700 | [diff] [blame] | 659 | .name = "x", |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 660 | .is_authenticated = ceph_x_is_authenticated, |
Sage Weil | a41359f | 2010-05-25 15:39:06 -0700 | [diff] [blame] | 661 | .should_authenticate = ceph_x_should_authenticate, |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 662 | .build_request = ceph_x_build_request, |
| 663 | .handle_reply = ceph_x_handle_reply, |
| 664 | .create_authorizer = ceph_x_create_authorizer, |
Sage Weil | 0bed9b5 | 2013-03-25 10:26:01 -0700 | [diff] [blame] | 665 | .update_authorizer = ceph_x_update_authorizer, |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 666 | .verify_authorizer_reply = ceph_x_verify_authorizer_reply, |
| 667 | .destroy_authorizer = ceph_x_destroy_authorizer, |
| 668 | .invalidate_authorizer = ceph_x_invalidate_authorizer, |
| 669 | .reset = ceph_x_reset, |
| 670 | .destroy = ceph_x_destroy, |
| 671 | }; |
| 672 | |
| 673 | |
| 674 | int ceph_x_init(struct ceph_auth_client *ac) |
| 675 | { |
| 676 | struct ceph_x_info *xi; |
| 677 | int ret; |
| 678 | |
| 679 | dout("ceph_x_init %p\n", ac); |
Sage Weil | b0930f8 | 2010-04-29 13:26:53 -0700 | [diff] [blame] | 680 | ret = -ENOMEM; |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 681 | xi = kzalloc(sizeof(*xi), GFP_NOFS); |
| 682 | if (!xi) |
Sage Weil | b0930f8 | 2010-04-29 13:26:53 -0700 | [diff] [blame] | 683 | goto out; |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 684 | |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 685 | ret = -EINVAL; |
Tommi Virtanen | 8323c3a | 2011-03-25 16:32:57 -0700 | [diff] [blame] | 686 | if (!ac->key) { |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 687 | pr_err("no secret set (for auth_x protocol)\n"); |
Sage Weil | b0930f8 | 2010-04-29 13:26:53 -0700 | [diff] [blame] | 688 | goto out_nomem; |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 689 | } |
| 690 | |
Tommi Virtanen | 8323c3a | 2011-03-25 16:32:57 -0700 | [diff] [blame] | 691 | ret = ceph_crypto_key_clone(&xi->secret, ac->key); |
| 692 | if (ret < 0) { |
| 693 | pr_err("cannot clone key: %d\n", ret); |
Sage Weil | b0930f8 | 2010-04-29 13:26:53 -0700 | [diff] [blame] | 694 | goto out_nomem; |
Tommi Virtanen | 8323c3a | 2011-03-25 16:32:57 -0700 | [diff] [blame] | 695 | } |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 696 | |
| 697 | xi->starting = true; |
| 698 | xi->ticket_handlers = RB_ROOT; |
| 699 | |
| 700 | ac->protocol = CEPH_AUTH_CEPHX; |
| 701 | ac->private = xi; |
| 702 | ac->ops = &ceph_x_ops; |
| 703 | return 0; |
| 704 | |
Sage Weil | b0930f8 | 2010-04-29 13:26:53 -0700 | [diff] [blame] | 705 | out_nomem: |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 706 | kfree(xi); |
Sage Weil | b0930f8 | 2010-04-29 13:26:53 -0700 | [diff] [blame] | 707 | out: |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 708 | return ret; |
| 709 | } |
| 710 | |
| 711 | |