Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 LT |
2 | /* |
3 | * linux/net/sunrpc/svcauth.c | |
4 | * | |
5 | * The generic interface for RPC authentication on the server side. | |
cca5172a | 6 | * |
1da177e4 LT |
7 | * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de> |
8 | * | |
9 | * CHANGES | |
10 | * 19-Apr-2000 Chris Evans - Security fix | |
11 | */ | |
12 | ||
13 | #include <linux/types.h> | |
1da177e4 LT |
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> | |
21 | ||
ff27e9f7 CL |
22 | #include <trace/events/sunrpc.h> |
23 | ||
1da177e4 LT |
24 | #define RPCDBG_FACILITY RPCDBG_AUTH |
25 | ||
26 | ||
27 | /* | |
28 | * Table of authenticators | |
29 | */ | |
30 | extern struct auth_ops svcauth_null; | |
31 | extern struct auth_ops svcauth_unix; | |
32 | ||
30382d6c TM |
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, | |
1da177e4 LT |
36 | }; |
37 | ||
30382d6c TM |
38 | static struct auth_ops * |
39 | svc_get_auth_ops(rpc_authflavor_t flavor) | |
40 | { | |
41 | struct auth_ops *aops; | |
42 | ||
43 | if (flavor >= RPC_AUTH_MAXFLAVOR) | |
44 | return NULL; | |
45 | rcu_read_lock(); | |
46 | aops = rcu_dereference(authtab[flavor]); | |
47 | if (aops != NULL && !try_module_get(aops->owner)) | |
48 | aops = NULL; | |
49 | rcu_read_unlock(); | |
50 | return aops; | |
51 | } | |
52 | ||
53 | static void | |
54 | svc_put_auth_ops(struct auth_ops *aops) | |
55 | { | |
56 | module_put(aops->owner); | |
57 | } | |
58 | ||
1da177e4 | 59 | int |
d8ed029d | 60 | svc_authenticate(struct svc_rqst *rqstp, __be32 *authp) |
1da177e4 LT |
61 | { |
62 | rpc_authflavor_t flavor; | |
63 | struct auth_ops *aops; | |
64 | ||
65 | *authp = rpc_auth_ok; | |
66 | ||
76994313 | 67 | flavor = svc_getnl(&rqstp->rq_arg.head[0]); |
1da177e4 LT |
68 | |
69 | dprintk("svc: svc_authenticate (%d)\n", flavor); | |
70 | ||
30382d6c TM |
71 | aops = svc_get_auth_ops(flavor); |
72 | if (aops == NULL) { | |
1da177e4 LT |
73 | *authp = rpc_autherr_badcred; |
74 | return SVC_DENIED; | |
75 | } | |
1da177e4 | 76 | |
a5cddc88 | 77 | rqstp->rq_auth_slack = 0; |
6496500c | 78 | init_svc_cred(&rqstp->rq_cred); |
a5cddc88 | 79 | |
1da177e4 LT |
80 | rqstp->rq_authop = aops; |
81 | return aops->accept(rqstp, authp); | |
82 | } | |
24c3767e | 83 | EXPORT_SYMBOL_GPL(svc_authenticate); |
1da177e4 LT |
84 | |
85 | int svc_set_client(struct svc_rqst *rqstp) | |
86 | { | |
6496500c | 87 | rqstp->rq_client = NULL; |
1da177e4 LT |
88 | return rqstp->rq_authop->set_client(rqstp); |
89 | } | |
24c3767e | 90 | EXPORT_SYMBOL_GPL(svc_set_client); |
1da177e4 LT |
91 | |
92 | /* A request, which was authenticated, has now executed. | |
59c51591 | 93 | * Time to finalise the credentials and verifier |
1da177e4 LT |
94 | * and release and resources |
95 | */ | |
96 | int svc_authorise(struct svc_rqst *rqstp) | |
97 | { | |
98 | struct auth_ops *aops = rqstp->rq_authop; | |
99 | int rv = 0; | |
100 | ||
101 | rqstp->rq_authop = NULL; | |
cca5172a | 102 | |
1da177e4 LT |
103 | if (aops) { |
104 | rv = aops->release(rqstp); | |
30382d6c | 105 | svc_put_auth_ops(aops); |
1da177e4 LT |
106 | } |
107 | return rv; | |
108 | } | |
109 | ||
110 | int | |
111 | svc_auth_register(rpc_authflavor_t flavor, struct auth_ops *aops) | |
112 | { | |
30382d6c | 113 | struct auth_ops *old; |
1da177e4 | 114 | int rv = -EINVAL; |
30382d6c TM |
115 | |
116 | if (flavor < RPC_AUTH_MAXFLAVOR) { | |
117 | old = cmpxchg((struct auth_ops ** __force)&authtab[flavor], NULL, aops); | |
118 | if (old == NULL || old == aops) | |
119 | rv = 0; | |
1da177e4 | 120 | } |
1da177e4 LT |
121 | return rv; |
122 | } | |
24c3767e | 123 | EXPORT_SYMBOL_GPL(svc_auth_register); |
1da177e4 LT |
124 | |
125 | void | |
126 | svc_auth_unregister(rpc_authflavor_t flavor) | |
127 | { | |
1da177e4 | 128 | if (flavor < RPC_AUTH_MAXFLAVOR) |
30382d6c | 129 | rcu_assign_pointer(authtab[flavor], NULL); |
1da177e4 | 130 | } |
24c3767e | 131 | EXPORT_SYMBOL_GPL(svc_auth_unregister); |
1da177e4 LT |
132 | |
133 | /************************************************** | |
efc36aa5 N |
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. | |
1da177e4 LT |
140 | */ |
141 | ||
1da177e4 LT |
142 | #define DN_HASHBITS 6 |
143 | #define DN_HASHMAX (1<<DN_HASHBITS) | |
1da177e4 | 144 | |
efc36aa5 | 145 | static struct hlist_head auth_domain_table[DN_HASHMAX]; |
3eb15f28 | 146 | static DEFINE_SPINLOCK(auth_domain_lock); |
1da177e4 | 147 | |
0a13cd1a | 148 | static void auth_domain_release(struct kref *kref) |
608a0ab2 | 149 | __releases(&auth_domain_lock) |
0a13cd1a PZ |
150 | { |
151 | struct auth_domain *dom = container_of(kref, struct auth_domain, ref); | |
152 | ||
608a0ab2 | 153 | hlist_del_rcu(&dom->hash); |
0a13cd1a PZ |
154 | dom->flavour->domain_release(dom); |
155 | spin_unlock(&auth_domain_lock); | |
156 | } | |
157 | ||
1da177e4 LT |
158 | void auth_domain_put(struct auth_domain *dom) |
159 | { | |
0a13cd1a | 160 | kref_put_lock(&dom->ref, auth_domain_release, &auth_domain_lock); |
1da177e4 | 161 | } |
24c3767e | 162 | EXPORT_SYMBOL_GPL(auth_domain_put); |
1da177e4 LT |
163 | |
164 | struct auth_domain * | |
efc36aa5 | 165 | auth_domain_lookup(char *name, struct auth_domain *new) |
1da177e4 | 166 | { |
efc36aa5 N |
167 | struct auth_domain *hp; |
168 | struct hlist_head *head; | |
efc36aa5 N |
169 | |
170 | head = &auth_domain_table[hash_str(name, DN_HASHBITS)]; | |
171 | ||
172 | spin_lock(&auth_domain_lock); | |
173 | ||
b67bfe0d | 174 | hlist_for_each_entry(hp, head, hash) { |
efc36aa5 N |
175 | if (strcmp(hp->name, name)==0) { |
176 | kref_get(&hp->ref); | |
177 | spin_unlock(&auth_domain_lock); | |
178 | return hp; | |
1da177e4 | 179 | } |
1da177e4 | 180 | } |
d6740df9 | 181 | if (new) |
608a0ab2 | 182 | hlist_add_head_rcu(&new->hash, head); |
efc36aa5 N |
183 | spin_unlock(&auth_domain_lock); |
184 | return new; | |
1da177e4 | 185 | } |
24c3767e | 186 | EXPORT_SYMBOL_GPL(auth_domain_lookup); |
1da177e4 LT |
187 | |
188 | struct auth_domain *auth_domain_find(char *name) | |
189 | { | |
608a0ab2 TM |
190 | struct auth_domain *hp; |
191 | struct hlist_head *head; | |
192 | ||
193 | head = &auth_domain_table[hash_str(name, DN_HASHBITS)]; | |
194 | ||
195 | rcu_read_lock(); | |
196 | hlist_for_each_entry_rcu(hp, head, hash) { | |
197 | if (strcmp(hp->name, name)==0) { | |
198 | if (!kref_get_unless_zero(&hp->ref)) | |
199 | hp = NULL; | |
200 | rcu_read_unlock(); | |
201 | return hp; | |
202 | } | |
203 | } | |
204 | rcu_read_unlock(); | |
205 | return NULL; | |
1da177e4 | 206 | } |
24c3767e | 207 | EXPORT_SYMBOL_GPL(auth_domain_find); |