1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ceph/ceph_debug.h>
4 #include <linux/module.h>
6 #include <linux/slab.h>
8 #include <linux/ceph/types.h>
9 #include <linux/ceph/decode.h>
10 #include <linux/ceph/libceph.h>
11 #include <linux/ceph/messenger.h>
12 #include "auth_none.h"
17 * get protocol handler
19 static u32 supported_protocols[] = {
24 static int init_protocol(struct ceph_auth_client *ac, int proto)
26 dout("%s proto %d\n", __func__, proto);
30 return ceph_auth_none_init(ac);
32 return ceph_x_init(ac);
34 pr_err("bad auth protocol %d\n", proto);
39 void ceph_auth_set_global_id(struct ceph_auth_client *ac, u64 global_id)
41 dout("%s global_id %llu\n", __func__, global_id);
44 pr_err("got zero global_id\n");
46 if (ac->global_id && global_id != ac->global_id)
47 pr_err("global_id changed from %llu to %llu\n", ac->global_id,
50 ac->global_id = global_id;
56 struct ceph_auth_client *ceph_auth_init(const char *name,
57 const struct ceph_crypto_key *key,
60 struct ceph_auth_client *ac;
64 ac = kzalloc(sizeof(*ac), GFP_NOFS);
68 mutex_init(&ac->mutex);
69 ac->negotiating = true;
73 ac->name = CEPH_AUTH_NAME_DEFAULT;
75 ac->preferred_mode = con_modes[0];
76 ac->fallback_mode = con_modes[1];
78 dout("%s name '%s' preferred_mode %d fallback_mode %d\n", __func__,
79 ac->name, ac->preferred_mode, ac->fallback_mode);
86 void ceph_auth_destroy(struct ceph_auth_client *ac)
88 dout("auth_destroy %p\n", ac);
95 * Reset occurs when reconnecting to the monitor.
97 void ceph_auth_reset(struct ceph_auth_client *ac)
99 mutex_lock(&ac->mutex);
100 dout("auth_reset %p\n", ac);
101 if (ac->ops && !ac->negotiating)
103 ac->negotiating = true;
104 mutex_unlock(&ac->mutex);
108 * EntityName, not to be confused with entity_name_t
110 int ceph_auth_entity_name_encode(const char *name, void **p, void *end)
112 int len = strlen(name);
114 if (*p + 2*sizeof(u32) + len > end)
116 ceph_encode_32(p, CEPH_ENTITY_TYPE_CLIENT);
117 ceph_encode_32(p, len);
118 ceph_encode_copy(p, name, len);
123 * Initiate protocol negotiation with monitor. Include entity name
124 * and list supported protocols.
126 int ceph_auth_build_hello(struct ceph_auth_client *ac, void *buf, size_t len)
128 struct ceph_mon_request_header *monhdr = buf;
129 void *p = monhdr + 1, *end = buf + len, *lenp;
133 mutex_lock(&ac->mutex);
134 dout("auth_build_hello\n");
135 monhdr->have_version = 0;
136 monhdr->session_mon = cpu_to_le16(-1);
137 monhdr->session_mon_tid = 0;
139 ceph_encode_32(&p, CEPH_AUTH_UNKNOWN); /* no protocol, yet */
144 ceph_decode_need(&p, end, 1 + sizeof(u32), bad);
145 ceph_encode_8(&p, 1);
146 num = ARRAY_SIZE(supported_protocols);
147 ceph_encode_32(&p, num);
148 ceph_decode_need(&p, end, num * sizeof(u32), bad);
149 for (i = 0; i < num; i++)
150 ceph_encode_32(&p, supported_protocols[i]);
152 ret = ceph_auth_entity_name_encode(ac->name, &p, end);
155 ceph_decode_need(&p, end, sizeof(u64), bad);
156 ceph_encode_64(&p, ac->global_id);
158 ceph_encode_32(&lenp, p - lenp - sizeof(u32));
161 mutex_unlock(&ac->mutex);
169 static int build_request(struct ceph_auth_client *ac, bool add_header,
170 void *buf, int buf_len)
172 void *end = buf + buf_len;
178 /* struct ceph_mon_request_header + protocol */
179 ceph_encode_64_safe(&p, end, 0, e_range);
180 ceph_encode_16_safe(&p, end, -1, e_range);
181 ceph_encode_64_safe(&p, end, 0, e_range);
182 ceph_encode_32_safe(&p, end, ac->protocol, e_range);
185 ceph_encode_need(&p, end, sizeof(u32), e_range);
186 ret = ac->ops->build_request(ac, p + sizeof(u32), end);
188 pr_err("auth protocol '%s' building request failed: %d\n",
189 ceph_auth_proto_name(ac->protocol), ret);
192 dout(" built request %d bytes\n", ret);
193 ceph_encode_32(&p, ret);
194 return p + ret - buf;
201 * Handle auth message from monitor.
203 int ceph_handle_auth_reply(struct ceph_auth_client *ac,
204 void *buf, size_t len,
205 void *reply_buf, size_t reply_len)
208 void *end = buf + len;
212 void *payload, *payload_end;
218 mutex_lock(&ac->mutex);
219 dout("handle_auth_reply %p %p\n", p, end);
220 ceph_decode_need(&p, end, sizeof(u32) * 3 + sizeof(u64), bad);
221 protocol = ceph_decode_32(&p);
222 result = ceph_decode_32(&p);
223 global_id = ceph_decode_64(&p);
224 payload_len = ceph_decode_32(&p);
227 ceph_decode_need(&p, end, sizeof(u32), bad);
228 result_msg_len = ceph_decode_32(&p);
234 dout(" result %d '%.*s' gid %llu len %d\n", result, result_msg_len,
235 result_msg, global_id, payload_len);
237 payload_end = payload + payload_len;
239 if (ac->negotiating) {
240 /* server does not support our protocols? */
241 if (!protocol && result < 0) {
245 /* set up (new) protocol handler? */
246 if (ac->protocol && ac->protocol != protocol) {
247 ac->ops->destroy(ac);
251 if (ac->protocol != protocol) {
252 ret = init_protocol(ac, protocol);
254 pr_err("auth protocol '%s' init failed: %d\n",
255 ceph_auth_proto_name(protocol), ret);
260 ac->negotiating = false;
264 pr_err("auth protocol '%s' mauth authentication failed: %d\n",
265 ceph_auth_proto_name(ac->protocol), result);
270 ret = ac->ops->handle_reply(ac, global_id, payload, payload_end,
271 NULL, NULL, NULL, NULL);
272 if (ret == -EAGAIN) {
273 ret = build_request(ac, true, reply_buf, reply_len);
280 mutex_unlock(&ac->mutex);
284 pr_err("failed to decode auth msg\n");
289 int ceph_build_auth(struct ceph_auth_client *ac,
290 void *msg_buf, size_t msg_len)
294 mutex_lock(&ac->mutex);
295 if (ac->ops->should_authenticate(ac))
296 ret = build_request(ac, true, msg_buf, msg_len);
297 mutex_unlock(&ac->mutex);
301 int ceph_auth_is_authenticated(struct ceph_auth_client *ac)
305 mutex_lock(&ac->mutex);
307 ret = ac->ops->is_authenticated(ac);
308 mutex_unlock(&ac->mutex);
311 EXPORT_SYMBOL(ceph_auth_is_authenticated);
313 int __ceph_auth_get_authorizer(struct ceph_auth_client *ac,
314 struct ceph_auth_handshake *auth,
315 int peer_type, bool force_new,
316 int *proto, int *pref_mode, int *fallb_mode)
320 mutex_lock(&ac->mutex);
321 if (force_new && auth->authorizer) {
322 ceph_auth_destroy_authorizer(auth->authorizer);
323 auth->authorizer = NULL;
325 if (!auth->authorizer)
326 ret = ac->ops->create_authorizer(ac, peer_type, auth);
327 else if (ac->ops->update_authorizer)
328 ret = ac->ops->update_authorizer(ac, peer_type, auth);
334 *proto = ac->protocol;
335 if (pref_mode && fallb_mode) {
336 *pref_mode = ac->preferred_mode;
337 *fallb_mode = ac->fallback_mode;
341 mutex_unlock(&ac->mutex);
344 EXPORT_SYMBOL(__ceph_auth_get_authorizer);
346 void ceph_auth_destroy_authorizer(struct ceph_authorizer *a)
350 EXPORT_SYMBOL(ceph_auth_destroy_authorizer);
352 int ceph_auth_add_authorizer_challenge(struct ceph_auth_client *ac,
353 struct ceph_authorizer *a,
355 int challenge_buf_len)
359 mutex_lock(&ac->mutex);
360 if (ac->ops && ac->ops->add_authorizer_challenge)
361 ret = ac->ops->add_authorizer_challenge(ac, a, challenge_buf,
363 mutex_unlock(&ac->mutex);
366 EXPORT_SYMBOL(ceph_auth_add_authorizer_challenge);
368 int ceph_auth_verify_authorizer_reply(struct ceph_auth_client *ac,
369 struct ceph_authorizer *a,
370 void *reply, int reply_len,
371 u8 *session_key, int *session_key_len,
372 u8 *con_secret, int *con_secret_len)
376 mutex_lock(&ac->mutex);
377 if (ac->ops && ac->ops->verify_authorizer_reply)
378 ret = ac->ops->verify_authorizer_reply(ac, a,
379 reply, reply_len, session_key, session_key_len,
380 con_secret, con_secret_len);
381 mutex_unlock(&ac->mutex);
384 EXPORT_SYMBOL(ceph_auth_verify_authorizer_reply);
386 void ceph_auth_invalidate_authorizer(struct ceph_auth_client *ac, int peer_type)
388 mutex_lock(&ac->mutex);
389 if (ac->ops && ac->ops->invalidate_authorizer)
390 ac->ops->invalidate_authorizer(ac, peer_type);
391 mutex_unlock(&ac->mutex);
393 EXPORT_SYMBOL(ceph_auth_invalidate_authorizer);
396 * msgr2 authentication
399 static bool contains(const int *arr, int cnt, int val)
403 for (i = 0; i < cnt; i++) {
411 static int encode_con_modes(void **p, void *end, int pref_mode, int fallb_mode)
413 WARN_ON(pref_mode == CEPH_CON_MODE_UNKNOWN);
414 if (fallb_mode != CEPH_CON_MODE_UNKNOWN) {
415 ceph_encode_32_safe(p, end, 2, e_range);
416 ceph_encode_32_safe(p, end, pref_mode, e_range);
417 ceph_encode_32_safe(p, end, fallb_mode, e_range);
419 ceph_encode_32_safe(p, end, 1, e_range);
420 ceph_encode_32_safe(p, end, pref_mode, e_range);
430 * Similar to ceph_auth_build_hello().
432 int ceph_auth_get_request(struct ceph_auth_client *ac, void *buf, int buf_len)
434 int proto = ac->key ? CEPH_AUTH_CEPHX : CEPH_AUTH_NONE;
435 void *end = buf + buf_len;
440 mutex_lock(&ac->mutex);
441 if (ac->protocol == CEPH_AUTH_UNKNOWN) {
442 ret = init_protocol(ac, proto);
444 pr_err("auth protocol '%s' init failed: %d\n",
445 ceph_auth_proto_name(proto), ret);
449 WARN_ON(ac->protocol != proto);
454 ceph_encode_32_safe(&p, end, ac->protocol, e_range);
455 ret = encode_con_modes(&p, end, ac->preferred_mode, ac->fallback_mode);
460 p += 4; /* space for len */
462 ceph_encode_8_safe(&p, end, CEPH_AUTH_MODE_MON, e_range);
463 ret = ceph_auth_entity_name_encode(ac->name, &p, end);
467 ceph_encode_64_safe(&p, end, ac->global_id, e_range);
468 ceph_encode_32(&lenp, p - lenp - 4);
472 mutex_unlock(&ac->mutex);
480 int ceph_auth_handle_reply_more(struct ceph_auth_client *ac, void *reply,
481 int reply_len, void *buf, int buf_len)
485 mutex_lock(&ac->mutex);
486 ret = ac->ops->handle_reply(ac, 0, reply, reply + reply_len,
487 NULL, NULL, NULL, NULL);
489 ret = build_request(ac, false, buf, buf_len);
492 mutex_unlock(&ac->mutex);
496 int ceph_auth_handle_reply_done(struct ceph_auth_client *ac,
497 u64 global_id, void *reply, int reply_len,
498 u8 *session_key, int *session_key_len,
499 u8 *con_secret, int *con_secret_len)
503 mutex_lock(&ac->mutex);
504 ret = ac->ops->handle_reply(ac, global_id, reply, reply + reply_len,
505 session_key, session_key_len,
506 con_secret, con_secret_len);
507 WARN_ON(ret == -EAGAIN || ret > 0);
508 mutex_unlock(&ac->mutex);
512 bool ceph_auth_handle_bad_method(struct ceph_auth_client *ac,
513 int used_proto, int result,
514 const int *allowed_protos, int proto_cnt,
515 const int *allowed_modes, int mode_cnt)
517 mutex_lock(&ac->mutex);
518 WARN_ON(used_proto != ac->protocol);
520 if (result == -EOPNOTSUPP) {
521 if (!contains(allowed_protos, proto_cnt, ac->protocol)) {
522 pr_err("auth protocol '%s' not allowed\n",
523 ceph_auth_proto_name(ac->protocol));
526 if (!contains(allowed_modes, mode_cnt, ac->preferred_mode) &&
527 (ac->fallback_mode == CEPH_CON_MODE_UNKNOWN ||
528 !contains(allowed_modes, mode_cnt, ac->fallback_mode))) {
529 pr_err("preferred mode '%s' not allowed\n",
530 ceph_con_mode_name(ac->preferred_mode));
531 if (ac->fallback_mode == CEPH_CON_MODE_UNKNOWN)
532 pr_err("no fallback mode\n");
534 pr_err("fallback mode '%s' not allowed\n",
535 ceph_con_mode_name(ac->fallback_mode));
540 WARN_ON(result == -EOPNOTSUPP || result >= 0);
541 pr_err("auth protocol '%s' msgr authentication failed: %d\n",
542 ceph_auth_proto_name(ac->protocol), result);
544 mutex_unlock(&ac->mutex);
548 mutex_unlock(&ac->mutex);
552 int ceph_auth_get_authorizer(struct ceph_auth_client *ac,
553 struct ceph_auth_handshake *auth,
554 int peer_type, void *buf, int *buf_len)
556 void *end = buf + *buf_len;
557 int pref_mode, fallb_mode;
562 ret = __ceph_auth_get_authorizer(ac, auth, peer_type, true, &proto,
563 &pref_mode, &fallb_mode);
568 ceph_encode_32_safe(&p, end, proto, e_range);
569 ret = encode_con_modes(&p, end, pref_mode, fallb_mode);
573 ceph_encode_32_safe(&p, end, auth->authorizer_buf_len, e_range);
580 EXPORT_SYMBOL(ceph_auth_get_authorizer);
582 int ceph_auth_handle_svc_reply_more(struct ceph_auth_client *ac,
583 struct ceph_auth_handshake *auth,
584 void *reply, int reply_len,
585 void *buf, int *buf_len)
587 void *end = buf + *buf_len;
591 ret = ceph_auth_add_authorizer_challenge(ac, auth->authorizer,
597 ceph_encode_32_safe(&p, end, auth->authorizer_buf_len, e_range);
604 EXPORT_SYMBOL(ceph_auth_handle_svc_reply_more);
606 int ceph_auth_handle_svc_reply_done(struct ceph_auth_client *ac,
607 struct ceph_auth_handshake *auth,
608 void *reply, int reply_len,
609 u8 *session_key, int *session_key_len,
610 u8 *con_secret, int *con_secret_len)
612 return ceph_auth_verify_authorizer_reply(ac, auth->authorizer,
613 reply, reply_len, session_key, session_key_len,
614 con_secret, con_secret_len);
616 EXPORT_SYMBOL(ceph_auth_handle_svc_reply_done);
618 bool ceph_auth_handle_bad_authorizer(struct ceph_auth_client *ac,
619 int peer_type, int used_proto, int result,
620 const int *allowed_protos, int proto_cnt,
621 const int *allowed_modes, int mode_cnt)
623 mutex_lock(&ac->mutex);
624 WARN_ON(used_proto != ac->protocol);
626 if (result == -EOPNOTSUPP) {
627 if (!contains(allowed_protos, proto_cnt, ac->protocol)) {
628 pr_err("auth protocol '%s' not allowed by %s\n",
629 ceph_auth_proto_name(ac->protocol),
630 ceph_entity_type_name(peer_type));
633 if (!contains(allowed_modes, mode_cnt, ac->preferred_mode) &&
634 (ac->fallback_mode == CEPH_CON_MODE_UNKNOWN ||
635 !contains(allowed_modes, mode_cnt, ac->fallback_mode))) {
636 pr_err("preferred mode '%s' not allowed by %s\n",
637 ceph_con_mode_name(ac->preferred_mode),
638 ceph_entity_type_name(peer_type));
639 if (ac->fallback_mode == CEPH_CON_MODE_UNKNOWN)
640 pr_err("no fallback mode\n");
642 pr_err("fallback mode '%s' not allowed by %s\n",
643 ceph_con_mode_name(ac->fallback_mode),
644 ceph_entity_type_name(peer_type));
649 WARN_ON(result == -EOPNOTSUPP || result >= 0);
650 pr_err("auth protocol '%s' authorization to %s failed: %d\n",
651 ceph_auth_proto_name(ac->protocol),
652 ceph_entity_type_name(peer_type), result);
654 if (ac->ops->invalidate_authorizer)
655 ac->ops->invalidate_authorizer(ac, peer_type);
657 mutex_unlock(&ac->mutex);
661 mutex_unlock(&ac->mutex);
664 EXPORT_SYMBOL(ceph_auth_handle_bad_authorizer);