1// SPDX-License-Identifier: GPL-2.0
2
3#include <linux/ceph/ceph_debug.h>
4
5#include <linux/err.h>
6#include <linux/module.h>
7#include <linux/random.h>
8#include <linux/slab.h>
9
10#include <linux/ceph/decode.h>
11#include <linux/ceph/auth.h>
12
13#include "auth_none.h"
14
15static void reset(struct ceph_auth_client *ac)
16{
17	struct ceph_auth_none_info *xi = ac->private;
18
19	xi->starting = true;
20}
21
22static void destroy(struct ceph_auth_client *ac)
23{
24	kfree(ac->private);
25	ac->private = NULL;
26}
27
28static int is_authenticated(struct ceph_auth_client *ac)
29{
30	struct ceph_auth_none_info *xi = ac->private;
31
32	return !xi->starting;
33}
34
35static int should_authenticate(struct ceph_auth_client *ac)
36{
37	struct ceph_auth_none_info *xi = ac->private;
38
39	return xi->starting;
40}
41
42static int ceph_auth_none_build_authorizer(struct ceph_auth_client *ac,
43					   struct ceph_none_authorizer *au)
44{
45	void *p = au->buf;
46	void *const end = p + sizeof(au->buf);
47	int ret;
48
49	ceph_encode_8_safe(&p, end, 1, e_range);
50	ret = ceph_auth_entity_name_encode(ac->name, &p, end);
51	if (ret < 0)
52		return ret;
53
54	ceph_encode_64_safe(&p, end, ac->global_id, e_range);
55	au->buf_len = p - (void *)au->buf;
56	dout("%s built authorizer len %d\n", __func__, au->buf_len);
57	return 0;
58
59e_range:
60	return -ERANGE;
61}
62
63static int build_request(struct ceph_auth_client *ac, void *buf, void *end)
64{
65	return 0;
66}
67
68/*
69 * the generic auth code decode the global_id, and we carry no actual
70 * authenticate state, so nothing happens here.
71 */
72static int handle_reply(struct ceph_auth_client *ac, u64 global_id,
73			void *buf, void *end, u8 *session_key,
74			int *session_key_len, u8 *con_secret,
75			int *con_secret_len)
76{
77	struct ceph_auth_none_info *xi = ac->private;
78
79	xi->starting = false;
80	ceph_auth_set_global_id(ac, global_id);
81	return 0;
82}
83
84static void ceph_auth_none_destroy_authorizer(struct ceph_authorizer *a)
85{
86	kfree(a);
87}
88
89/*
90 * build an 'authorizer' with our entity_name and global_id.  it is
91 * identical for all services we connect to.
92 */
93static int ceph_auth_none_create_authorizer(
94	struct ceph_auth_client *ac, int peer_type,
95	struct ceph_auth_handshake *auth)
96{
97	struct ceph_none_authorizer *au;
98	int ret;
99
100	au = kmalloc(sizeof(*au), GFP_NOFS);
101	if (!au)
102		return -ENOMEM;
103
104	au->base.destroy = ceph_auth_none_destroy_authorizer;
105
106	ret = ceph_auth_none_build_authorizer(ac, au);
107	if (ret) {
108		kfree(au);
109		return ret;
110	}
111
112	auth->authorizer = (struct ceph_authorizer *) au;
113	auth->authorizer_buf = au->buf;
114	auth->authorizer_buf_len = au->buf_len;
115	auth->authorizer_reply_buf = NULL;
116	auth->authorizer_reply_buf_len = 0;
117
118	return 0;
119}
120
121static const struct ceph_auth_client_ops ceph_auth_none_ops = {
122	.reset = reset,
123	.destroy = destroy,
124	.is_authenticated = is_authenticated,
125	.should_authenticate = should_authenticate,
126	.build_request = build_request,
127	.handle_reply = handle_reply,
128	.create_authorizer = ceph_auth_none_create_authorizer,
129};
130
131int ceph_auth_none_init(struct ceph_auth_client *ac)
132{
133	struct ceph_auth_none_info *xi;
134
135	dout("ceph_auth_none_init %p\n", ac);
136	xi = kzalloc(sizeof(*xi), GFP_NOFS);
137	if (!xi)
138		return -ENOMEM;
139
140	xi->starting = true;
141
142	ac->protocol = CEPH_AUTH_NONE;
143	ac->private = xi;
144	ac->ops = &ceph_auth_none_ops;
145	return 0;
146}
147