xref: /linux/net/ceph/auth.c (revision 00498b994113a871a556f7ff24a4cf8a00611700)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ceph/ceph_debug.h>
3 
4 #include <linux/module.h>
5 #include <linux/err.h>
6 #include <linux/slab.h>
7 
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"
13 #include "auth_x.h"
14 
15 
16 /*
17  * get protocol handler
18  */
19 static u32 supported_protocols[] = {
20 	CEPH_AUTH_NONE,
21 	CEPH_AUTH_CEPHX
22 };
23 
24 static int init_protocol(struct ceph_auth_client *ac, int proto)
25 {
26 	dout("%s proto %d\n", __func__, proto);
27 
28 	switch (proto) {
29 	case CEPH_AUTH_NONE:
30 		return ceph_auth_none_init(ac);
31 	case CEPH_AUTH_CEPHX:
32 		return ceph_x_init(ac);
33 	default:
34 		pr_err("bad auth protocol %d\n", proto);
35 		return -EINVAL;
36 	}
37 }
38 
39 /*
40  * setup, teardown.
41  */
42 struct ceph_auth_client *ceph_auth_init(const char *name,
43 					const struct ceph_crypto_key *key,
44 					const int *con_modes)
45 {
46 	struct ceph_auth_client *ac;
47 	int ret;
48 
49 	ret = -ENOMEM;
50 	ac = kzalloc(sizeof(*ac), GFP_NOFS);
51 	if (!ac)
52 		goto out;
53 
54 	mutex_init(&ac->mutex);
55 	ac->negotiating = true;
56 	if (name)
57 		ac->name = name;
58 	else
59 		ac->name = CEPH_AUTH_NAME_DEFAULT;
60 	ac->key = key;
61 	ac->preferred_mode = con_modes[0];
62 	ac->fallback_mode = con_modes[1];
63 
64 	dout("%s name '%s' preferred_mode %d fallback_mode %d\n", __func__,
65 	     ac->name, ac->preferred_mode, ac->fallback_mode);
66 	return ac;
67 
68 out:
69 	return ERR_PTR(ret);
70 }
71 
72 void ceph_auth_destroy(struct ceph_auth_client *ac)
73 {
74 	dout("auth_destroy %p\n", ac);
75 	if (ac->ops)
76 		ac->ops->destroy(ac);
77 	kfree(ac);
78 }
79 
80 /*
81  * Reset occurs when reconnecting to the monitor.
82  */
83 void ceph_auth_reset(struct ceph_auth_client *ac)
84 {
85 	mutex_lock(&ac->mutex);
86 	dout("auth_reset %p\n", ac);
87 	if (ac->ops && !ac->negotiating)
88 		ac->ops->reset(ac);
89 	ac->negotiating = true;
90 	mutex_unlock(&ac->mutex);
91 }
92 
93 /*
94  * EntityName, not to be confused with entity_name_t
95  */
96 int ceph_auth_entity_name_encode(const char *name, void **p, void *end)
97 {
98 	int len = strlen(name);
99 
100 	if (*p + 2*sizeof(u32) + len > end)
101 		return -ERANGE;
102 	ceph_encode_32(p, CEPH_ENTITY_TYPE_CLIENT);
103 	ceph_encode_32(p, len);
104 	ceph_encode_copy(p, name, len);
105 	return 0;
106 }
107 
108 /*
109  * Initiate protocol negotiation with monitor.  Include entity name
110  * and list supported protocols.
111  */
112 int ceph_auth_build_hello(struct ceph_auth_client *ac, void *buf, size_t len)
113 {
114 	struct ceph_mon_request_header *monhdr = buf;
115 	void *p = monhdr + 1, *end = buf + len, *lenp;
116 	int i, num;
117 	int ret;
118 
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;
124 
125 	ceph_encode_32(&p, CEPH_AUTH_UNKNOWN);  /* no protocol, yet */
126 
127 	lenp = p;
128 	p += sizeof(u32);
129 
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]);
137 
138 	ret = ceph_auth_entity_name_encode(ac->name, &p, end);
139 	if (ret < 0)
140 		goto out;
141 	ceph_decode_need(&p, end, sizeof(u64), bad);
142 	ceph_encode_64(&p, ac->global_id);
143 
144 	ceph_encode_32(&lenp, p - lenp - sizeof(u32));
145 	ret = p - buf;
146 out:
147 	mutex_unlock(&ac->mutex);
148 	return ret;
149 
150 bad:
151 	ret = -ERANGE;
152 	goto out;
153 }
154 
155 static int build_request(struct ceph_auth_client *ac, bool add_header,
156 			 void *buf, int buf_len)
157 {
158 	void *end = buf + buf_len;
159 	void *p;
160 	int ret;
161 
162 	p = buf;
163 	if (add_header) {
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);
169 	}
170 
171 	ceph_encode_need(&p, end, sizeof(u32), e_range);
172 	ret = ac->ops->build_request(ac, p + sizeof(u32), end);
173 	if (ret < 0) {
174 		pr_err("auth protocol '%s' building request failed: %d\n",
175 		       ceph_auth_proto_name(ac->protocol), ret);
176 		return ret;
177 	}
178 	dout(" built request %d bytes\n", ret);
179 	ceph_encode_32(&p, ret);
180 	return p + ret - buf;
181 
182 e_range:
183 	return -ERANGE;
184 }
185 
186 /*
187  * Handle auth message from monitor.
188  */
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)
192 {
193 	void *p = buf;
194 	void *end = buf + len;
195 	int protocol;
196 	s32 result;
197 	u64 global_id;
198 	void *payload, *payload_end;
199 	int payload_len;
200 	char *result_msg;
201 	int result_msg_len;
202 	int ret = -EINVAL;
203 
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);
211 	payload = p;
212 	p += payload_len;
213 	ceph_decode_need(&p, end, sizeof(u32), bad);
214 	result_msg_len = ceph_decode_32(&p);
215 	result_msg = p;
216 	p += result_msg_len;
217 	if (p != end)
218 		goto bad;
219 
220 	dout(" result %d '%.*s' gid %llu len %d\n", result, result_msg_len,
221 	     result_msg, global_id, payload_len);
222 
223 	payload_end = payload + payload_len;
224 
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;
228 	}
229 
230 	if (ac->negotiating) {
231 		/* server does not support our protocols? */
232 		if (!protocol && result < 0) {
233 			ret = result;
234 			goto out;
235 		}
236 		/* set up (new) protocol handler? */
237 		if (ac->protocol && ac->protocol != protocol) {
238 			ac->ops->destroy(ac);
239 			ac->protocol = 0;
240 			ac->ops = NULL;
241 		}
242 		if (ac->protocol != protocol) {
243 			ret = init_protocol(ac, protocol);
244 			if (ret) {
245 				pr_err("auth protocol '%s' init failed: %d\n",
246 				       ceph_auth_proto_name(protocol), ret);
247 				goto out;
248 			}
249 		}
250 
251 		ac->negotiating = false;
252 	}
253 
254 	ret = ac->ops->handle_reply(ac, result, payload, payload_end,
255 				    NULL, NULL, NULL, NULL);
256 	if (ret == -EAGAIN)
257 		ret = build_request(ac, true, reply_buf, reply_len);
258 	else if (ret)
259 		pr_err("auth protocol '%s' mauth authentication failed: %d\n",
260 		       ceph_auth_proto_name(ac->protocol), result);
261 
262 out:
263 	mutex_unlock(&ac->mutex);
264 	return ret;
265 
266 bad:
267 	pr_err("failed to decode auth msg\n");
268 	ret = -EINVAL;
269 	goto out;
270 }
271 
272 int ceph_build_auth(struct ceph_auth_client *ac,
273 		    void *msg_buf, size_t msg_len)
274 {
275 	int ret = 0;
276 
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);
281 	return ret;
282 }
283 
284 int ceph_auth_is_authenticated(struct ceph_auth_client *ac)
285 {
286 	int ret = 0;
287 
288 	mutex_lock(&ac->mutex);
289 	if (ac->ops)
290 		ret = ac->ops->is_authenticated(ac);
291 	mutex_unlock(&ac->mutex);
292 	return ret;
293 }
294 EXPORT_SYMBOL(ceph_auth_is_authenticated);
295 
296 int ceph_auth_create_authorizer(struct ceph_auth_client *ac,
297 				int peer_type,
298 				struct ceph_auth_handshake *auth)
299 {
300 	int ret = 0;
301 
302 	mutex_lock(&ac->mutex);
303 	if (ac->ops && ac->ops->create_authorizer)
304 		ret = ac->ops->create_authorizer(ac, peer_type, auth);
305 	mutex_unlock(&ac->mutex);
306 	return ret;
307 }
308 EXPORT_SYMBOL(ceph_auth_create_authorizer);
309 
310 void ceph_auth_destroy_authorizer(struct ceph_authorizer *a)
311 {
312 	a->destroy(a);
313 }
314 EXPORT_SYMBOL(ceph_auth_destroy_authorizer);
315 
316 int ceph_auth_update_authorizer(struct ceph_auth_client *ac,
317 				int peer_type,
318 				struct ceph_auth_handshake *a)
319 {
320 	int ret = 0;
321 
322 	mutex_lock(&ac->mutex);
323 	if (ac->ops && ac->ops->update_authorizer)
324 		ret = ac->ops->update_authorizer(ac, peer_type, a);
325 	mutex_unlock(&ac->mutex);
326 	return ret;
327 }
328 EXPORT_SYMBOL(ceph_auth_update_authorizer);
329 
330 int ceph_auth_add_authorizer_challenge(struct ceph_auth_client *ac,
331 				       struct ceph_authorizer *a,
332 				       void *challenge_buf,
333 				       int challenge_buf_len)
334 {
335 	int ret = 0;
336 
337 	mutex_lock(&ac->mutex);
338 	if (ac->ops && ac->ops->add_authorizer_challenge)
339 		ret = ac->ops->add_authorizer_challenge(ac, a, challenge_buf,
340 							challenge_buf_len);
341 	mutex_unlock(&ac->mutex);
342 	return ret;
343 }
344 EXPORT_SYMBOL(ceph_auth_add_authorizer_challenge);
345 
346 int ceph_auth_verify_authorizer_reply(struct ceph_auth_client *ac,
347 				      struct ceph_authorizer *a,
348 				      void *reply, int reply_len,
349 				      u8 *session_key, int *session_key_len,
350 				      u8 *con_secret, int *con_secret_len)
351 {
352 	int ret = 0;
353 
354 	mutex_lock(&ac->mutex);
355 	if (ac->ops && ac->ops->verify_authorizer_reply)
356 		ret = ac->ops->verify_authorizer_reply(ac, a,
357 			reply, reply_len, session_key, session_key_len,
358 			con_secret, con_secret_len);
359 	mutex_unlock(&ac->mutex);
360 	return ret;
361 }
362 EXPORT_SYMBOL(ceph_auth_verify_authorizer_reply);
363 
364 void ceph_auth_invalidate_authorizer(struct ceph_auth_client *ac, int peer_type)
365 {
366 	mutex_lock(&ac->mutex);
367 	if (ac->ops && ac->ops->invalidate_authorizer)
368 		ac->ops->invalidate_authorizer(ac, peer_type);
369 	mutex_unlock(&ac->mutex);
370 }
371 EXPORT_SYMBOL(ceph_auth_invalidate_authorizer);
372