1// SPDX-License-Identifier: GPL-2.0
2/*
3 * linux/net/sunrpc/auth_null.c
4 *
5 * AUTH_NULL authentication. Really :-)
6 *
7 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
8 */
9
10#include <linux/types.h>
11#include <linux/module.h>
12#include <linux/sunrpc/clnt.h>
13
14#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
15# define RPCDBG_FACILITY	RPCDBG_AUTH
16#endif
17
18static struct rpc_auth null_auth;
19static struct rpc_cred null_cred;
20
21static struct rpc_auth *
22nul_create(const struct rpc_auth_create_args *args, struct rpc_clnt *clnt)
23{
24	refcount_inc(&null_auth.au_count);
25	return &null_auth;
26}
27
28static void
29nul_destroy(struct rpc_auth *auth)
30{
31}
32
33/*
34 * Lookup NULL creds for current process
35 */
36static struct rpc_cred *
37nul_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
38{
39	return get_rpccred(&null_cred);
40}
41
42/*
43 * Destroy cred handle.
44 */
45static void
46nul_destroy_cred(struct rpc_cred *cred)
47{
48}
49
50/*
51 * Match cred handle against current process
52 */
53static int
54nul_match(struct auth_cred *acred, struct rpc_cred *cred, int taskflags)
55{
56	return 1;
57}
58
59/*
60 * Marshal credential.
61 */
62static int
63nul_marshal(struct rpc_task *task, struct xdr_stream *xdr)
64{
65	__be32 *p;
66
67	p = xdr_reserve_space(xdr, 4 * sizeof(*p));
68	if (!p)
69		return -EMSGSIZE;
70	/* Credential */
71	*p++ = rpc_auth_null;
72	*p++ = xdr_zero;
73	/* Verifier */
74	*p++ = rpc_auth_null;
75	*p   = xdr_zero;
76	return 0;
77}
78
79/*
80 * Refresh credential. This is a no-op for AUTH_NULL
81 */
82static int
83nul_refresh(struct rpc_task *task)
84{
85	set_bit(RPCAUTH_CRED_UPTODATE, &task->tk_rqstp->rq_cred->cr_flags);
86	return 0;
87}
88
89static int
90nul_validate(struct rpc_task *task, struct xdr_stream *xdr)
91{
92	__be32 *p;
93
94	p = xdr_inline_decode(xdr, 2 * sizeof(*p));
95	if (!p)
96		return -EIO;
97	if (*p++ != rpc_auth_null)
98		return -EIO;
99	if (*p != xdr_zero)
100		return -EIO;
101	return 0;
102}
103
104const struct rpc_authops authnull_ops = {
105	.owner		= THIS_MODULE,
106	.au_flavor	= RPC_AUTH_NULL,
107	.au_name	= "NULL",
108	.create		= nul_create,
109	.destroy	= nul_destroy,
110	.lookup_cred	= nul_lookup_cred,
111};
112
113static
114struct rpc_auth null_auth = {
115	.au_cslack	= NUL_CALLSLACK,
116	.au_rslack	= NUL_REPLYSLACK,
117	.au_verfsize	= NUL_REPLYSLACK,
118	.au_ralign	= NUL_REPLYSLACK,
119	.au_ops		= &authnull_ops,
120	.au_flavor	= RPC_AUTH_NULL,
121	.au_count	= REFCOUNT_INIT(1),
122};
123
124static
125const struct rpc_credops null_credops = {
126	.cr_name	= "AUTH_NULL",
127	.crdestroy	= nul_destroy_cred,
128	.crmatch	= nul_match,
129	.crmarshal	= nul_marshal,
130	.crwrap_req	= rpcauth_wrap_req_encode,
131	.crrefresh	= nul_refresh,
132	.crvalidate	= nul_validate,
133	.crunwrap_resp	= rpcauth_unwrap_resp_decode,
134};
135
136static
137struct rpc_cred null_cred = {
138	.cr_lru		= LIST_HEAD_INIT(null_cred.cr_lru),
139	.cr_auth	= &null_auth,
140	.cr_ops		= &null_credops,
141	.cr_count	= REFCOUNT_INIT(2),
142	.cr_flags	= 1UL << RPCAUTH_CRED_UPTODATE,
143};
144