1 #include <linux/ceph/ceph_debug.h>
3 #include <linux/module.h>
5 #include <linux/slab.h>
7 #include <linux/ceph/types.h>
8 #include <linux/ceph/decode.h>
9 #include <linux/ceph/libceph.h>
10 #include <linux/ceph/messenger.h>
11 #include "auth_none.h"
16 * get protocol handler
18 static u32 supported_protocols[] = {
23 static int ceph_auth_init_protocol(struct ceph_auth_client *ac, int protocol)
27 return ceph_auth_none_init(ac);
29 return ceph_x_init(ac);
38 struct ceph_auth_client *ceph_auth_init(const char *name, const struct ceph_crypto_key *key)
40 struct ceph_auth_client *ac;
43 dout("auth_init name '%s'\n", name);
46 ac = kzalloc(sizeof(*ac), GFP_NOFS);
50 mutex_init(&ac->mutex);
51 ac->negotiating = true;
55 ac->name = CEPH_AUTH_NAME_DEFAULT;
56 dout("auth_init name %s\n", ac->name);
64 void ceph_auth_destroy(struct ceph_auth_client *ac)
66 dout("auth_destroy %p\n", ac);
73 * Reset occurs when reconnecting to the monitor.
75 void ceph_auth_reset(struct ceph_auth_client *ac)
77 mutex_lock(&ac->mutex);
78 dout("auth_reset %p\n", ac);
79 if (ac->ops && !ac->negotiating)
81 ac->negotiating = true;
82 mutex_unlock(&ac->mutex);
85 int ceph_entity_name_encode(const char *name, void **p, void *end)
87 int len = strlen(name);
89 if (*p + 2*sizeof(u32) + len > end)
91 ceph_encode_32(p, CEPH_ENTITY_TYPE_CLIENT);
92 ceph_encode_32(p, len);
93 ceph_encode_copy(p, name, len);
98 * Initiate protocol negotiation with monitor. Include entity name
99 * and list supported protocols.
101 int ceph_auth_build_hello(struct ceph_auth_client *ac, void *buf, size_t len)
103 struct ceph_mon_request_header *monhdr = buf;
104 void *p = monhdr + 1, *end = buf + len, *lenp;
108 mutex_lock(&ac->mutex);
109 dout("auth_build_hello\n");
110 monhdr->have_version = 0;
111 monhdr->session_mon = cpu_to_le16(-1);
112 monhdr->session_mon_tid = 0;
114 ceph_encode_32(&p, 0); /* no protocol, yet */
119 ceph_decode_need(&p, end, 1 + sizeof(u32), bad);
120 ceph_encode_8(&p, 1);
121 num = ARRAY_SIZE(supported_protocols);
122 ceph_encode_32(&p, num);
123 ceph_decode_need(&p, end, num * sizeof(u32), bad);
124 for (i = 0; i < num; i++)
125 ceph_encode_32(&p, supported_protocols[i]);
127 ret = ceph_entity_name_encode(ac->name, &p, end);
130 ceph_decode_need(&p, end, sizeof(u64), bad);
131 ceph_encode_64(&p, ac->global_id);
133 ceph_encode_32(&lenp, p - lenp - sizeof(u32));
136 mutex_unlock(&ac->mutex);
144 static int ceph_build_auth_request(struct ceph_auth_client *ac,
145 void *msg_buf, size_t msg_len)
147 struct ceph_mon_request_header *monhdr = msg_buf;
148 void *p = monhdr + 1;
149 void *end = msg_buf + msg_len;
152 monhdr->have_version = 0;
153 monhdr->session_mon = cpu_to_le16(-1);
154 monhdr->session_mon_tid = 0;
156 ceph_encode_32(&p, ac->protocol);
158 ret = ac->ops->build_request(ac, p + sizeof(u32), end);
160 pr_err("error %d building auth method %s request\n", ret,
164 dout(" built request %d bytes\n", ret);
165 ceph_encode_32(&p, ret);
166 ret = p + ret - msg_buf;
172 * Handle auth message from monitor.
174 int ceph_handle_auth_reply(struct ceph_auth_client *ac,
175 void *buf, size_t len,
176 void *reply_buf, size_t reply_len)
179 void *end = buf + len;
183 void *payload, *payload_end;
189 mutex_lock(&ac->mutex);
190 dout("handle_auth_reply %p %p\n", p, end);
191 ceph_decode_need(&p, end, sizeof(u32) * 3 + sizeof(u64), bad);
192 protocol = ceph_decode_32(&p);
193 result = ceph_decode_32(&p);
194 global_id = ceph_decode_64(&p);
195 payload_len = ceph_decode_32(&p);
198 ceph_decode_need(&p, end, sizeof(u32), bad);
199 result_msg_len = ceph_decode_32(&p);
205 dout(" result %d '%.*s' gid %llu len %d\n", result, result_msg_len,
206 result_msg, global_id, payload_len);
208 payload_end = payload + payload_len;
210 if (global_id && ac->global_id != global_id) {
211 dout(" set global_id %lld -> %lld\n", ac->global_id, global_id);
212 ac->global_id = global_id;
215 if (ac->negotiating) {
216 /* server does not support our protocols? */
217 if (!protocol && result < 0) {
221 /* set up (new) protocol handler? */
222 if (ac->protocol && ac->protocol != protocol) {
223 ac->ops->destroy(ac);
227 if (ac->protocol != protocol) {
228 ret = ceph_auth_init_protocol(ac, protocol);
230 pr_err("error %d on auth protocol %d init\n",
236 ac->negotiating = false;
239 ret = ac->ops->handle_reply(ac, result, payload, payload_end);
240 if (ret == -EAGAIN) {
241 ret = ceph_build_auth_request(ac, reply_buf, reply_len);
243 pr_err("auth method '%s' error %d\n", ac->ops->name, ret);
247 mutex_unlock(&ac->mutex);
251 pr_err("failed to decode auth msg\n");
256 int ceph_build_auth(struct ceph_auth_client *ac,
257 void *msg_buf, size_t msg_len)
261 mutex_lock(&ac->mutex);
263 ret = ceph_auth_build_hello(ac, msg_buf, msg_len);
264 else if (ac->ops->should_authenticate(ac))
265 ret = ceph_build_auth_request(ac, msg_buf, msg_len);
266 mutex_unlock(&ac->mutex);
270 int ceph_auth_is_authenticated(struct ceph_auth_client *ac)
274 mutex_lock(&ac->mutex);
276 ret = ac->ops->is_authenticated(ac);
277 mutex_unlock(&ac->mutex);
280 EXPORT_SYMBOL(ceph_auth_is_authenticated);
282 int ceph_auth_create_authorizer(struct ceph_auth_client *ac,
284 struct ceph_auth_handshake *auth)
288 mutex_lock(&ac->mutex);
289 if (ac->ops && ac->ops->create_authorizer)
290 ret = ac->ops->create_authorizer(ac, peer_type, auth);
291 mutex_unlock(&ac->mutex);
294 EXPORT_SYMBOL(ceph_auth_create_authorizer);
296 void ceph_auth_destroy_authorizer(struct ceph_auth_client *ac,
297 struct ceph_authorizer *a)
299 mutex_lock(&ac->mutex);
300 if (ac->ops && ac->ops->destroy_authorizer)
301 ac->ops->destroy_authorizer(ac, a);
302 mutex_unlock(&ac->mutex);
304 EXPORT_SYMBOL(ceph_auth_destroy_authorizer);
306 int ceph_auth_update_authorizer(struct ceph_auth_client *ac,
308 struct ceph_auth_handshake *a)
312 mutex_lock(&ac->mutex);
313 if (ac->ops && ac->ops->update_authorizer)
314 ret = ac->ops->update_authorizer(ac, peer_type, a);
315 mutex_unlock(&ac->mutex);
318 EXPORT_SYMBOL(ceph_auth_update_authorizer);
320 int ceph_auth_verify_authorizer_reply(struct ceph_auth_client *ac,
321 struct ceph_authorizer *a, size_t len)
325 mutex_lock(&ac->mutex);
326 if (ac->ops && ac->ops->verify_authorizer_reply)
327 ret = ac->ops->verify_authorizer_reply(ac, a, len);
328 mutex_unlock(&ac->mutex);
331 EXPORT_SYMBOL(ceph_auth_verify_authorizer_reply);
333 void ceph_auth_invalidate_authorizer(struct ceph_auth_client *ac, int peer_type)
335 mutex_lock(&ac->mutex);
336 if (ac->ops && ac->ops->invalidate_authorizer)
337 ac->ops->invalidate_authorizer(ac, peer_type);
338 mutex_unlock(&ac->mutex);
340 EXPORT_SYMBOL(ceph_auth_invalidate_authorizer);