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);
86 * EntityName, not to be confused with entity_name_t
88 int ceph_auth_entity_name_encode(const char *name, void **p, void *end)
90 int len = strlen(name);
92 if (*p + 2*sizeof(u32) + len > end)
94 ceph_encode_32(p, CEPH_ENTITY_TYPE_CLIENT);
95 ceph_encode_32(p, len);
96 ceph_encode_copy(p, name, len);
101 * Initiate protocol negotiation with monitor. Include entity name
102 * and list supported protocols.
104 int ceph_auth_build_hello(struct ceph_auth_client *ac, void *buf, size_t len)
106 struct ceph_mon_request_header *monhdr = buf;
107 void *p = monhdr + 1, *end = buf + len, *lenp;
111 mutex_lock(&ac->mutex);
112 dout("auth_build_hello\n");
113 monhdr->have_version = 0;
114 monhdr->session_mon = cpu_to_le16(-1);
115 monhdr->session_mon_tid = 0;
117 ceph_encode_32(&p, CEPH_AUTH_UNKNOWN); /* no protocol, yet */
122 ceph_decode_need(&p, end, 1 + sizeof(u32), bad);
123 ceph_encode_8(&p, 1);
124 num = ARRAY_SIZE(supported_protocols);
125 ceph_encode_32(&p, num);
126 ceph_decode_need(&p, end, num * sizeof(u32), bad);
127 for (i = 0; i < num; i++)
128 ceph_encode_32(&p, supported_protocols[i]);
130 ret = ceph_auth_entity_name_encode(ac->name, &p, end);
133 ceph_decode_need(&p, end, sizeof(u64), bad);
134 ceph_encode_64(&p, ac->global_id);
136 ceph_encode_32(&lenp, p - lenp - sizeof(u32));
139 mutex_unlock(&ac->mutex);
147 static int ceph_build_auth_request(struct ceph_auth_client *ac,
148 void *msg_buf, size_t msg_len)
150 struct ceph_mon_request_header *monhdr = msg_buf;
151 void *p = monhdr + 1;
152 void *end = msg_buf + msg_len;
155 monhdr->have_version = 0;
156 monhdr->session_mon = cpu_to_le16(-1);
157 monhdr->session_mon_tid = 0;
159 ceph_encode_32(&p, ac->protocol);
161 ret = ac->ops->build_request(ac, p + sizeof(u32), end);
163 pr_err("error %d building auth method %s request\n", ret,
167 dout(" built request %d bytes\n", ret);
168 ceph_encode_32(&p, ret);
169 ret = p + ret - msg_buf;
175 * Handle auth message from monitor.
177 int ceph_handle_auth_reply(struct ceph_auth_client *ac,
178 void *buf, size_t len,
179 void *reply_buf, size_t reply_len)
182 void *end = buf + len;
186 void *payload, *payload_end;
192 mutex_lock(&ac->mutex);
193 dout("handle_auth_reply %p %p\n", p, end);
194 ceph_decode_need(&p, end, sizeof(u32) * 3 + sizeof(u64), bad);
195 protocol = ceph_decode_32(&p);
196 result = ceph_decode_32(&p);
197 global_id = ceph_decode_64(&p);
198 payload_len = ceph_decode_32(&p);
201 ceph_decode_need(&p, end, sizeof(u32), bad);
202 result_msg_len = ceph_decode_32(&p);
208 dout(" result %d '%.*s' gid %llu len %d\n", result, result_msg_len,
209 result_msg, global_id, payload_len);
211 payload_end = payload + payload_len;
213 if (global_id && ac->global_id != global_id) {
214 dout(" set global_id %lld -> %lld\n", ac->global_id, global_id);
215 ac->global_id = global_id;
218 if (ac->negotiating) {
219 /* server does not support our protocols? */
220 if (!protocol && result < 0) {
224 /* set up (new) protocol handler? */
225 if (ac->protocol && ac->protocol != protocol) {
226 ac->ops->destroy(ac);
230 if (ac->protocol != protocol) {
231 ret = ceph_auth_init_protocol(ac, protocol);
233 pr_err("error %d on auth protocol %d init\n",
239 ac->negotiating = false;
242 ret = ac->ops->handle_reply(ac, result, payload, payload_end);
243 if (ret == -EAGAIN) {
244 ret = ceph_build_auth_request(ac, reply_buf, reply_len);
246 pr_err("auth method '%s' error %d\n", ac->ops->name, ret);
250 mutex_unlock(&ac->mutex);
254 pr_err("failed to decode auth msg\n");
259 int ceph_build_auth(struct ceph_auth_client *ac,
260 void *msg_buf, size_t msg_len)
264 mutex_lock(&ac->mutex);
265 if (ac->ops->should_authenticate(ac))
266 ret = ceph_build_auth_request(ac, msg_buf, msg_len);
267 mutex_unlock(&ac->mutex);
271 int ceph_auth_is_authenticated(struct ceph_auth_client *ac)
275 mutex_lock(&ac->mutex);
277 ret = ac->ops->is_authenticated(ac);
278 mutex_unlock(&ac->mutex);
281 EXPORT_SYMBOL(ceph_auth_is_authenticated);
283 int ceph_auth_create_authorizer(struct ceph_auth_client *ac,
285 struct ceph_auth_handshake *auth)
289 mutex_lock(&ac->mutex);
290 if (ac->ops && ac->ops->create_authorizer)
291 ret = ac->ops->create_authorizer(ac, peer_type, auth);
292 mutex_unlock(&ac->mutex);
295 EXPORT_SYMBOL(ceph_auth_create_authorizer);
297 void ceph_auth_destroy_authorizer(struct ceph_authorizer *a)
301 EXPORT_SYMBOL(ceph_auth_destroy_authorizer);
303 int ceph_auth_update_authorizer(struct ceph_auth_client *ac,
305 struct ceph_auth_handshake *a)
309 mutex_lock(&ac->mutex);
310 if (ac->ops && ac->ops->update_authorizer)
311 ret = ac->ops->update_authorizer(ac, peer_type, a);
312 mutex_unlock(&ac->mutex);
315 EXPORT_SYMBOL(ceph_auth_update_authorizer);
317 int ceph_auth_verify_authorizer_reply(struct ceph_auth_client *ac,
318 struct ceph_authorizer *a)
322 mutex_lock(&ac->mutex);
323 if (ac->ops && ac->ops->verify_authorizer_reply)
324 ret = ac->ops->verify_authorizer_reply(ac, a);
325 mutex_unlock(&ac->mutex);
328 EXPORT_SYMBOL(ceph_auth_verify_authorizer_reply);
330 void ceph_auth_invalidate_authorizer(struct ceph_auth_client *ac, int peer_type)
332 mutex_lock(&ac->mutex);
333 if (ac->ops && ac->ops->invalidate_authorizer)
334 ac->ops->invalidate_authorizer(ac, peer_type);
335 mutex_unlock(&ac->mutex);
337 EXPORT_SYMBOL(ceph_auth_invalidate_authorizer);