blob: 81f4c776c558ef0ebe49318a1435acc5685ac06e [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
Trond Myklebuste092bdc2007-06-23 19:45:36 -040028static LIST_HEAD(cred_unused);
Trond Myklebustf5c21872007-06-25 17:11:20 -040029static unsigned long number_cred_unused;
Trond Myklebuste092bdc2007-06-23 19:45:36 -040030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static u32
32pseudoflavor_to_flavor(u32 flavor) {
33 if (flavor >= RPC_AUTH_MAXFLAVOR)
34 return RPC_AUTH_GSS;
35 return flavor;
36}
37
38int
Trond Myklebustf1c0a862007-06-23 20:17:58 -040039rpcauth_register(const struct rpc_authops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
41 rpc_authflavor_t flavor;
Trond Myklebustfc1b3562007-06-09 16:15:46 -040042 int ret = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44 if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
45 return -EINVAL;
Trond Myklebustfc1b3562007-06-09 16:15:46 -040046 spin_lock(&rpc_authflavor_lock);
47 if (auth_flavors[flavor] == NULL) {
48 auth_flavors[flavor] = ops;
49 ret = 0;
50 }
51 spin_unlock(&rpc_authflavor_lock);
52 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
54
55int
Trond Myklebustf1c0a862007-06-23 20:17:58 -040056rpcauth_unregister(const struct rpc_authops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
58 rpc_authflavor_t flavor;
Trond Myklebustfc1b3562007-06-09 16:15:46 -040059 int ret = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61 if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
62 return -EINVAL;
Trond Myklebustfc1b3562007-06-09 16:15:46 -040063 spin_lock(&rpc_authflavor_lock);
64 if (auth_flavors[flavor] == ops) {
65 auth_flavors[flavor] = NULL;
66 ret = 0;
67 }
68 spin_unlock(&rpc_authflavor_lock);
69 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
71
72struct rpc_auth *
73rpcauth_create(rpc_authflavor_t pseudoflavor, struct rpc_clnt *clnt)
74{
75 struct rpc_auth *auth;
Trond Myklebustf1c0a862007-06-23 20:17:58 -040076 const struct rpc_authops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 u32 flavor = pseudoflavor_to_flavor(pseudoflavor);
78
Olaf Kirchf344f6d2006-03-20 13:44:08 -050079 auth = ERR_PTR(-EINVAL);
80 if (flavor >= RPC_AUTH_MAXFLAVOR)
81 goto out;
82
Olaf Kirchf344f6d2006-03-20 13:44:08 -050083#ifdef CONFIG_KMOD
84 if ((ops = auth_flavors[flavor]) == NULL)
85 request_module("rpc-auth-%u", flavor);
86#endif
Trond Myklebustfc1b3562007-06-09 16:15:46 -040087 spin_lock(&rpc_authflavor_lock);
88 ops = auth_flavors[flavor];
89 if (ops == NULL || !try_module_get(ops->owner)) {
90 spin_unlock(&rpc_authflavor_lock);
Olaf Kirchf344f6d2006-03-20 13:44:08 -050091 goto out;
Trond Myklebustfc1b3562007-06-09 16:15:46 -040092 }
93 spin_unlock(&rpc_authflavor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 auth = ops->create(clnt, pseudoflavor);
Trond Myklebustfc1b3562007-06-09 16:15:46 -040095 module_put(ops->owner);
J. Bruce Fields6a192752005-06-22 17:16:23 +000096 if (IS_ERR(auth))
97 return auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 if (clnt->cl_auth)
Trond Myklebustde7a8ce2007-06-23 10:46:47 -040099 rpcauth_release(clnt->cl_auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 clnt->cl_auth = auth;
Olaf Kirchf344f6d2006-03-20 13:44:08 -0500101
102out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return auth;
104}
105
106void
Trond Myklebustde7a8ce2007-06-23 10:46:47 -0400107rpcauth_release(struct rpc_auth *auth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 if (!atomic_dec_and_test(&auth->au_count))
110 return;
111 auth->au_ops->destroy(auth);
112}
113
114static DEFINE_SPINLOCK(rpc_credcache_lock);
115
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400116static void
117rpcauth_unhash_cred_locked(struct rpc_cred *cred)
118{
119 hlist_del_rcu(&cred->cr_hash);
120 smp_mb__before_clear_bit();
121 clear_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
122}
123
Trond Myklebust9499b432007-06-24 15:57:57 -0400124static void
125rpcauth_unhash_cred(struct rpc_cred *cred)
126{
127 spinlock_t *cache_lock;
128
129 cache_lock = &cred->cr_auth->au_credcache->lock;
130 spin_lock(cache_lock);
131 if (atomic_read(&cred->cr_count) == 0)
132 rpcauth_unhash_cred_locked(cred);
133 spin_unlock(cache_lock);
134}
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136/*
137 * Initialize RPC credential cache
138 */
139int
Trond Myklebustf5c21872007-06-25 17:11:20 -0400140rpcauth_init_credcache(struct rpc_auth *auth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 struct rpc_cred_cache *new;
143 int i;
144
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800145 new = kmalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (!new)
147 return -ENOMEM;
148 for (i = 0; i < RPC_CREDCACHE_NR; i++)
149 INIT_HLIST_HEAD(&new->hashtable[i]);
Trond Myklebust9499b432007-06-24 15:57:57 -0400150 spin_lock_init(&new->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 auth->au_credcache = new;
152 return 0;
153}
154
155/*
156 * Destroy a list of credentials
157 */
158static inline
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400159void rpcauth_destroy_credlist(struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 struct rpc_cred *cred;
162
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400163 while (!list_empty(head)) {
164 cred = list_entry(head->next, struct rpc_cred, cr_lru);
165 list_del_init(&cred->cr_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 put_rpccred(cred);
167 }
168}
169
170/*
171 * Clear the RPC credential cache, and delete those credentials
172 * that are not referenced.
173 */
174void
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400175rpcauth_clear_credcache(struct rpc_cred_cache *cache)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400177 LIST_HEAD(free);
178 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 struct rpc_cred *cred;
180 int i;
181
182 spin_lock(&rpc_credcache_lock);
Trond Myklebust9499b432007-06-24 15:57:57 -0400183 spin_lock(&cache->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 for (i = 0; i < RPC_CREDCACHE_NR; i++) {
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400185 head = &cache->hashtable[i];
186 while (!hlist_empty(head)) {
187 cred = hlist_entry(head->first, struct rpc_cred, cr_hash);
188 get_rpccred(cred);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400189 if (!list_empty(&cred->cr_lru)) {
190 list_del(&cred->cr_lru);
191 number_cred_unused--;
192 }
193 list_add_tail(&cred->cr_lru, &free);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400194 rpcauth_unhash_cred_locked(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
196 }
Trond Myklebust9499b432007-06-24 15:57:57 -0400197 spin_unlock(&cache->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 spin_unlock(&rpc_credcache_lock);
199 rpcauth_destroy_credlist(&free);
200}
201
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400202/*
203 * Destroy the RPC credential cache
204 */
205void
206rpcauth_destroy_credcache(struct rpc_auth *auth)
207{
208 struct rpc_cred_cache *cache = auth->au_credcache;
209
210 if (cache) {
211 auth->au_credcache = NULL;
212 rpcauth_clear_credcache(cache);
213 kfree(cache);
214 }
215}
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217/*
218 * Remove stale credentials. Avoid sleeping inside the loop.
219 */
Trond Myklebustf5c21872007-06-25 17:11:20 -0400220static int
221rpcauth_prune_expired(struct list_head *free, int nr_to_scan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
Trond Myklebust9499b432007-06-24 15:57:57 -0400223 spinlock_t *cache_lock;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400224 struct rpc_cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400226 while (!list_empty(&cred_unused)) {
227 cred = list_entry(cred_unused.next, struct rpc_cred, cr_lru);
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400228 list_del_init(&cred->cr_lru);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400229 number_cred_unused--;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400230 if (atomic_read(&cred->cr_count) != 0)
231 continue;
Trond Myklebust9499b432007-06-24 15:57:57 -0400232 cache_lock = &cred->cr_auth->au_credcache->lock;
233 spin_lock(cache_lock);
234 if (atomic_read(&cred->cr_count) == 0) {
235 get_rpccred(cred);
236 list_add_tail(&cred->cr_lru, free);
237 rpcauth_unhash_cred_locked(cred);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400238 nr_to_scan--;
Trond Myklebust9499b432007-06-24 15:57:57 -0400239 }
240 spin_unlock(cache_lock);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400241 if (nr_to_scan == 0)
242 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
Trond Myklebustf5c21872007-06-25 17:11:20 -0400244 return nr_to_scan;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400245}
246
247/*
Trond Myklebustf5c21872007-06-25 17:11:20 -0400248 * Run memory cache shrinker.
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400249 */
Trond Myklebustf5c21872007-06-25 17:11:20 -0400250static int
251rpcauth_cache_shrinker(int nr_to_scan, gfp_t gfp_mask)
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400252{
Trond Myklebustf5c21872007-06-25 17:11:20 -0400253 LIST_HEAD(free);
254 int res;
255
256 if (list_empty(&cred_unused))
257 return 0;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400258 spin_lock(&rpc_credcache_lock);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400259 nr_to_scan = rpcauth_prune_expired(&free, nr_to_scan);
260 res = (number_cred_unused / 100) * sysctl_vfs_cache_pressure;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400261 spin_unlock(&rpc_credcache_lock);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400262 rpcauth_destroy_credlist(&free);
263 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
266/*
267 * Look up a process' credentials in the authentication cache
268 */
269struct rpc_cred *
270rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred,
Trond Myklebust8a317762006-02-01 12:18:36 -0500271 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400273 LIST_HEAD(free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 struct rpc_cred_cache *cache = auth->au_credcache;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400275 struct hlist_node *pos;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400276 struct rpc_cred *cred = NULL,
277 *entry, *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 int nr = 0;
279
Trond Myklebust8a317762006-02-01 12:18:36 -0500280 if (!(flags & RPCAUTH_LOOKUP_ROOTCREDS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 nr = acred->uid & RPC_CREDCACHE_MASK;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400282
283 rcu_read_lock();
284 hlist_for_each_entry_rcu(entry, pos, &cache->hashtable[nr], cr_hash) {
285 if (!entry->cr_ops->crmatch(acred, entry, flags))
286 continue;
Trond Myklebust9499b432007-06-24 15:57:57 -0400287 spin_lock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400288 if (test_bit(RPCAUTH_CRED_HASHED, &entry->cr_flags) == 0) {
Trond Myklebust9499b432007-06-24 15:57:57 -0400289 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400290 continue;
291 }
292 cred = get_rpccred(entry);
Trond Myklebust9499b432007-06-24 15:57:57 -0400293 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400294 break;
295 }
296 rcu_read_unlock();
297
Trond Myklebust9499b432007-06-24 15:57:57 -0400298 if (cred != NULL)
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400299 goto found;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400300
301 new = auth->au_ops->crcreate(auth, acred, flags);
302 if (IS_ERR(new)) {
303 cred = new;
304 goto out;
305 }
306
Trond Myklebust9499b432007-06-24 15:57:57 -0400307 spin_lock(&cache->lock);
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400308 hlist_for_each_entry(entry, pos, &cache->hashtable[nr], cr_hash) {
309 if (!entry->cr_ops->crmatch(acred, entry, flags))
310 continue;
311 cred = get_rpccred(entry);
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400312 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400314 if (cred == NULL) {
Trond Myklebust5fe47552007-06-23 19:55:31 -0400315 cred = new;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400316 set_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
317 hlist_add_head_rcu(&cred->cr_hash, &cache->hashtable[nr]);
318 } else
319 list_add_tail(&new->cr_lru, &free);
Trond Myklebust9499b432007-06-24 15:57:57 -0400320 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400321found:
322 if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags)
Trond Myklebustfba3bad2006-02-01 12:19:27 -0500323 && cred->cr_ops->cr_init != NULL
324 && !(flags & RPCAUTH_LOOKUP_NEW)) {
325 int res = cred->cr_ops->cr_init(auth, cred);
326 if (res < 0) {
327 put_rpccred(cred);
328 cred = ERR_PTR(res);
329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400331 rpcauth_destroy_credlist(&free);
332out:
333 return cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
336struct rpc_cred *
Trond Myklebust8a317762006-02-01 12:18:36 -0500337rpcauth_lookupcred(struct rpc_auth *auth, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339 struct auth_cred acred = {
340 .uid = current->fsuid,
341 .gid = current->fsgid,
342 .group_info = current->group_info,
343 };
344 struct rpc_cred *ret;
345
Chuck Lever46121cf2007-01-31 12:14:08 -0500346 dprintk("RPC: looking up %s cred\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 auth->au_ops->au_name);
348 get_group_info(acred.group_info);
Trond Myklebust8a317762006-02-01 12:18:36 -0500349 ret = auth->au_ops->lookup_cred(auth, &acred, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 put_group_info(acred.group_info);
351 return ret;
352}
353
Trond Myklebust5fe47552007-06-23 19:55:31 -0400354void
355rpcauth_init_cred(struct rpc_cred *cred, const struct auth_cred *acred,
356 struct rpc_auth *auth, const struct rpc_credops *ops)
357{
358 INIT_HLIST_NODE(&cred->cr_hash);
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400359 INIT_LIST_HEAD(&cred->cr_lru);
Trond Myklebust5fe47552007-06-23 19:55:31 -0400360 atomic_set(&cred->cr_count, 1);
361 cred->cr_auth = auth;
362 cred->cr_ops = ops;
363 cred->cr_expire = jiffies;
364#ifdef RPC_DEBUG
365 cred->cr_magic = RPCAUTH_CRED_MAGIC;
366#endif
367 cred->cr_uid = acred->uid;
368}
369EXPORT_SYMBOL(rpcauth_init_cred);
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371struct rpc_cred *
372rpcauth_bindcred(struct rpc_task *task)
373{
374 struct rpc_auth *auth = task->tk_auth;
375 struct auth_cred acred = {
376 .uid = current->fsuid,
377 .gid = current->fsgid,
378 .group_info = current->group_info,
379 };
380 struct rpc_cred *ret;
Trond Myklebust8a317762006-02-01 12:18:36 -0500381 int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Chuck Lever46121cf2007-01-31 12:14:08 -0500383 dprintk("RPC: %5u looking up %s cred\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 task->tk_pid, task->tk_auth->au_ops->au_name);
385 get_group_info(acred.group_info);
Trond Myklebust8a317762006-02-01 12:18:36 -0500386 if (task->tk_flags & RPC_TASK_ROOTCREDS)
387 flags |= RPCAUTH_LOOKUP_ROOTCREDS;
388 ret = auth->au_ops->lookup_cred(auth, &acred, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (!IS_ERR(ret))
390 task->tk_msg.rpc_cred = ret;
391 else
392 task->tk_status = PTR_ERR(ret);
393 put_group_info(acred.group_info);
394 return ret;
395}
396
397void
398rpcauth_holdcred(struct rpc_task *task)
399{
Chuck Lever46121cf2007-01-31 12:14:08 -0500400 dprintk("RPC: %5u holding %s cred %p\n",
401 task->tk_pid, task->tk_auth->au_ops->au_name,
402 task->tk_msg.rpc_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 if (task->tk_msg.rpc_cred)
404 get_rpccred(task->tk_msg.rpc_cred);
405}
406
407void
408put_rpccred(struct rpc_cred *cred)
409{
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400410 /* Fast path for unhashed credentials */
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400411 if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0)
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400412 goto need_lock;
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (!atomic_dec_and_test(&cred->cr_count))
415 return;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400416 goto out_destroy;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400417need_lock:
418 if (!atomic_dec_and_lock(&cred->cr_count, &rpc_credcache_lock))
419 return;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400420 if (!list_empty(&cred->cr_lru)) {
421 number_cred_unused--;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400422 list_del_init(&cred->cr_lru);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400423 }
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400424 if (test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) == 0)
Trond Myklebust9499b432007-06-24 15:57:57 -0400425 rpcauth_unhash_cred(cred);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400426 else if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) {
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400427 cred->cr_expire = jiffies;
428 list_add_tail(&cred->cr_lru, &cred_unused);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400429 number_cred_unused++;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400430 spin_unlock(&rpc_credcache_lock);
431 return;
432 }
433 spin_unlock(&rpc_credcache_lock);
434out_destroy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 cred->cr_ops->crdestroy(cred);
436}
437
438void
439rpcauth_unbindcred(struct rpc_task *task)
440{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 struct rpc_cred *cred = task->tk_msg.rpc_cred;
442
Chuck Lever46121cf2007-01-31 12:14:08 -0500443 dprintk("RPC: %5u releasing %s cred %p\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500444 task->tk_pid, task->tk_auth->au_ops->au_name, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 put_rpccred(cred);
447 task->tk_msg.rpc_cred = NULL;
448}
449
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700450__be32 *
451rpcauth_marshcred(struct rpc_task *task, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 struct rpc_cred *cred = task->tk_msg.rpc_cred;
454
Chuck Lever46121cf2007-01-31 12:14:08 -0500455 dprintk("RPC: %5u marshaling %s cred %p\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500456 task->tk_pid, task->tk_auth->au_ops->au_name, cred);
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return cred->cr_ops->crmarshal(task, p);
459}
460
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700461__be32 *
462rpcauth_checkverf(struct rpc_task *task, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 struct rpc_cred *cred = task->tk_msg.rpc_cred;
465
Chuck Lever46121cf2007-01-31 12:14:08 -0500466 dprintk("RPC: %5u validating %s cred %p\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500467 task->tk_pid, task->tk_auth->au_ops->au_name, cred);
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return cred->cr_ops->crvalidate(task, p);
470}
471
472int
473rpcauth_wrap_req(struct rpc_task *task, kxdrproc_t encode, void *rqstp,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700474 __be32 *data, void *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
476 struct rpc_cred *cred = task->tk_msg.rpc_cred;
477
Chuck Lever46121cf2007-01-31 12:14:08 -0500478 dprintk("RPC: %5u using %s cred %p to wrap rpc data\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 task->tk_pid, cred->cr_ops->cr_name, cred);
480 if (cred->cr_ops->crwrap_req)
481 return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj);
482 /* By default, we encode the arguments normally. */
483 return encode(rqstp, data, obj);
484}
485
486int
487rpcauth_unwrap_resp(struct rpc_task *task, kxdrproc_t decode, void *rqstp,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700488 __be32 *data, void *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 struct rpc_cred *cred = task->tk_msg.rpc_cred;
491
Chuck Lever46121cf2007-01-31 12:14:08 -0500492 dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 task->tk_pid, cred->cr_ops->cr_name, cred);
494 if (cred->cr_ops->crunwrap_resp)
495 return cred->cr_ops->crunwrap_resp(task, decode, rqstp,
496 data, obj);
497 /* By default, we decode the arguments normally. */
498 return decode(rqstp, data, obj);
499}
500
501int
502rpcauth_refreshcred(struct rpc_task *task)
503{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 struct rpc_cred *cred = task->tk_msg.rpc_cred;
505 int err;
506
Chuck Lever46121cf2007-01-31 12:14:08 -0500507 dprintk("RPC: %5u refreshing %s cred %p\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500508 task->tk_pid, task->tk_auth->au_ops->au_name, cred);
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 err = cred->cr_ops->crrefresh(task);
511 if (err < 0)
512 task->tk_status = err;
513 return err;
514}
515
516void
517rpcauth_invalcred(struct rpc_task *task)
518{
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400519 struct rpc_cred *cred = task->tk_msg.rpc_cred;
520
Chuck Lever46121cf2007-01-31 12:14:08 -0500521 dprintk("RPC: %5u invalidating %s cred %p\n",
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400522 task->tk_pid, task->tk_auth->au_ops->au_name, cred);
523 if (cred)
524 clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
527int
528rpcauth_uptodatecred(struct rpc_task *task)
529{
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400530 struct rpc_cred *cred = task->tk_msg.rpc_cred;
531
532 return cred == NULL ||
533 test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
Trond Myklebustf5c21872007-06-25 17:11:20 -0400535
536
537static struct shrinker *rpc_cred_shrinker;
538
539void __init rpcauth_init_module(void)
540{
541 rpc_init_authunix();
542 rpc_cred_shrinker = set_shrinker(DEFAULT_SEEKS, rpcauth_cache_shrinker);
543}
544
545void __exit rpcauth_remove_module(void)
546{
547 if (rpc_cred_shrinker != NULL)
548 remove_shrinker(rpc_cred_shrinker);
549}