blob: 2156327da45b15433a58384579b378025a1b052e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/net/sunrpc/auth.c
3 *
4 * Generic RPC client authentication API.
5 *
6 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
7 */
8
9#include <linux/types.h>
10#include <linux/sched.h>
11#include <linux/module.h>
12#include <linux/slab.h>
13#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/sunrpc/clnt.h>
15#include <linux/spinlock.h>
16
17#ifdef RPC_DEBUG
18# define RPCDBG_FACILITY RPCDBG_AUTH
19#endif
20
Trond Myklebustfc1b3562007-06-09 16:15:46 -040021static DEFINE_SPINLOCK(rpc_authflavor_lock);
Trond Myklebustf1c0a862007-06-23 20:17:58 -040022static const struct rpc_authops *auth_flavors[RPC_AUTH_MAXFLAVOR] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 &authnull_ops, /* AUTH_NULL */
24 &authunix_ops, /* AUTH_UNIX */
25 NULL, /* others can be loadable modules */
26};
27
28static u32
29pseudoflavor_to_flavor(u32 flavor) {
30 if (flavor >= RPC_AUTH_MAXFLAVOR)
31 return RPC_AUTH_GSS;
32 return flavor;
33}
34
35int
Trond Myklebustf1c0a862007-06-23 20:17:58 -040036rpcauth_register(const struct rpc_authops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
38 rpc_authflavor_t flavor;
Trond Myklebustfc1b3562007-06-09 16:15:46 -040039 int ret = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41 if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
42 return -EINVAL;
Trond Myklebustfc1b3562007-06-09 16:15:46 -040043 spin_lock(&rpc_authflavor_lock);
44 if (auth_flavors[flavor] == NULL) {
45 auth_flavors[flavor] = ops;
46 ret = 0;
47 }
48 spin_unlock(&rpc_authflavor_lock);
49 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050}
51
52int
Trond Myklebustf1c0a862007-06-23 20:17:58 -040053rpcauth_unregister(const struct rpc_authops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
55 rpc_authflavor_t flavor;
Trond Myklebustfc1b3562007-06-09 16:15:46 -040056 int ret = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58 if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
59 return -EINVAL;
Trond Myklebustfc1b3562007-06-09 16:15:46 -040060 spin_lock(&rpc_authflavor_lock);
61 if (auth_flavors[flavor] == ops) {
62 auth_flavors[flavor] = NULL;
63 ret = 0;
64 }
65 spin_unlock(&rpc_authflavor_lock);
66 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
68
69struct rpc_auth *
70rpcauth_create(rpc_authflavor_t pseudoflavor, struct rpc_clnt *clnt)
71{
72 struct rpc_auth *auth;
Trond Myklebustf1c0a862007-06-23 20:17:58 -040073 const struct rpc_authops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 u32 flavor = pseudoflavor_to_flavor(pseudoflavor);
75
Olaf Kirchf344f6d2006-03-20 13:44:08 -050076 auth = ERR_PTR(-EINVAL);
77 if (flavor >= RPC_AUTH_MAXFLAVOR)
78 goto out;
79
Olaf Kirchf344f6d2006-03-20 13:44:08 -050080#ifdef CONFIG_KMOD
81 if ((ops = auth_flavors[flavor]) == NULL)
82 request_module("rpc-auth-%u", flavor);
83#endif
Trond Myklebustfc1b3562007-06-09 16:15:46 -040084 spin_lock(&rpc_authflavor_lock);
85 ops = auth_flavors[flavor];
86 if (ops == NULL || !try_module_get(ops->owner)) {
87 spin_unlock(&rpc_authflavor_lock);
Olaf Kirchf344f6d2006-03-20 13:44:08 -050088 goto out;
Trond Myklebustfc1b3562007-06-09 16:15:46 -040089 }
90 spin_unlock(&rpc_authflavor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 auth = ops->create(clnt, pseudoflavor);
Trond Myklebustfc1b3562007-06-09 16:15:46 -040092 module_put(ops->owner);
J. Bruce Fields6a192752005-06-22 17:16:23 +000093 if (IS_ERR(auth))
94 return auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 if (clnt->cl_auth)
Trond Myklebustde7a8ce2007-06-23 10:46:47 -040096 rpcauth_release(clnt->cl_auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 clnt->cl_auth = auth;
Olaf Kirchf344f6d2006-03-20 13:44:08 -050098
99out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return auth;
101}
102
103void
Trond Myklebustde7a8ce2007-06-23 10:46:47 -0400104rpcauth_release(struct rpc_auth *auth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 if (!atomic_dec_and_test(&auth->au_count))
107 return;
108 auth->au_ops->destroy(auth);
109}
110
111static DEFINE_SPINLOCK(rpc_credcache_lock);
112
113/*
114 * Initialize RPC credential cache
115 */
116int
117rpcauth_init_credcache(struct rpc_auth *auth, unsigned long expire)
118{
119 struct rpc_cred_cache *new;
120 int i;
121
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800122 new = kmalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 if (!new)
124 return -ENOMEM;
125 for (i = 0; i < RPC_CREDCACHE_NR; i++)
126 INIT_HLIST_HEAD(&new->hashtable[i]);
127 new->expire = expire;
128 new->nextgc = jiffies + (expire >> 1);
129 auth->au_credcache = new;
130 return 0;
131}
132
133/*
134 * Destroy a list of credentials
135 */
136static inline
137void rpcauth_destroy_credlist(struct hlist_head *head)
138{
139 struct rpc_cred *cred;
140
141 while (!hlist_empty(head)) {
142 cred = hlist_entry(head->first, struct rpc_cred, cr_hash);
143 hlist_del_init(&cred->cr_hash);
144 put_rpccred(cred);
145 }
146}
147
148/*
149 * Clear the RPC credential cache, and delete those credentials
150 * that are not referenced.
151 */
152void
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400153rpcauth_clear_credcache(struct rpc_cred_cache *cache)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 HLIST_HEAD(free);
156 struct hlist_node *pos, *next;
157 struct rpc_cred *cred;
158 int i;
159
160 spin_lock(&rpc_credcache_lock);
161 for (i = 0; i < RPC_CREDCACHE_NR; i++) {
162 hlist_for_each_safe(pos, next, &cache->hashtable[i]) {
163 cred = hlist_entry(pos, struct rpc_cred, cr_hash);
164 __hlist_del(&cred->cr_hash);
165 hlist_add_head(&cred->cr_hash, &free);
166 }
167 }
168 spin_unlock(&rpc_credcache_lock);
169 rpcauth_destroy_credlist(&free);
170}
171
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400172/*
173 * Destroy the RPC credential cache
174 */
175void
176rpcauth_destroy_credcache(struct rpc_auth *auth)
177{
178 struct rpc_cred_cache *cache = auth->au_credcache;
179
180 if (cache) {
181 auth->au_credcache = NULL;
182 rpcauth_clear_credcache(cache);
183 kfree(cache);
184 }
185}
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187static void
188rpcauth_prune_expired(struct rpc_auth *auth, struct rpc_cred *cred, struct hlist_head *free)
189{
190 if (atomic_read(&cred->cr_count) != 1)
191 return;
192 if (time_after(jiffies, cred->cr_expire + auth->au_credcache->expire))
193 cred->cr_flags &= ~RPCAUTH_CRED_UPTODATE;
194 if (!(cred->cr_flags & RPCAUTH_CRED_UPTODATE)) {
195 __hlist_del(&cred->cr_hash);
196 hlist_add_head(&cred->cr_hash, free);
197 }
198}
199
200/*
201 * Remove stale credentials. Avoid sleeping inside the loop.
202 */
203static void
204rpcauth_gc_credcache(struct rpc_auth *auth, struct hlist_head *free)
205{
206 struct rpc_cred_cache *cache = auth->au_credcache;
207 struct hlist_node *pos, *next;
208 struct rpc_cred *cred;
209 int i;
210
Chuck Lever46121cf2007-01-31 12:14:08 -0500211 dprintk("RPC: gc'ing RPC credentials for auth %p\n", auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 for (i = 0; i < RPC_CREDCACHE_NR; i++) {
213 hlist_for_each_safe(pos, next, &cache->hashtable[i]) {
214 cred = hlist_entry(pos, struct rpc_cred, cr_hash);
215 rpcauth_prune_expired(auth, cred, free);
216 }
217 }
218 cache->nextgc = jiffies + cache->expire;
219}
220
221/*
222 * Look up a process' credentials in the authentication cache
223 */
224struct rpc_cred *
225rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred,
Trond Myklebust8a317762006-02-01 12:18:36 -0500226 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 struct rpc_cred_cache *cache = auth->au_credcache;
229 HLIST_HEAD(free);
230 struct hlist_node *pos, *next;
231 struct rpc_cred *new = NULL,
232 *cred = NULL;
233 int nr = 0;
234
Trond Myklebust8a317762006-02-01 12:18:36 -0500235 if (!(flags & RPCAUTH_LOOKUP_ROOTCREDS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 nr = acred->uid & RPC_CREDCACHE_MASK;
237retry:
238 spin_lock(&rpc_credcache_lock);
239 if (time_before(cache->nextgc, jiffies))
240 rpcauth_gc_credcache(auth, &free);
241 hlist_for_each_safe(pos, next, &cache->hashtable[nr]) {
242 struct rpc_cred *entry;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800243 entry = hlist_entry(pos, struct rpc_cred, cr_hash);
Trond Myklebust8a317762006-02-01 12:18:36 -0500244 if (entry->cr_ops->crmatch(acred, entry, flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 hlist_del(&entry->cr_hash);
246 cred = entry;
247 break;
248 }
249 rpcauth_prune_expired(auth, entry, &free);
250 }
251 if (new) {
252 if (cred)
253 hlist_add_head(&new->cr_hash, &free);
254 else
255 cred = new;
256 }
257 if (cred) {
258 hlist_add_head(&cred->cr_hash, &cache->hashtable[nr]);
259 get_rpccred(cred);
260 }
261 spin_unlock(&rpc_credcache_lock);
262
263 rpcauth_destroy_credlist(&free);
264
265 if (!cred) {
Trond Myklebust8a317762006-02-01 12:18:36 -0500266 new = auth->au_ops->crcreate(auth, acred, flags);
Trond Myklebust5fe47552007-06-23 19:55:31 -0400267 if (!IS_ERR(new))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 goto retry;
Trond Myklebust5fe47552007-06-23 19:55:31 -0400269 cred = new;
Trond Myklebustfba3bad2006-02-01 12:19:27 -0500270 } else if ((cred->cr_flags & RPCAUTH_CRED_NEW)
271 && cred->cr_ops->cr_init != NULL
272 && !(flags & RPCAUTH_LOOKUP_NEW)) {
273 int res = cred->cr_ops->cr_init(auth, cred);
274 if (res < 0) {
275 put_rpccred(cred);
276 cred = ERR_PTR(res);
277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
279
280 return (struct rpc_cred *) cred;
281}
282
283struct rpc_cred *
Trond Myklebust8a317762006-02-01 12:18:36 -0500284rpcauth_lookupcred(struct rpc_auth *auth, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
286 struct auth_cred acred = {
287 .uid = current->fsuid,
288 .gid = current->fsgid,
289 .group_info = current->group_info,
290 };
291 struct rpc_cred *ret;
292
Chuck Lever46121cf2007-01-31 12:14:08 -0500293 dprintk("RPC: looking up %s cred\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 auth->au_ops->au_name);
295 get_group_info(acred.group_info);
Trond Myklebust8a317762006-02-01 12:18:36 -0500296 ret = auth->au_ops->lookup_cred(auth, &acred, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 put_group_info(acred.group_info);
298 return ret;
299}
300
Trond Myklebust5fe47552007-06-23 19:55:31 -0400301void
302rpcauth_init_cred(struct rpc_cred *cred, const struct auth_cred *acred,
303 struct rpc_auth *auth, const struct rpc_credops *ops)
304{
305 INIT_HLIST_NODE(&cred->cr_hash);
306 atomic_set(&cred->cr_count, 1);
307 cred->cr_auth = auth;
308 cred->cr_ops = ops;
309 cred->cr_expire = jiffies;
310#ifdef RPC_DEBUG
311 cred->cr_magic = RPCAUTH_CRED_MAGIC;
312#endif
313 cred->cr_uid = acred->uid;
314}
315EXPORT_SYMBOL(rpcauth_init_cred);
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317struct rpc_cred *
318rpcauth_bindcred(struct rpc_task *task)
319{
320 struct rpc_auth *auth = task->tk_auth;
321 struct auth_cred acred = {
322 .uid = current->fsuid,
323 .gid = current->fsgid,
324 .group_info = current->group_info,
325 };
326 struct rpc_cred *ret;
Trond Myklebust8a317762006-02-01 12:18:36 -0500327 int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Chuck Lever46121cf2007-01-31 12:14:08 -0500329 dprintk("RPC: %5u looking up %s cred\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 task->tk_pid, task->tk_auth->au_ops->au_name);
331 get_group_info(acred.group_info);
Trond Myklebust8a317762006-02-01 12:18:36 -0500332 if (task->tk_flags & RPC_TASK_ROOTCREDS)
333 flags |= RPCAUTH_LOOKUP_ROOTCREDS;
334 ret = auth->au_ops->lookup_cred(auth, &acred, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if (!IS_ERR(ret))
336 task->tk_msg.rpc_cred = ret;
337 else
338 task->tk_status = PTR_ERR(ret);
339 put_group_info(acred.group_info);
340 return ret;
341}
342
343void
344rpcauth_holdcred(struct rpc_task *task)
345{
Chuck Lever46121cf2007-01-31 12:14:08 -0500346 dprintk("RPC: %5u holding %s cred %p\n",
347 task->tk_pid, task->tk_auth->au_ops->au_name,
348 task->tk_msg.rpc_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (task->tk_msg.rpc_cred)
350 get_rpccred(task->tk_msg.rpc_cred);
351}
352
353void
354put_rpccred(struct rpc_cred *cred)
355{
356 cred->cr_expire = jiffies;
357 if (!atomic_dec_and_test(&cred->cr_count))
358 return;
359 cred->cr_ops->crdestroy(cred);
360}
361
362void
363rpcauth_unbindcred(struct rpc_task *task)
364{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 struct rpc_cred *cred = task->tk_msg.rpc_cred;
366
Chuck Lever46121cf2007-01-31 12:14:08 -0500367 dprintk("RPC: %5u releasing %s cred %p\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500368 task->tk_pid, task->tk_auth->au_ops->au_name, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 put_rpccred(cred);
371 task->tk_msg.rpc_cred = NULL;
372}
373
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700374__be32 *
375rpcauth_marshcred(struct rpc_task *task, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 struct rpc_cred *cred = task->tk_msg.rpc_cred;
378
Chuck Lever46121cf2007-01-31 12:14:08 -0500379 dprintk("RPC: %5u marshaling %s cred %p\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500380 task->tk_pid, task->tk_auth->au_ops->au_name, cred);
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return cred->cr_ops->crmarshal(task, p);
383}
384
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700385__be32 *
386rpcauth_checkverf(struct rpc_task *task, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 struct rpc_cred *cred = task->tk_msg.rpc_cred;
389
Chuck Lever46121cf2007-01-31 12:14:08 -0500390 dprintk("RPC: %5u validating %s cred %p\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500391 task->tk_pid, task->tk_auth->au_ops->au_name, cred);
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return cred->cr_ops->crvalidate(task, p);
394}
395
396int
397rpcauth_wrap_req(struct rpc_task *task, kxdrproc_t encode, void *rqstp,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700398 __be32 *data, void *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
400 struct rpc_cred *cred = task->tk_msg.rpc_cred;
401
Chuck Lever46121cf2007-01-31 12:14:08 -0500402 dprintk("RPC: %5u using %s cred %p to wrap rpc data\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 task->tk_pid, cred->cr_ops->cr_name, cred);
404 if (cred->cr_ops->crwrap_req)
405 return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj);
406 /* By default, we encode the arguments normally. */
407 return encode(rqstp, data, obj);
408}
409
410int
411rpcauth_unwrap_resp(struct rpc_task *task, kxdrproc_t decode, void *rqstp,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700412 __be32 *data, void *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 struct rpc_cred *cred = task->tk_msg.rpc_cred;
415
Chuck Lever46121cf2007-01-31 12:14:08 -0500416 dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 task->tk_pid, cred->cr_ops->cr_name, cred);
418 if (cred->cr_ops->crunwrap_resp)
419 return cred->cr_ops->crunwrap_resp(task, decode, rqstp,
420 data, obj);
421 /* By default, we decode the arguments normally. */
422 return decode(rqstp, data, obj);
423}
424
425int
426rpcauth_refreshcred(struct rpc_task *task)
427{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 struct rpc_cred *cred = task->tk_msg.rpc_cred;
429 int err;
430
Chuck Lever46121cf2007-01-31 12:14:08 -0500431 dprintk("RPC: %5u refreshing %s cred %p\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500432 task->tk_pid, task->tk_auth->au_ops->au_name, cred);
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 err = cred->cr_ops->crrefresh(task);
435 if (err < 0)
436 task->tk_status = err;
437 return err;
438}
439
440void
441rpcauth_invalcred(struct rpc_task *task)
442{
Chuck Lever46121cf2007-01-31 12:14:08 -0500443 dprintk("RPC: %5u invalidating %s cred %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 task->tk_pid, task->tk_auth->au_ops->au_name, task->tk_msg.rpc_cred);
445 spin_lock(&rpc_credcache_lock);
446 if (task->tk_msg.rpc_cred)
447 task->tk_msg.rpc_cred->cr_flags &= ~RPCAUTH_CRED_UPTODATE;
448 spin_unlock(&rpc_credcache_lock);
449}
450
451int
452rpcauth_uptodatecred(struct rpc_task *task)
453{
454 return !(task->tk_msg.rpc_cred) ||
455 (task->tk_msg.rpc_cred->cr_flags & RPCAUTH_CRED_UPTODATE);
456}