1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/net/sunrpc/svcauth.c
5 * The generic interface for RPC authentication on the server side.
7 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
10 * 19-Apr-2000 Chris Evans - Security fix
13 #include <linux/types.h>
14 #include <linux/module.h>
15 #include <linux/sunrpc/types.h>
16 #include <linux/sunrpc/xdr.h>
17 #include <linux/sunrpc/svcsock.h>
18 #include <linux/sunrpc/svcauth.h>
19 #include <linux/err.h>
20 #include <linux/hash.h>
22 #include <trace/events/sunrpc.h>
24 #define RPCDBG_FACILITY RPCDBG_AUTH
28 * Table of authenticators
30 extern struct auth_ops svcauth_null;
31 extern struct auth_ops svcauth_unix;
33 static struct auth_ops __rcu *authtab[RPC_AUTH_MAXFLAVOR] = {
34 [RPC_AUTH_NULL] = (struct auth_ops __force __rcu *)&svcauth_null,
35 [RPC_AUTH_UNIX] = (struct auth_ops __force __rcu *)&svcauth_unix,
38 static struct auth_ops *
39 svc_get_auth_ops(rpc_authflavor_t flavor)
41 struct auth_ops *aops;
43 if (flavor >= RPC_AUTH_MAXFLAVOR)
46 aops = rcu_dereference(authtab[flavor]);
47 if (aops != NULL && !try_module_get(aops->owner))
54 svc_put_auth_ops(struct auth_ops *aops)
56 module_put(aops->owner);
60 svc_authenticate(struct svc_rqst *rqstp, __be32 *authp)
62 rpc_authflavor_t flavor;
63 struct auth_ops *aops;
67 flavor = svc_getnl(&rqstp->rq_arg.head[0]);
69 dprintk("svc: svc_authenticate (%d)\n", flavor);
71 aops = svc_get_auth_ops(flavor);
73 *authp = rpc_autherr_badcred;
77 rqstp->rq_auth_slack = 0;
78 init_svc_cred(&rqstp->rq_cred);
80 rqstp->rq_authop = aops;
81 return aops->accept(rqstp, authp);
83 EXPORT_SYMBOL_GPL(svc_authenticate);
85 int svc_set_client(struct svc_rqst *rqstp)
87 rqstp->rq_client = NULL;
88 return rqstp->rq_authop->set_client(rqstp);
90 EXPORT_SYMBOL_GPL(svc_set_client);
92 /* A request, which was authenticated, has now executed.
93 * Time to finalise the credentials and verifier
94 * and release and resources
96 int svc_authorise(struct svc_rqst *rqstp)
98 struct auth_ops *aops = rqstp->rq_authop;
101 rqstp->rq_authop = NULL;
104 rv = aops->release(rqstp);
105 svc_put_auth_ops(aops);
111 svc_auth_register(rpc_authflavor_t flavor, struct auth_ops *aops)
113 struct auth_ops *old;
116 if (flavor < RPC_AUTH_MAXFLAVOR) {
117 old = cmpxchg((struct auth_ops ** __force)&authtab[flavor], NULL, aops);
118 if (old == NULL || old == aops)
123 EXPORT_SYMBOL_GPL(svc_auth_register);
126 svc_auth_unregister(rpc_authflavor_t flavor)
128 if (flavor < RPC_AUTH_MAXFLAVOR)
129 rcu_assign_pointer(authtab[flavor], NULL);
131 EXPORT_SYMBOL_GPL(svc_auth_unregister);
133 /**************************************************
134 * 'auth_domains' are stored in a hash table indexed by name.
135 * When the last reference to an 'auth_domain' is dropped,
136 * the object is unhashed and freed.
137 * If auth_domain_lookup fails to find an entry, it will return
138 * it's second argument 'new'. If this is non-null, it will
139 * have been atomically linked into the table.
142 #define DN_HASHBITS 6
143 #define DN_HASHMAX (1<<DN_HASHBITS)
145 static struct hlist_head auth_domain_table[DN_HASHMAX];
146 static DEFINE_SPINLOCK(auth_domain_lock);
148 static void auth_domain_release(struct kref *kref)
149 __releases(&auth_domain_lock)
151 struct auth_domain *dom = container_of(kref, struct auth_domain, ref);
153 hlist_del_rcu(&dom->hash);
154 dom->flavour->domain_release(dom);
155 spin_unlock(&auth_domain_lock);
158 void auth_domain_put(struct auth_domain *dom)
160 kref_put_lock(&dom->ref, auth_domain_release, &auth_domain_lock);
162 EXPORT_SYMBOL_GPL(auth_domain_put);
165 auth_domain_lookup(char *name, struct auth_domain *new)
167 struct auth_domain *hp;
168 struct hlist_head *head;
170 head = &auth_domain_table[hash_str(name, DN_HASHBITS)];
172 spin_lock(&auth_domain_lock);
174 hlist_for_each_entry(hp, head, hash) {
175 if (strcmp(hp->name, name)==0) {
177 spin_unlock(&auth_domain_lock);
182 hlist_add_head_rcu(&new->hash, head);
183 spin_unlock(&auth_domain_lock);
186 EXPORT_SYMBOL_GPL(auth_domain_lookup);
188 struct auth_domain *auth_domain_find(char *name)
190 struct auth_domain *hp;
191 struct hlist_head *head;
193 head = &auth_domain_table[hash_str(name, DN_HASHBITS)];
196 hlist_for_each_entry_rcu(hp, head, hash) {
197 if (strcmp(hp->name, name)==0) {
198 if (!kref_get_unless_zero(&hp->ref))
207 EXPORT_SYMBOL_GPL(auth_domain_find);