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);
42 struct ceph_auth_client *ceph_auth_init(const char *name,
43 const struct ceph_crypto_key *key,
46 struct ceph_auth_client *ac;
50 ac = kzalloc(sizeof(*ac), GFP_NOFS);
54 mutex_init(&ac->mutex);
55 ac->negotiating = true;
59 ac->name = CEPH_AUTH_NAME_DEFAULT;
61 ac->preferred_mode = con_modes[0];
62 ac->fallback_mode = con_modes[1];
64 dout("%s name '%s' preferred_mode %d fallback_mode %d\n", __func__,
65 ac->name, ac->preferred_mode, ac->fallback_mode);
72 void ceph_auth_destroy(struct ceph_auth_client *ac)
74 dout("auth_destroy %p\n", ac);
81 * Reset occurs when reconnecting to the monitor.
83 void ceph_auth_reset(struct ceph_auth_client *ac)
85 mutex_lock(&ac->mutex);
86 dout("auth_reset %p\n", ac);
87 if (ac->ops && !ac->negotiating)
89 ac->negotiating = true;
90 mutex_unlock(&ac->mutex);
94 * EntityName, not to be confused with entity_name_t
96 int ceph_auth_entity_name_encode(const char *name, void **p, void *end)
98 int len = strlen(name);
100 if (*p + 2*sizeof(u32) + len > end)
102 ceph_encode_32(p, CEPH_ENTITY_TYPE_CLIENT);
103 ceph_encode_32(p, len);
104 ceph_encode_copy(p, name, len);
109 * Initiate protocol negotiation with monitor. Include entity name
110 * and list supported protocols.
112 int ceph_auth_build_hello(struct ceph_auth_client *ac, void *buf, size_t len)
114 struct ceph_mon_request_header *monhdr = buf;
115 void *p = monhdr + 1, *end = buf + len, *lenp;
119 mutex_lock(&ac->mutex);
120 dout("auth_build_hello\n");
121 monhdr->have_version = 0;
122 monhdr->session_mon = cpu_to_le16(-1);
123 monhdr->session_mon_tid = 0;
125 ceph_encode_32(&p, CEPH_AUTH_UNKNOWN); /* no protocol, yet */
130 ceph_decode_need(&p, end, 1 + sizeof(u32), bad);
131 ceph_encode_8(&p, 1);
132 num = ARRAY_SIZE(supported_protocols);
133 ceph_encode_32(&p, num);
134 ceph_decode_need(&p, end, num * sizeof(u32), bad);
135 for (i = 0; i < num; i++)
136 ceph_encode_32(&p, supported_protocols[i]);
138 ret = ceph_auth_entity_name_encode(ac->name, &p, end);
141 ceph_decode_need(&p, end, sizeof(u64), bad);
142 ceph_encode_64(&p, ac->global_id);
144 ceph_encode_32(&lenp, p - lenp - sizeof(u32));
147 mutex_unlock(&ac->mutex);
155 static int build_request(struct ceph_auth_client *ac, bool add_header,
156 void *buf, int buf_len)
158 void *end = buf + buf_len;
164 /* struct ceph_mon_request_header + protocol */
165 ceph_encode_64_safe(&p, end, 0, e_range);
166 ceph_encode_16_safe(&p, end, -1, e_range);
167 ceph_encode_64_safe(&p, end, 0, e_range);
168 ceph_encode_32_safe(&p, end, ac->protocol, e_range);
171 ceph_encode_need(&p, end, sizeof(u32), e_range);
172 ret = ac->ops->build_request(ac, p + sizeof(u32), end);
174 pr_err("auth protocol '%s' building request failed: %d\n",
175 ceph_auth_proto_name(ac->protocol), ret);
178 dout(" built request %d bytes\n", ret);
179 ceph_encode_32(&p, ret);
180 return p + ret - buf;
187 * Handle auth message from monitor.
189 int ceph_handle_auth_reply(struct ceph_auth_client *ac,
190 void *buf, size_t len,
191 void *reply_buf, size_t reply_len)
194 void *end = buf + len;
198 void *payload, *payload_end;
204 mutex_lock(&ac->mutex);
205 dout("handle_auth_reply %p %p\n", p, end);
206 ceph_decode_need(&p, end, sizeof(u32) * 3 + sizeof(u64), bad);
207 protocol = ceph_decode_32(&p);
208 result = ceph_decode_32(&p);
209 global_id = ceph_decode_64(&p);
210 payload_len = ceph_decode_32(&p);
213 ceph_decode_need(&p, end, sizeof(u32), bad);
214 result_msg_len = ceph_decode_32(&p);
220 dout(" result %d '%.*s' gid %llu len %d\n", result, result_msg_len,
221 result_msg, global_id, payload_len);
223 payload_end = payload + payload_len;
225 if (global_id && ac->global_id != global_id) {
226 dout(" set global_id %lld -> %lld\n", ac->global_id, global_id);
227 ac->global_id = global_id;
230 if (ac->negotiating) {
231 /* server does not support our protocols? */
232 if (!protocol && result < 0) {
236 /* set up (new) protocol handler? */
237 if (ac->protocol && ac->protocol != protocol) {
238 ac->ops->destroy(ac);
242 if (ac->protocol != protocol) {
243 ret = init_protocol(ac, protocol);
245 pr_err("auth protocol '%s' init failed: %d\n",
246 ceph_auth_proto_name(protocol), ret);
251 ac->negotiating = false;
254 ret = ac->ops->handle_reply(ac, result, payload, payload_end,
255 NULL, NULL, NULL, NULL);
257 ret = build_request(ac, true, reply_buf, reply_len);
259 pr_err("auth protocol '%s' mauth authentication failed: %d\n",
260 ceph_auth_proto_name(ac->protocol), result);
263 mutex_unlock(&ac->mutex);
267 pr_err("failed to decode auth msg\n");
272 int ceph_build_auth(struct ceph_auth_client *ac,
273 void *msg_buf, size_t msg_len)
277 mutex_lock(&ac->mutex);
278 if (ac->ops->should_authenticate(ac))
279 ret = build_request(ac, true, msg_buf, msg_len);
280 mutex_unlock(&ac->mutex);
284 int ceph_auth_is_authenticated(struct ceph_auth_client *ac)
288 mutex_lock(&ac->mutex);
290 ret = ac->ops->is_authenticated(ac);
291 mutex_unlock(&ac->mutex);
294 EXPORT_SYMBOL(ceph_auth_is_authenticated);
296 int __ceph_auth_get_authorizer(struct ceph_auth_client *ac,
297 struct ceph_auth_handshake *auth,
298 int peer_type, bool force_new,
299 int *proto, int *pref_mode, int *fallb_mode)
303 mutex_lock(&ac->mutex);
304 if (force_new && auth->authorizer) {
305 ceph_auth_destroy_authorizer(auth->authorizer);
306 auth->authorizer = NULL;
308 if (!auth->authorizer)
309 ret = ac->ops->create_authorizer(ac, peer_type, auth);
310 else if (ac->ops->update_authorizer)
311 ret = ac->ops->update_authorizer(ac, peer_type, auth);
317 *proto = ac->protocol;
318 if (pref_mode && fallb_mode) {
319 *pref_mode = ac->preferred_mode;
320 *fallb_mode = ac->fallback_mode;
324 mutex_unlock(&ac->mutex);
327 EXPORT_SYMBOL(__ceph_auth_get_authorizer);
329 int ceph_auth_create_authorizer(struct ceph_auth_client *ac,
331 struct ceph_auth_handshake *auth)
335 mutex_lock(&ac->mutex);
336 if (ac->ops && ac->ops->create_authorizer)
337 ret = ac->ops->create_authorizer(ac, peer_type, auth);
338 mutex_unlock(&ac->mutex);
341 EXPORT_SYMBOL(ceph_auth_create_authorizer);
343 void ceph_auth_destroy_authorizer(struct ceph_authorizer *a)
347 EXPORT_SYMBOL(ceph_auth_destroy_authorizer);
349 int ceph_auth_update_authorizer(struct ceph_auth_client *ac,
351 struct ceph_auth_handshake *a)
355 mutex_lock(&ac->mutex);
356 if (ac->ops && ac->ops->update_authorizer)
357 ret = ac->ops->update_authorizer(ac, peer_type, a);
358 mutex_unlock(&ac->mutex);
361 EXPORT_SYMBOL(ceph_auth_update_authorizer);
363 int ceph_auth_add_authorizer_challenge(struct ceph_auth_client *ac,
364 struct ceph_authorizer *a,
366 int challenge_buf_len)
370 mutex_lock(&ac->mutex);
371 if (ac->ops && ac->ops->add_authorizer_challenge)
372 ret = ac->ops->add_authorizer_challenge(ac, a, challenge_buf,
374 mutex_unlock(&ac->mutex);
377 EXPORT_SYMBOL(ceph_auth_add_authorizer_challenge);
379 int ceph_auth_verify_authorizer_reply(struct ceph_auth_client *ac,
380 struct ceph_authorizer *a,
381 void *reply, int reply_len,
382 u8 *session_key, int *session_key_len,
383 u8 *con_secret, int *con_secret_len)
387 mutex_lock(&ac->mutex);
388 if (ac->ops && ac->ops->verify_authorizer_reply)
389 ret = ac->ops->verify_authorizer_reply(ac, a,
390 reply, reply_len, session_key, session_key_len,
391 con_secret, con_secret_len);
392 mutex_unlock(&ac->mutex);
395 EXPORT_SYMBOL(ceph_auth_verify_authorizer_reply);
397 void ceph_auth_invalidate_authorizer(struct ceph_auth_client *ac, int peer_type)
399 mutex_lock(&ac->mutex);
400 if (ac->ops && ac->ops->invalidate_authorizer)
401 ac->ops->invalidate_authorizer(ac, peer_type);
402 mutex_unlock(&ac->mutex);
404 EXPORT_SYMBOL(ceph_auth_invalidate_authorizer);
407 * msgr2 authentication
410 static bool contains(const int *arr, int cnt, int val)
414 for (i = 0; i < cnt; i++) {
422 static int encode_con_modes(void **p, void *end, int pref_mode, int fallb_mode)
424 WARN_ON(pref_mode == CEPH_CON_MODE_UNKNOWN);
425 if (fallb_mode != CEPH_CON_MODE_UNKNOWN) {
426 ceph_encode_32_safe(p, end, 2, e_range);
427 ceph_encode_32_safe(p, end, pref_mode, e_range);
428 ceph_encode_32_safe(p, end, fallb_mode, e_range);
430 ceph_encode_32_safe(p, end, 1, e_range);
431 ceph_encode_32_safe(p, end, pref_mode, e_range);
441 * Similar to ceph_auth_build_hello().
443 int ceph_auth_get_request(struct ceph_auth_client *ac, void *buf, int buf_len)
445 int proto = ac->key ? CEPH_AUTH_CEPHX : CEPH_AUTH_NONE;
446 void *end = buf + buf_len;
451 mutex_lock(&ac->mutex);
452 if (ac->protocol == CEPH_AUTH_UNKNOWN) {
453 ret = init_protocol(ac, proto);
455 pr_err("auth protocol '%s' init failed: %d\n",
456 ceph_auth_proto_name(proto), ret);
460 WARN_ON(ac->protocol != proto);
465 ceph_encode_32_safe(&p, end, ac->protocol, e_range);
466 ret = encode_con_modes(&p, end, ac->preferred_mode, ac->fallback_mode);
471 p += 4; /* space for len */
473 ceph_encode_8_safe(&p, end, CEPH_AUTH_MODE_MON, e_range);
474 ret = ceph_auth_entity_name_encode(ac->name, &p, end);
478 ceph_encode_64_safe(&p, end, ac->global_id, e_range);
479 ceph_encode_32(&lenp, p - lenp - 4);
483 mutex_unlock(&ac->mutex);
491 int ceph_auth_handle_reply_more(struct ceph_auth_client *ac, void *reply,
492 int reply_len, void *buf, int buf_len)
496 mutex_lock(&ac->mutex);
497 ret = ac->ops->handle_reply(ac, 0, reply, reply + reply_len,
498 NULL, NULL, NULL, NULL);
500 ret = build_request(ac, false, buf, buf_len);
503 mutex_unlock(&ac->mutex);
507 int ceph_auth_handle_reply_done(struct ceph_auth_client *ac,
508 u64 global_id, void *reply, int reply_len,
509 u8 *session_key, int *session_key_len,
510 u8 *con_secret, int *con_secret_len)
514 mutex_lock(&ac->mutex);
515 if (global_id && ac->global_id != global_id) {
516 dout("%s global_id %llu -> %llu\n", __func__, ac->global_id,
518 ac->global_id = global_id;
521 ret = ac->ops->handle_reply(ac, 0, reply, reply + reply_len,
522 session_key, session_key_len,
523 con_secret, con_secret_len);
524 mutex_unlock(&ac->mutex);
528 bool ceph_auth_handle_bad_method(struct ceph_auth_client *ac,
529 int used_proto, int result,
530 const int *allowed_protos, int proto_cnt,
531 const int *allowed_modes, int mode_cnt)
533 mutex_lock(&ac->mutex);
534 WARN_ON(used_proto != ac->protocol);
536 if (result == -EOPNOTSUPP) {
537 if (!contains(allowed_protos, proto_cnt, ac->protocol)) {
538 pr_err("auth protocol '%s' not allowed\n",
539 ceph_auth_proto_name(ac->protocol));
542 if (!contains(allowed_modes, mode_cnt, ac->preferred_mode) &&
543 (ac->fallback_mode == CEPH_CON_MODE_UNKNOWN ||
544 !contains(allowed_modes, mode_cnt, ac->fallback_mode))) {
545 pr_err("preferred mode '%s' not allowed\n",
546 ceph_con_mode_name(ac->preferred_mode));
547 if (ac->fallback_mode == CEPH_CON_MODE_UNKNOWN)
548 pr_err("no fallback mode\n");
550 pr_err("fallback mode '%s' not allowed\n",
551 ceph_con_mode_name(ac->fallback_mode));
556 WARN_ON(result == -EOPNOTSUPP || result >= 0);
557 pr_err("auth protocol '%s' msgr authentication failed: %d\n",
558 ceph_auth_proto_name(ac->protocol), result);
560 mutex_unlock(&ac->mutex);
564 mutex_unlock(&ac->mutex);
568 int ceph_auth_get_authorizer(struct ceph_auth_client *ac,
569 struct ceph_auth_handshake *auth,
570 int peer_type, void *buf, int *buf_len)
572 void *end = buf + *buf_len;
573 int pref_mode, fallb_mode;
578 ret = __ceph_auth_get_authorizer(ac, auth, peer_type, true, &proto,
579 &pref_mode, &fallb_mode);
584 ceph_encode_32_safe(&p, end, proto, e_range);
585 ret = encode_con_modes(&p, end, pref_mode, fallb_mode);
589 ceph_encode_32_safe(&p, end, auth->authorizer_buf_len, e_range);
596 EXPORT_SYMBOL(ceph_auth_get_authorizer);
598 int ceph_auth_handle_svc_reply_more(struct ceph_auth_client *ac,
599 struct ceph_auth_handshake *auth,
600 void *reply, int reply_len,
601 void *buf, int *buf_len)
603 void *end = buf + *buf_len;
607 ret = ceph_auth_add_authorizer_challenge(ac, auth->authorizer,
613 ceph_encode_32_safe(&p, end, auth->authorizer_buf_len, e_range);
620 EXPORT_SYMBOL(ceph_auth_handle_svc_reply_more);
622 int ceph_auth_handle_svc_reply_done(struct ceph_auth_client *ac,
623 struct ceph_auth_handshake *auth,
624 void *reply, int reply_len,
625 u8 *session_key, int *session_key_len,
626 u8 *con_secret, int *con_secret_len)
628 return ceph_auth_verify_authorizer_reply(ac, auth->authorizer,
629 reply, reply_len, session_key, session_key_len,
630 con_secret, con_secret_len);
632 EXPORT_SYMBOL(ceph_auth_handle_svc_reply_done);
634 bool ceph_auth_handle_bad_authorizer(struct ceph_auth_client *ac,
635 int peer_type, int used_proto, int result,
636 const int *allowed_protos, int proto_cnt,
637 const int *allowed_modes, int mode_cnt)
639 mutex_lock(&ac->mutex);
640 WARN_ON(used_proto != ac->protocol);
642 if (result == -EOPNOTSUPP) {
643 if (!contains(allowed_protos, proto_cnt, ac->protocol)) {
644 pr_err("auth protocol '%s' not allowed by %s\n",
645 ceph_auth_proto_name(ac->protocol),
646 ceph_entity_type_name(peer_type));
649 if (!contains(allowed_modes, mode_cnt, ac->preferred_mode) &&
650 (ac->fallback_mode == CEPH_CON_MODE_UNKNOWN ||
651 !contains(allowed_modes, mode_cnt, ac->fallback_mode))) {
652 pr_err("preferred mode '%s' not allowed by %s\n",
653 ceph_con_mode_name(ac->preferred_mode),
654 ceph_entity_type_name(peer_type));
655 if (ac->fallback_mode == CEPH_CON_MODE_UNKNOWN)
656 pr_err("no fallback mode\n");
658 pr_err("fallback mode '%s' not allowed by %s\n",
659 ceph_con_mode_name(ac->fallback_mode),
660 ceph_entity_type_name(peer_type));
665 WARN_ON(result == -EOPNOTSUPP || result >= 0);
666 pr_err("auth protocol '%s' authorization to %s failed: %d\n",
667 ceph_auth_proto_name(ac->protocol),
668 ceph_entity_type_name(peer_type), result);
670 if (ac->ops->invalidate_authorizer)
671 ac->ops->invalidate_authorizer(ac, peer_type);
673 mutex_unlock(&ac->mutex);
677 mutex_unlock(&ac->mutex);
680 EXPORT_SYMBOL(ceph_auth_handle_bad_authorizer);