xref: /linux/net/ceph/auth_none.c (revision 762f99f4f3cb41a775b5157dd761217beba65873)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
24e7a5dcdSSage Weil 
33d14c5d2SYehuda Sadeh #include <linux/ceph/ceph_debug.h>
44e7a5dcdSSage Weil 
54e7a5dcdSSage Weil #include <linux/err.h>
64e7a5dcdSSage Weil #include <linux/module.h>
74e7a5dcdSSage Weil #include <linux/random.h>
85a0e3ad6STejun Heo #include <linux/slab.h>
94e7a5dcdSSage Weil 
103d14c5d2SYehuda Sadeh #include <linux/ceph/decode.h>
113d14c5d2SYehuda Sadeh #include <linux/ceph/auth.h>
123d14c5d2SYehuda Sadeh 
134e7a5dcdSSage Weil #include "auth_none.h"
144e7a5dcdSSage Weil 
reset(struct ceph_auth_client * ac)154e7a5dcdSSage Weil static void reset(struct ceph_auth_client *ac)
164e7a5dcdSSage Weil {
174e7a5dcdSSage Weil 	struct ceph_auth_none_info *xi = ac->private;
184e7a5dcdSSage Weil 
194e7a5dcdSSage Weil 	xi->starting = true;
204e7a5dcdSSage Weil }
214e7a5dcdSSage Weil 
destroy(struct ceph_auth_client * ac)224e7a5dcdSSage Weil static void destroy(struct ceph_auth_client *ac)
234e7a5dcdSSage Weil {
244e7a5dcdSSage Weil 	kfree(ac->private);
254e7a5dcdSSage Weil 	ac->private = NULL;
264e7a5dcdSSage Weil }
274e7a5dcdSSage Weil 
is_authenticated(struct ceph_auth_client * ac)284e7a5dcdSSage Weil static int is_authenticated(struct ceph_auth_client *ac)
294e7a5dcdSSage Weil {
304e7a5dcdSSage Weil 	struct ceph_auth_none_info *xi = ac->private;
314e7a5dcdSSage Weil 
324e7a5dcdSSage Weil 	return !xi->starting;
334e7a5dcdSSage Weil }
344e7a5dcdSSage Weil 
should_authenticate(struct ceph_auth_client * ac)35a41359faSSage Weil static int should_authenticate(struct ceph_auth_client *ac)
36a41359faSSage Weil {
37a41359faSSage Weil 	struct ceph_auth_none_info *xi = ac->private;
38a41359faSSage Weil 
39a41359faSSage Weil 	return xi->starting;
40a41359faSSage Weil }
41a41359faSSage Weil 
ceph_auth_none_build_authorizer(struct ceph_auth_client * ac,struct ceph_none_authorizer * au)426c1ea260SIlya Dryomov static int ceph_auth_none_build_authorizer(struct ceph_auth_client *ac,
436c1ea260SIlya Dryomov 					   struct ceph_none_authorizer *au)
446c1ea260SIlya Dryomov {
456c1ea260SIlya Dryomov 	void *p = au->buf;
466c1ea260SIlya Dryomov 	void *const end = p + sizeof(au->buf);
476c1ea260SIlya Dryomov 	int ret;
486c1ea260SIlya Dryomov 
496c1ea260SIlya Dryomov 	ceph_encode_8_safe(&p, end, 1, e_range);
50f01d5cb2SIlya Dryomov 	ret = ceph_auth_entity_name_encode(ac->name, &p, end);
516c1ea260SIlya Dryomov 	if (ret < 0)
526c1ea260SIlya Dryomov 		return ret;
536c1ea260SIlya Dryomov 
546c1ea260SIlya Dryomov 	ceph_encode_64_safe(&p, end, ac->global_id, e_range);
556c1ea260SIlya Dryomov 	au->buf_len = p - (void *)au->buf;
566c1ea260SIlya Dryomov 	dout("%s built authorizer len %d\n", __func__, au->buf_len);
576c1ea260SIlya Dryomov 	return 0;
586c1ea260SIlya Dryomov 
596c1ea260SIlya Dryomov e_range:
606c1ea260SIlya Dryomov 	return -ERANGE;
616c1ea260SIlya Dryomov }
626c1ea260SIlya Dryomov 
build_request(struct ceph_auth_client * ac,void * buf,void * end)632cb33cacSTyler Hicks static int build_request(struct ceph_auth_client *ac, void *buf, void *end)
642cb33cacSTyler Hicks {
652cb33cacSTyler Hicks 	return 0;
662cb33cacSTyler Hicks }
672cb33cacSTyler Hicks 
684e7a5dcdSSage Weil /*
694e7a5dcdSSage Weil  * the generic auth code decode the global_id, and we carry no actual
704e7a5dcdSSage Weil  * authenticate state, so nothing happens here.
714e7a5dcdSSage Weil  */
handle_reply(struct ceph_auth_client * ac,u64 global_id,void * buf,void * end,u8 * session_key,int * session_key_len,u8 * con_secret,int * con_secret_len)7203af4c7bSIlya Dryomov static int handle_reply(struct ceph_auth_client *ac, u64 global_id,
73285ea34fSIlya Dryomov 			void *buf, void *end, u8 *session_key,
74285ea34fSIlya Dryomov 			int *session_key_len, u8 *con_secret,
75285ea34fSIlya Dryomov 			int *con_secret_len)
764e7a5dcdSSage Weil {
774e7a5dcdSSage Weil 	struct ceph_auth_none_info *xi = ac->private;
784e7a5dcdSSage Weil 
794e7a5dcdSSage Weil 	xi->starting = false;
8003af4c7bSIlya Dryomov 	ceph_auth_set_global_id(ac, global_id);
813c0d0894SIlya Dryomov 	return 0;
824e7a5dcdSSage Weil }
834e7a5dcdSSage Weil 
ceph_auth_none_destroy_authorizer(struct ceph_authorizer * a)846c1ea260SIlya Dryomov static void ceph_auth_none_destroy_authorizer(struct ceph_authorizer *a)
856c1ea260SIlya Dryomov {
866c1ea260SIlya Dryomov 	kfree(a);
876c1ea260SIlya Dryomov }
886c1ea260SIlya Dryomov 
894e7a5dcdSSage Weil /*
906c1ea260SIlya Dryomov  * build an 'authorizer' with our entity_name and global_id.  it is
916c1ea260SIlya Dryomov  * identical for all services we connect to.
924e7a5dcdSSage Weil  */
ceph_auth_none_create_authorizer(struct ceph_auth_client * ac,int peer_type,struct ceph_auth_handshake * auth)934e7a5dcdSSage Weil static int ceph_auth_none_create_authorizer(
944e7a5dcdSSage Weil 	struct ceph_auth_client *ac, int peer_type,
9574f1869fSAlex Elder 	struct ceph_auth_handshake *auth)
964e7a5dcdSSage Weil {
976c1ea260SIlya Dryomov 	struct ceph_none_authorizer *au;
984e7a5dcdSSage Weil 	int ret;
994e7a5dcdSSage Weil 
1006c1ea260SIlya Dryomov 	au = kmalloc(sizeof(*au), GFP_NOFS);
1016c1ea260SIlya Dryomov 	if (!au)
1026c1ea260SIlya Dryomov 		return -ENOMEM;
1036c1ea260SIlya Dryomov 
1046c1ea260SIlya Dryomov 	au->base.destroy = ceph_auth_none_destroy_authorizer;
1056c1ea260SIlya Dryomov 
1066c1ea260SIlya Dryomov 	ret = ceph_auth_none_build_authorizer(ac, au);
1076c1ea260SIlya Dryomov 	if (ret) {
1086c1ea260SIlya Dryomov 		kfree(au);
1096c1ea260SIlya Dryomov 		return ret;
1104e7a5dcdSSage Weil 	}
1114e7a5dcdSSage Weil 
11274f1869fSAlex Elder 	auth->authorizer = (struct ceph_authorizer *) au;
11374f1869fSAlex Elder 	auth->authorizer_buf = au->buf;
11474f1869fSAlex Elder 	auth->authorizer_buf_len = au->buf_len;
115d71a95e7SIlya Dryomov 	auth->authorizer_reply_buf = NULL;
116d71a95e7SIlya Dryomov 	auth->authorizer_reply_buf_len = 0;
11774f1869fSAlex Elder 
1184e7a5dcdSSage Weil 	return 0;
1194e7a5dcdSSage Weil }
1204e7a5dcdSSage Weil 
1214e7a5dcdSSage Weil static const struct ceph_auth_client_ops ceph_auth_none_ops = {
1224e7a5dcdSSage Weil 	.reset = reset,
1234e7a5dcdSSage Weil 	.destroy = destroy,
1244e7a5dcdSSage Weil 	.is_authenticated = is_authenticated,
125a41359faSSage Weil 	.should_authenticate = should_authenticate,
1262cb33cacSTyler Hicks 	.build_request = build_request,
1274e7a5dcdSSage Weil 	.handle_reply = handle_reply,
1284e7a5dcdSSage Weil 	.create_authorizer = ceph_auth_none_create_authorizer,
1294e7a5dcdSSage Weil };
1304e7a5dcdSSage Weil 
ceph_auth_none_init(struct ceph_auth_client * ac)1314e7a5dcdSSage Weil int ceph_auth_none_init(struct ceph_auth_client *ac)
1324e7a5dcdSSage Weil {
1334e7a5dcdSSage Weil 	struct ceph_auth_none_info *xi;
1344e7a5dcdSSage Weil 
1354e7a5dcdSSage Weil 	dout("ceph_auth_none_init %p\n", ac);
1364e7a5dcdSSage Weil 	xi = kzalloc(sizeof(*xi), GFP_NOFS);
1374e7a5dcdSSage Weil 	if (!xi)
1384e7a5dcdSSage Weil 		return -ENOMEM;
1394e7a5dcdSSage Weil 
1404e7a5dcdSSage Weil 	xi->starting = true;
1414e7a5dcdSSage Weil 
1424e7a5dcdSSage Weil 	ac->protocol = CEPH_AUTH_NONE;
1434e7a5dcdSSage Weil 	ac->private = xi;
1444e7a5dcdSSage Weil 	ac->ops = &ceph_auth_none_ops;
1454e7a5dcdSSage Weil 	return 0;
1464e7a5dcdSSage Weil }
147