2 * linux/net/sunrpc/auth.c
4 * Generic RPC client authentication API.
6 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
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>
14 #include <linux/sunrpc/clnt.h>
15 #include <linux/spinlock.h>
18 # define RPCDBG_FACILITY RPCDBG_AUTH
21 static DEFINE_SPINLOCK(rpc_authflavor_lock);
22 static const struct rpc_authops *auth_flavors[RPC_AUTH_MAXFLAVOR] = {
23 &authnull_ops, /* AUTH_NULL */
24 &authunix_ops, /* AUTH_UNIX */
25 NULL, /* others can be loadable modules */
28 static LIST_HEAD(cred_unused);
31 pseudoflavor_to_flavor(u32 flavor) {
32 if (flavor >= RPC_AUTH_MAXFLAVOR)
38 rpcauth_register(const struct rpc_authops *ops)
40 rpc_authflavor_t flavor;
43 if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
45 spin_lock(&rpc_authflavor_lock);
46 if (auth_flavors[flavor] == NULL) {
47 auth_flavors[flavor] = ops;
50 spin_unlock(&rpc_authflavor_lock);
55 rpcauth_unregister(const struct rpc_authops *ops)
57 rpc_authflavor_t flavor;
60 if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
62 spin_lock(&rpc_authflavor_lock);
63 if (auth_flavors[flavor] == ops) {
64 auth_flavors[flavor] = NULL;
67 spin_unlock(&rpc_authflavor_lock);
72 rpcauth_create(rpc_authflavor_t pseudoflavor, struct rpc_clnt *clnt)
74 struct rpc_auth *auth;
75 const struct rpc_authops *ops;
76 u32 flavor = pseudoflavor_to_flavor(pseudoflavor);
78 auth = ERR_PTR(-EINVAL);
79 if (flavor >= RPC_AUTH_MAXFLAVOR)
83 if ((ops = auth_flavors[flavor]) == NULL)
84 request_module("rpc-auth-%u", flavor);
86 spin_lock(&rpc_authflavor_lock);
87 ops = auth_flavors[flavor];
88 if (ops == NULL || !try_module_get(ops->owner)) {
89 spin_unlock(&rpc_authflavor_lock);
92 spin_unlock(&rpc_authflavor_lock);
93 auth = ops->create(clnt, pseudoflavor);
94 module_put(ops->owner);
98 rpcauth_release(clnt->cl_auth);
106 rpcauth_release(struct rpc_auth *auth)
108 if (!atomic_dec_and_test(&auth->au_count))
110 auth->au_ops->destroy(auth);
113 static DEFINE_SPINLOCK(rpc_credcache_lock);
116 rpcauth_unhash_cred_locked(struct rpc_cred *cred)
118 hlist_del_rcu(&cred->cr_hash);
119 smp_mb__before_clear_bit();
120 clear_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
124 rpcauth_unhash_cred(struct rpc_cred *cred)
126 spinlock_t *cache_lock;
128 cache_lock = &cred->cr_auth->au_credcache->lock;
129 spin_lock(cache_lock);
130 if (atomic_read(&cred->cr_count) == 0)
131 rpcauth_unhash_cred_locked(cred);
132 spin_unlock(cache_lock);
136 * Initialize RPC credential cache
139 rpcauth_init_credcache(struct rpc_auth *auth, unsigned long expire)
141 struct rpc_cred_cache *new;
144 new = kmalloc(sizeof(*new), GFP_KERNEL);
147 for (i = 0; i < RPC_CREDCACHE_NR; i++)
148 INIT_HLIST_HEAD(&new->hashtable[i]);
149 spin_lock_init(&new->lock);
150 new->expire = expire;
151 new->nextgc = jiffies + (expire >> 1);
152 auth->au_credcache = new;
157 * Destroy a list of credentials
160 void rpcauth_destroy_credlist(struct list_head *head)
162 struct rpc_cred *cred;
164 while (!list_empty(head)) {
165 cred = list_entry(head->next, struct rpc_cred, cr_lru);
166 list_del_init(&cred->cr_lru);
172 * Clear the RPC credential cache, and delete those credentials
173 * that are not referenced.
176 rpcauth_clear_credcache(struct rpc_cred_cache *cache)
179 struct hlist_head *head;
180 struct rpc_cred *cred;
183 spin_lock(&rpc_credcache_lock);
184 spin_lock(&cache->lock);
185 for (i = 0; i < RPC_CREDCACHE_NR; i++) {
186 head = &cache->hashtable[i];
187 while (!hlist_empty(head)) {
188 cred = hlist_entry(head->first, struct rpc_cred, cr_hash);
190 list_move_tail(&cred->cr_lru, &free);
191 rpcauth_unhash_cred_locked(cred);
194 spin_unlock(&cache->lock);
195 spin_unlock(&rpc_credcache_lock);
196 rpcauth_destroy_credlist(&free);
200 * Destroy the RPC credential cache
203 rpcauth_destroy_credcache(struct rpc_auth *auth)
205 struct rpc_cred_cache *cache = auth->au_credcache;
208 auth->au_credcache = NULL;
209 rpcauth_clear_credcache(cache);
215 * Remove stale credentials. Avoid sleeping inside the loop.
218 rpcauth_prune_expired(struct list_head *free)
220 spinlock_t *cache_lock;
221 struct rpc_cred *cred;
223 while (!list_empty(&cred_unused)) {
224 cred = list_entry(cred_unused.next, struct rpc_cred, cr_lru);
225 if (time_after(jiffies, cred->cr_expire +
226 cred->cr_auth->au_credcache->expire))
228 list_del_init(&cred->cr_lru);
229 if (atomic_read(&cred->cr_count) != 0)
231 cache_lock = &cred->cr_auth->au_credcache->lock;
232 spin_lock(cache_lock);
233 if (atomic_read(&cred->cr_count) == 0) {
235 list_add_tail(&cred->cr_lru, free);
236 rpcauth_unhash_cred_locked(cred);
238 spin_unlock(cache_lock);
243 * Run garbage collector.
246 rpcauth_gc_credcache(struct rpc_cred_cache *cache, struct list_head *free)
248 if (list_empty(&cred_unused) || time_before(jiffies, cache->nextgc))
250 spin_lock(&rpc_credcache_lock);
251 cache->nextgc = jiffies + cache->expire;
252 rpcauth_prune_expired(free);
253 spin_unlock(&rpc_credcache_lock);
257 * Look up a process' credentials in the authentication cache
260 rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred,
264 struct rpc_cred_cache *cache = auth->au_credcache;
265 struct hlist_node *pos;
266 struct rpc_cred *cred = NULL,
270 if (!(flags & RPCAUTH_LOOKUP_ROOTCREDS))
271 nr = acred->uid & RPC_CREDCACHE_MASK;
274 hlist_for_each_entry_rcu(entry, pos, &cache->hashtable[nr], cr_hash) {
275 if (!entry->cr_ops->crmatch(acred, entry, flags))
277 spin_lock(&cache->lock);
278 if (test_bit(RPCAUTH_CRED_HASHED, &entry->cr_flags) == 0) {
279 spin_unlock(&cache->lock);
282 cred = get_rpccred(entry);
283 spin_unlock(&cache->lock);
291 new = auth->au_ops->crcreate(auth, acred, flags);
297 spin_lock(&cache->lock);
298 hlist_for_each_entry(entry, pos, &cache->hashtable[nr], cr_hash) {
299 if (!entry->cr_ops->crmatch(acred, entry, flags))
301 cred = get_rpccred(entry);
306 set_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
307 hlist_add_head_rcu(&cred->cr_hash, &cache->hashtable[nr]);
309 list_add_tail(&new->cr_lru, &free);
310 spin_unlock(&cache->lock);
312 if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags)
313 && cred->cr_ops->cr_init != NULL
314 && !(flags & RPCAUTH_LOOKUP_NEW)) {
315 int res = cred->cr_ops->cr_init(auth, cred);
321 rpcauth_gc_credcache(cache, &free);
322 rpcauth_destroy_credlist(&free);
328 rpcauth_lookupcred(struct rpc_auth *auth, int flags)
330 struct auth_cred acred = {
331 .uid = current->fsuid,
332 .gid = current->fsgid,
333 .group_info = current->group_info,
335 struct rpc_cred *ret;
337 dprintk("RPC: looking up %s cred\n",
338 auth->au_ops->au_name);
339 get_group_info(acred.group_info);
340 ret = auth->au_ops->lookup_cred(auth, &acred, flags);
341 put_group_info(acred.group_info);
346 rpcauth_init_cred(struct rpc_cred *cred, const struct auth_cred *acred,
347 struct rpc_auth *auth, const struct rpc_credops *ops)
349 INIT_HLIST_NODE(&cred->cr_hash);
350 INIT_LIST_HEAD(&cred->cr_lru);
351 atomic_set(&cred->cr_count, 1);
352 cred->cr_auth = auth;
354 cred->cr_expire = jiffies;
356 cred->cr_magic = RPCAUTH_CRED_MAGIC;
358 cred->cr_uid = acred->uid;
360 EXPORT_SYMBOL(rpcauth_init_cred);
363 rpcauth_bindcred(struct rpc_task *task)
365 struct rpc_auth *auth = task->tk_auth;
366 struct auth_cred acred = {
367 .uid = current->fsuid,
368 .gid = current->fsgid,
369 .group_info = current->group_info,
371 struct rpc_cred *ret;
374 dprintk("RPC: %5u looking up %s cred\n",
375 task->tk_pid, task->tk_auth->au_ops->au_name);
376 get_group_info(acred.group_info);
377 if (task->tk_flags & RPC_TASK_ROOTCREDS)
378 flags |= RPCAUTH_LOOKUP_ROOTCREDS;
379 ret = auth->au_ops->lookup_cred(auth, &acred, flags);
381 task->tk_msg.rpc_cred = ret;
383 task->tk_status = PTR_ERR(ret);
384 put_group_info(acred.group_info);
389 rpcauth_holdcred(struct rpc_task *task)
391 dprintk("RPC: %5u holding %s cred %p\n",
392 task->tk_pid, task->tk_auth->au_ops->au_name,
393 task->tk_msg.rpc_cred);
394 if (task->tk_msg.rpc_cred)
395 get_rpccred(task->tk_msg.rpc_cred);
399 put_rpccred(struct rpc_cred *cred)
401 /* Fast path for unhashed credentials */
402 if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0)
405 if (!atomic_dec_and_test(&cred->cr_count))
409 if (!atomic_dec_and_lock(&cred->cr_count, &rpc_credcache_lock))
411 if (!list_empty(&cred->cr_lru))
412 list_del_init(&cred->cr_lru);
413 if (test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) == 0)
414 rpcauth_unhash_cred(cred);
415 else if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) {
416 cred->cr_expire = jiffies;
417 list_add_tail(&cred->cr_lru, &cred_unused);
418 spin_unlock(&rpc_credcache_lock);
421 spin_unlock(&rpc_credcache_lock);
423 cred->cr_ops->crdestroy(cred);
427 rpcauth_unbindcred(struct rpc_task *task)
429 struct rpc_cred *cred = task->tk_msg.rpc_cred;
431 dprintk("RPC: %5u releasing %s cred %p\n",
432 task->tk_pid, task->tk_auth->au_ops->au_name, cred);
435 task->tk_msg.rpc_cred = NULL;
439 rpcauth_marshcred(struct rpc_task *task, __be32 *p)
441 struct rpc_cred *cred = task->tk_msg.rpc_cred;
443 dprintk("RPC: %5u marshaling %s cred %p\n",
444 task->tk_pid, task->tk_auth->au_ops->au_name, cred);
446 return cred->cr_ops->crmarshal(task, p);
450 rpcauth_checkverf(struct rpc_task *task, __be32 *p)
452 struct rpc_cred *cred = task->tk_msg.rpc_cred;
454 dprintk("RPC: %5u validating %s cred %p\n",
455 task->tk_pid, task->tk_auth->au_ops->au_name, cred);
457 return cred->cr_ops->crvalidate(task, p);
461 rpcauth_wrap_req(struct rpc_task *task, kxdrproc_t encode, void *rqstp,
462 __be32 *data, void *obj)
464 struct rpc_cred *cred = task->tk_msg.rpc_cred;
466 dprintk("RPC: %5u using %s cred %p to wrap rpc data\n",
467 task->tk_pid, cred->cr_ops->cr_name, cred);
468 if (cred->cr_ops->crwrap_req)
469 return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj);
470 /* By default, we encode the arguments normally. */
471 return encode(rqstp, data, obj);
475 rpcauth_unwrap_resp(struct rpc_task *task, kxdrproc_t decode, void *rqstp,
476 __be32 *data, void *obj)
478 struct rpc_cred *cred = task->tk_msg.rpc_cred;
480 dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n",
481 task->tk_pid, cred->cr_ops->cr_name, cred);
482 if (cred->cr_ops->crunwrap_resp)
483 return cred->cr_ops->crunwrap_resp(task, decode, rqstp,
485 /* By default, we decode the arguments normally. */
486 return decode(rqstp, data, obj);
490 rpcauth_refreshcred(struct rpc_task *task)
492 struct rpc_cred *cred = task->tk_msg.rpc_cred;
495 dprintk("RPC: %5u refreshing %s cred %p\n",
496 task->tk_pid, task->tk_auth->au_ops->au_name, cred);
498 err = cred->cr_ops->crrefresh(task);
500 task->tk_status = err;
505 rpcauth_invalcred(struct rpc_task *task)
507 struct rpc_cred *cred = task->tk_msg.rpc_cred;
509 dprintk("RPC: %5u invalidating %s cred %p\n",
510 task->tk_pid, task->tk_auth->au_ops->au_name, cred);
512 clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
516 rpcauth_uptodatecred(struct rpc_task *task)
518 struct rpc_cred *cred = task->tk_msg.rpc_cred;
520 return cred == NULL ||
521 test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0;