blob: c822b3ae1bd37b2ca9e4708b3d1ed6b7dd66f6c3 [file] [log] [blame]
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001#include <linux/ceph/ceph_debug.h>
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002
3#include <linux/module.h>
4#include <linux/err.h>
Sage Weild45d0d92010-04-20 15:20:33 -07005#include <linux/slab.h>
Sage Weil4e7a5dc2009-11-18 16:19:57 -08006
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07007#include <linux/ceph/types.h>
8#include <linux/ceph/decode.h>
9#include <linux/ceph/libceph.h>
10#include <linux/ceph/messenger.h>
Sage Weil4e7a5dc2009-11-18 16:19:57 -080011#include "auth_none.h"
Sage Weilec0994e2010-02-02 16:25:35 -080012#include "auth_x.h"
Sage Weil4e7a5dc2009-11-18 16:19:57 -080013
Sage Weil4e7a5dc2009-11-18 16:19:57 -080014
15/*
16 * get protocol handler
17 */
18static u32 supported_protocols[] = {
Sage Weilec0994e2010-02-02 16:25:35 -080019 CEPH_AUTH_NONE,
20 CEPH_AUTH_CEPHX
Sage Weil4e7a5dc2009-11-18 16:19:57 -080021};
22
Yehuda Sadehcd84db62010-06-11 16:58:48 -070023static int ceph_auth_init_protocol(struct ceph_auth_client *ac, int protocol)
Sage Weil4e7a5dc2009-11-18 16:19:57 -080024{
25 switch (protocol) {
26 case CEPH_AUTH_NONE:
27 return ceph_auth_none_init(ac);
Sage Weilec0994e2010-02-02 16:25:35 -080028 case CEPH_AUTH_CEPHX:
29 return ceph_x_init(ac);
Sage Weil4e7a5dc2009-11-18 16:19:57 -080030 default:
31 return -ENOENT;
32 }
33}
34
35/*
36 * setup, teardown.
37 */
Tommi Virtanen8323c3a2011-03-25 16:32:57 -070038struct ceph_auth_client *ceph_auth_init(const char *name, const struct ceph_crypto_key *key)
Sage Weil4e7a5dc2009-11-18 16:19:57 -080039{
40 struct ceph_auth_client *ac;
41 int ret;
42
Tommi Virtanen8323c3a2011-03-25 16:32:57 -070043 dout("auth_init name '%s'\n", name);
Sage Weil4e7a5dc2009-11-18 16:19:57 -080044
45 ret = -ENOMEM;
46 ac = kzalloc(sizeof(*ac), GFP_NOFS);
47 if (!ac)
48 goto out;
49
Sage Weile9966072013-03-25 10:26:30 -070050 mutex_init(&ac->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -080051 ac->negotiating = true;
52 if (name)
53 ac->name = name;
54 else
55 ac->name = CEPH_AUTH_NAME_DEFAULT;
Tommi Virtanen8323c3a2011-03-25 16:32:57 -070056 dout("auth_init name %s\n", ac->name);
57 ac->key = key;
Sage Weil4e7a5dc2009-11-18 16:19:57 -080058 return ac;
59
60out:
61 return ERR_PTR(ret);
62}
63
64void ceph_auth_destroy(struct ceph_auth_client *ac)
65{
66 dout("auth_destroy %p\n", ac);
67 if (ac->ops)
68 ac->ops->destroy(ac);
69 kfree(ac);
70}
71
72/*
73 * Reset occurs when reconnecting to the monitor.
74 */
75void ceph_auth_reset(struct ceph_auth_client *ac)
76{
Sage Weile9966072013-03-25 10:26:30 -070077 mutex_lock(&ac->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -080078 dout("auth_reset %p\n", ac);
79 if (ac->ops && !ac->negotiating)
80 ac->ops->reset(ac);
81 ac->negotiating = true;
Sage Weile9966072013-03-25 10:26:30 -070082 mutex_unlock(&ac->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -080083}
84
Ilya Dryomovf01d5cb2016-06-02 16:45:08 +020085/*
86 * EntityName, not to be confused with entity_name_t
87 */
88int ceph_auth_entity_name_encode(const char *name, void **p, void *end)
Sage Weil4e7a5dc2009-11-18 16:19:57 -080089{
90 int len = strlen(name);
91
92 if (*p + 2*sizeof(u32) + len > end)
93 return -ERANGE;
94 ceph_encode_32(p, CEPH_ENTITY_TYPE_CLIENT);
95 ceph_encode_32(p, len);
96 ceph_encode_copy(p, name, len);
97 return 0;
98}
99
100/*
101 * Initiate protocol negotiation with monitor. Include entity name
102 * and list supported protocols.
103 */
104int ceph_auth_build_hello(struct ceph_auth_client *ac, void *buf, size_t len)
105{
106 struct ceph_mon_request_header *monhdr = buf;
107 void *p = monhdr + 1, *end = buf + len, *lenp;
108 int i, num;
109 int ret;
110
Sage Weile9966072013-03-25 10:26:30 -0700111 mutex_lock(&ac->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800112 dout("auth_build_hello\n");
113 monhdr->have_version = 0;
114 monhdr->session_mon = cpu_to_le16(-1);
115 monhdr->session_mon_tid = 0;
116
Ilya Dryomovfdc723e2016-09-23 09:57:56 +0200117 ceph_encode_32(&p, CEPH_AUTH_UNKNOWN); /* no protocol, yet */
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800118
119 lenp = p;
120 p += sizeof(u32);
121
Sage Weil07c87392010-02-04 09:42:20 -0800122 ceph_decode_need(&p, end, 1 + sizeof(u32), bad);
123 ceph_encode_8(&p, 1);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800124 num = ARRAY_SIZE(supported_protocols);
125 ceph_encode_32(&p, num);
Sage Weil07c87392010-02-04 09:42:20 -0800126 ceph_decode_need(&p, end, num * sizeof(u32), bad);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800127 for (i = 0; i < num; i++)
128 ceph_encode_32(&p, supported_protocols[i]);
129
Ilya Dryomovf01d5cb2016-06-02 16:45:08 +0200130 ret = ceph_auth_entity_name_encode(ac->name, &p, end);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800131 if (ret < 0)
Sage Weile9966072013-03-25 10:26:30 -0700132 goto out;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800133 ceph_decode_need(&p, end, sizeof(u64), bad);
134 ceph_encode_64(&p, ac->global_id);
135
136 ceph_encode_32(&lenp, p - lenp - sizeof(u32));
Sage Weile9966072013-03-25 10:26:30 -0700137 ret = p - buf;
138out:
139 mutex_unlock(&ac->mutex);
140 return ret;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800141
142bad:
Sage Weile9966072013-03-25 10:26:30 -0700143 ret = -ERANGE;
144 goto out;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800145}
146
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700147static int ceph_build_auth_request(struct ceph_auth_client *ac,
148 void *msg_buf, size_t msg_len)
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800149{
150 struct ceph_mon_request_header *monhdr = msg_buf;
151 void *p = monhdr + 1;
152 void *end = msg_buf + msg_len;
153 int ret;
154
155 monhdr->have_version = 0;
156 monhdr->session_mon = cpu_to_le16(-1);
157 monhdr->session_mon_tid = 0;
158
159 ceph_encode_32(&p, ac->protocol);
160
161 ret = ac->ops->build_request(ac, p + sizeof(u32), end);
162 if (ret < 0) {
Sage Weil559c1e02010-05-14 09:55:18 -0700163 pr_err("error %d building auth method %s request\n", ret,
164 ac->ops->name);
Sage Weile9966072013-03-25 10:26:30 -0700165 goto out;
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800166 }
167 dout(" built request %d bytes\n", ret);
168 ceph_encode_32(&p, ret);
Sage Weile9966072013-03-25 10:26:30 -0700169 ret = p + ret - msg_buf;
170out:
171 return ret;
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800172}
173
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800174/*
175 * Handle auth message from monitor.
176 */
177int ceph_handle_auth_reply(struct ceph_auth_client *ac,
178 void *buf, size_t len,
179 void *reply_buf, size_t reply_len)
180{
181 void *p = buf;
182 void *end = buf + len;
183 int protocol;
184 s32 result;
185 u64 global_id;
186 void *payload, *payload_end;
187 int payload_len;
188 char *result_msg;
189 int result_msg_len;
190 int ret = -EINVAL;
191
Sage Weile9966072013-03-25 10:26:30 -0700192 mutex_lock(&ac->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800193 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);
199 payload = p;
200 p += payload_len;
201 ceph_decode_need(&p, end, sizeof(u32), bad);
202 result_msg_len = ceph_decode_32(&p);
203 result_msg = p;
204 p += result_msg_len;
205 if (p != end)
206 goto bad;
207
208 dout(" result %d '%.*s' gid %llu len %d\n", result, result_msg_len,
209 result_msg, global_id, payload_len);
210
211 payload_end = payload + payload_len;
212
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;
216 }
217
218 if (ac->negotiating) {
Yehuda Sadehdc146572009-11-20 13:59:13 -0800219 /* server does not support our protocols? */
220 if (!protocol && result < 0) {
221 ret = result;
222 goto out;
223 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800224 /* set up (new) protocol handler? */
225 if (ac->protocol && ac->protocol != protocol) {
226 ac->ops->destroy(ac);
227 ac->protocol = 0;
228 ac->ops = NULL;
229 }
230 if (ac->protocol != protocol) {
231 ret = ceph_auth_init_protocol(ac, protocol);
232 if (ret) {
Sage Weile95e9a72010-05-25 09:24:42 -0700233 pr_err("error %d on auth protocol %d init\n",
234 ret, protocol);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800235 goto out;
236 }
237 }
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800238
239 ac->negotiating = false;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800240 }
241
242 ret = ac->ops->handle_reply(ac, result, payload, payload_end);
243 if (ret == -EAGAIN) {
Sage Weile9966072013-03-25 10:26:30 -0700244 ret = ceph_build_auth_request(ac, reply_buf, reply_len);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800245 } else if (ret) {
Sage Weil559c1e02010-05-14 09:55:18 -0700246 pr_err("auth method '%s' error %d\n", ac->ops->name, ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800247 }
Sage Weile9966072013-03-25 10:26:30 -0700248
249out:
250 mutex_unlock(&ac->mutex);
251 return ret;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800252
253bad:
254 pr_err("failed to decode auth msg\n");
Sage Weile9966072013-03-25 10:26:30 -0700255 ret = -EINVAL;
256 goto out;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800257}
258
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800259int ceph_build_auth(struct ceph_auth_client *ac,
260 void *msg_buf, size_t msg_len)
261{
Sage Weile9966072013-03-25 10:26:30 -0700262 int ret = 0;
263
264 mutex_lock(&ac->mutex);
Ilya Dryomov464691b2016-09-23 09:57:57 +0200265 if (ac->ops->should_authenticate(ac))
Sage Weile9966072013-03-25 10:26:30 -0700266 ret = ceph_build_auth_request(ac, msg_buf, msg_len);
267 mutex_unlock(&ac->mutex);
268 return ret;
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800269}
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800270
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800271int ceph_auth_is_authenticated(struct ceph_auth_client *ac)
272{
Sage Weile9966072013-03-25 10:26:30 -0700273 int ret = 0;
274
275 mutex_lock(&ac->mutex);
276 if (ac->ops)
277 ret = ac->ops->is_authenticated(ac);
278 mutex_unlock(&ac->mutex);
279 return ret;
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800280}
Sage Weil27859f92013-03-25 10:26:14 -0700281EXPORT_SYMBOL(ceph_auth_is_authenticated);
282
283int ceph_auth_create_authorizer(struct ceph_auth_client *ac,
284 int peer_type,
285 struct ceph_auth_handshake *auth)
286{
Sage Weile9966072013-03-25 10:26:30 -0700287 int ret = 0;
288
289 mutex_lock(&ac->mutex);
Sage Weil27859f92013-03-25 10:26:14 -0700290 if (ac->ops && ac->ops->create_authorizer)
Sage Weile9966072013-03-25 10:26:30 -0700291 ret = ac->ops->create_authorizer(ac, peer_type, auth);
292 mutex_unlock(&ac->mutex);
293 return ret;
Sage Weil27859f92013-03-25 10:26:14 -0700294}
295EXPORT_SYMBOL(ceph_auth_create_authorizer);
296
Ilya Dryomov6c1ea262016-04-11 19:34:49 +0200297void ceph_auth_destroy_authorizer(struct ceph_authorizer *a)
Sage Weil27859f92013-03-25 10:26:14 -0700298{
Ilya Dryomov6c1ea262016-04-11 19:34:49 +0200299 a->destroy(a);
Sage Weil27859f92013-03-25 10:26:14 -0700300}
301EXPORT_SYMBOL(ceph_auth_destroy_authorizer);
302
303int ceph_auth_update_authorizer(struct ceph_auth_client *ac,
304 int peer_type,
305 struct ceph_auth_handshake *a)
306{
307 int ret = 0;
308
Sage Weile9966072013-03-25 10:26:30 -0700309 mutex_lock(&ac->mutex);
Sage Weil27859f92013-03-25 10:26:14 -0700310 if (ac->ops && ac->ops->update_authorizer)
311 ret = ac->ops->update_authorizer(ac, peer_type, a);
Sage Weile9966072013-03-25 10:26:30 -0700312 mutex_unlock(&ac->mutex);
Sage Weil27859f92013-03-25 10:26:14 -0700313 return ret;
314}
315EXPORT_SYMBOL(ceph_auth_update_authorizer);
316
317int ceph_auth_verify_authorizer_reply(struct ceph_auth_client *ac,
318 struct ceph_authorizer *a, size_t len)
319{
Sage Weile9966072013-03-25 10:26:30 -0700320 int ret = 0;
321
322 mutex_lock(&ac->mutex);
Sage Weil27859f92013-03-25 10:26:14 -0700323 if (ac->ops && ac->ops->verify_authorizer_reply)
Sage Weile9966072013-03-25 10:26:30 -0700324 ret = ac->ops->verify_authorizer_reply(ac, a, len);
325 mutex_unlock(&ac->mutex);
326 return ret;
Sage Weil27859f92013-03-25 10:26:14 -0700327}
328EXPORT_SYMBOL(ceph_auth_verify_authorizer_reply);
329
330void ceph_auth_invalidate_authorizer(struct ceph_auth_client *ac, int peer_type)
331{
Sage Weile9966072013-03-25 10:26:30 -0700332 mutex_lock(&ac->mutex);
Sage Weil27859f92013-03-25 10:26:14 -0700333 if (ac->ops && ac->ops->invalidate_authorizer)
334 ac->ops->invalidate_authorizer(ac, peer_type);
Sage Weile9966072013-03-25 10:26:30 -0700335 mutex_unlock(&ac->mutex);
Sage Weil27859f92013-03-25 10:26:14 -0700336}
337EXPORT_SYMBOL(ceph_auth_invalidate_authorizer);