blob: 29a8ecc609284db068aa32d1b5a243a988c03715 [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>
J. Bruce Fieldsd8558f92007-07-10 15:19:26 -040016#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#ifdef RPC_DEBUG
19# define RPCDBG_FACILITY RPCDBG_AUTH
20#endif
21
Trond Myklebustfc1b3562007-06-09 16:15:46 -040022static DEFINE_SPINLOCK(rpc_authflavor_lock);
Trond Myklebustf1c0a862007-06-23 20:17:58 -040023static const struct rpc_authops *auth_flavors[RPC_AUTH_MAXFLAVOR] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 &authnull_ops, /* AUTH_NULL */
25 &authunix_ops, /* AUTH_UNIX */
26 NULL, /* others can be loadable modules */
27};
28
Trond Myklebuste092bdc2007-06-23 19:45:36 -040029static LIST_HEAD(cred_unused);
Trond Myklebustf5c21872007-06-25 17:11:20 -040030static unsigned long number_cred_unused;
Trond Myklebuste092bdc2007-06-23 19:45:36 -040031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032static u32
33pseudoflavor_to_flavor(u32 flavor) {
34 if (flavor >= RPC_AUTH_MAXFLAVOR)
35 return RPC_AUTH_GSS;
36 return flavor;
37}
38
39int
Trond Myklebustf1c0a862007-06-23 20:17:58 -040040rpcauth_register(const struct rpc_authops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
42 rpc_authflavor_t flavor;
Trond Myklebustfc1b3562007-06-09 16:15:46 -040043 int ret = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45 if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
46 return -EINVAL;
Trond Myklebustfc1b3562007-06-09 16:15:46 -040047 spin_lock(&rpc_authflavor_lock);
48 if (auth_flavors[flavor] == NULL) {
49 auth_flavors[flavor] = ops;
50 ret = 0;
51 }
52 spin_unlock(&rpc_authflavor_lock);
53 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55
56int
Trond Myklebustf1c0a862007-06-23 20:17:58 -040057rpcauth_unregister(const struct rpc_authops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
59 rpc_authflavor_t flavor;
Trond Myklebustfc1b3562007-06-09 16:15:46 -040060 int ret = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
63 return -EINVAL;
Trond Myklebustfc1b3562007-06-09 16:15:46 -040064 spin_lock(&rpc_authflavor_lock);
65 if (auth_flavors[flavor] == ops) {
66 auth_flavors[flavor] = NULL;
67 ret = 0;
68 }
69 spin_unlock(&rpc_authflavor_lock);
70 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72
73struct rpc_auth *
74rpcauth_create(rpc_authflavor_t pseudoflavor, struct rpc_clnt *clnt)
75{
76 struct rpc_auth *auth;
Trond Myklebustf1c0a862007-06-23 20:17:58 -040077 const struct rpc_authops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 u32 flavor = pseudoflavor_to_flavor(pseudoflavor);
79
Olaf Kirchf344f6d2006-03-20 13:44:08 -050080 auth = ERR_PTR(-EINVAL);
81 if (flavor >= RPC_AUTH_MAXFLAVOR)
82 goto out;
83
Olaf Kirchf344f6d2006-03-20 13:44:08 -050084#ifdef CONFIG_KMOD
85 if ((ops = auth_flavors[flavor]) == NULL)
86 request_module("rpc-auth-%u", flavor);
87#endif
Trond Myklebustfc1b3562007-06-09 16:15:46 -040088 spin_lock(&rpc_authflavor_lock);
89 ops = auth_flavors[flavor];
90 if (ops == NULL || !try_module_get(ops->owner)) {
91 spin_unlock(&rpc_authflavor_lock);
Olaf Kirchf344f6d2006-03-20 13:44:08 -050092 goto out;
Trond Myklebustfc1b3562007-06-09 16:15:46 -040093 }
94 spin_unlock(&rpc_authflavor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 auth = ops->create(clnt, pseudoflavor);
Trond Myklebustfc1b3562007-06-09 16:15:46 -040096 module_put(ops->owner);
J. Bruce Fields6a192752005-06-22 17:16:23 +000097 if (IS_ERR(auth))
98 return auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 if (clnt->cl_auth)
Trond Myklebustde7a8ce2007-06-23 10:46:47 -0400100 rpcauth_release(clnt->cl_auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 clnt->cl_auth = auth;
Olaf Kirchf344f6d2006-03-20 13:44:08 -0500102
103out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return auth;
105}
106
107void
Trond Myklebustde7a8ce2007-06-23 10:46:47 -0400108rpcauth_release(struct rpc_auth *auth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 if (!atomic_dec_and_test(&auth->au_count))
111 return;
112 auth->au_ops->destroy(auth);
113}
114
115static DEFINE_SPINLOCK(rpc_credcache_lock);
116
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400117static void
118rpcauth_unhash_cred_locked(struct rpc_cred *cred)
119{
120 hlist_del_rcu(&cred->cr_hash);
121 smp_mb__before_clear_bit();
122 clear_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
123}
124
Trond Myklebust9499b432007-06-24 15:57:57 -0400125static void
126rpcauth_unhash_cred(struct rpc_cred *cred)
127{
128 spinlock_t *cache_lock;
129
130 cache_lock = &cred->cr_auth->au_credcache->lock;
131 spin_lock(cache_lock);
132 if (atomic_read(&cred->cr_count) == 0)
133 rpcauth_unhash_cred_locked(cred);
134 spin_unlock(cache_lock);
135}
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137/*
138 * Initialize RPC credential cache
139 */
140int
Trond Myklebustf5c21872007-06-25 17:11:20 -0400141rpcauth_init_credcache(struct rpc_auth *auth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 struct rpc_cred_cache *new;
144 int i;
145
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800146 new = kmalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (!new)
148 return -ENOMEM;
149 for (i = 0; i < RPC_CREDCACHE_NR; i++)
150 INIT_HLIST_HEAD(&new->hashtable[i]);
Trond Myklebust9499b432007-06-24 15:57:57 -0400151 spin_lock_init(&new->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 auth->au_credcache = new;
153 return 0;
154}
155
156/*
157 * Destroy a list of credentials
158 */
159static inline
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400160void rpcauth_destroy_credlist(struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 struct rpc_cred *cred;
163
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400164 while (!list_empty(head)) {
165 cred = list_entry(head->next, struct rpc_cred, cr_lru);
166 list_del_init(&cred->cr_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 put_rpccred(cred);
168 }
169}
170
171/*
172 * Clear the RPC credential cache, and delete those credentials
173 * that are not referenced.
174 */
175void
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400176rpcauth_clear_credcache(struct rpc_cred_cache *cache)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400178 LIST_HEAD(free);
179 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 struct rpc_cred *cred;
181 int i;
182
183 spin_lock(&rpc_credcache_lock);
Trond Myklebust9499b432007-06-24 15:57:57 -0400184 spin_lock(&cache->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 for (i = 0; i < RPC_CREDCACHE_NR; i++) {
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400186 head = &cache->hashtable[i];
187 while (!hlist_empty(head)) {
188 cred = hlist_entry(head->first, struct rpc_cred, cr_hash);
189 get_rpccred(cred);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400190 if (!list_empty(&cred->cr_lru)) {
191 list_del(&cred->cr_lru);
192 number_cred_unused--;
193 }
194 list_add_tail(&cred->cr_lru, &free);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400195 rpcauth_unhash_cred_locked(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
197 }
Trond Myklebust9499b432007-06-24 15:57:57 -0400198 spin_unlock(&cache->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 spin_unlock(&rpc_credcache_lock);
200 rpcauth_destroy_credlist(&free);
201}
202
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400203/*
204 * Destroy the RPC credential cache
205 */
206void
207rpcauth_destroy_credcache(struct rpc_auth *auth)
208{
209 struct rpc_cred_cache *cache = auth->au_credcache;
210
211 if (cache) {
212 auth->au_credcache = NULL;
213 rpcauth_clear_credcache(cache);
214 kfree(cache);
215 }
216}
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218/*
219 * Remove stale credentials. Avoid sleeping inside the loop.
220 */
Trond Myklebustf5c21872007-06-25 17:11:20 -0400221static int
222rpcauth_prune_expired(struct list_head *free, int nr_to_scan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Trond Myklebust9499b432007-06-24 15:57:57 -0400224 spinlock_t *cache_lock;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400225 struct rpc_cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400227 while (!list_empty(&cred_unused)) {
228 cred = list_entry(cred_unused.next, struct rpc_cred, cr_lru);
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400229 list_del_init(&cred->cr_lru);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400230 number_cred_unused--;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400231 if (atomic_read(&cred->cr_count) != 0)
232 continue;
Trond Myklebust9499b432007-06-24 15:57:57 -0400233 cache_lock = &cred->cr_auth->au_credcache->lock;
234 spin_lock(cache_lock);
235 if (atomic_read(&cred->cr_count) == 0) {
236 get_rpccred(cred);
237 list_add_tail(&cred->cr_lru, free);
238 rpcauth_unhash_cred_locked(cred);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400239 nr_to_scan--;
Trond Myklebust9499b432007-06-24 15:57:57 -0400240 }
241 spin_unlock(cache_lock);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400242 if (nr_to_scan == 0)
243 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
Trond Myklebustf5c21872007-06-25 17:11:20 -0400245 return nr_to_scan;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400246}
247
248/*
Trond Myklebustf5c21872007-06-25 17:11:20 -0400249 * Run memory cache shrinker.
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400250 */
Trond Myklebustf5c21872007-06-25 17:11:20 -0400251static int
252rpcauth_cache_shrinker(int nr_to_scan, gfp_t gfp_mask)
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400253{
Trond Myklebustf5c21872007-06-25 17:11:20 -0400254 LIST_HEAD(free);
255 int res;
256
257 if (list_empty(&cred_unused))
258 return 0;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400259 spin_lock(&rpc_credcache_lock);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400260 nr_to_scan = rpcauth_prune_expired(&free, nr_to_scan);
261 res = (number_cred_unused / 100) * sysctl_vfs_cache_pressure;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400262 spin_unlock(&rpc_credcache_lock);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400263 rpcauth_destroy_credlist(&free);
264 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
267/*
268 * Look up a process' credentials in the authentication cache
269 */
270struct rpc_cred *
271rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred,
Trond Myklebust8a317762006-02-01 12:18:36 -0500272 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400274 LIST_HEAD(free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 struct rpc_cred_cache *cache = auth->au_credcache;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400276 struct hlist_node *pos;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400277 struct rpc_cred *cred = NULL,
278 *entry, *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 int nr = 0;
280
Trond Myklebust8a317762006-02-01 12:18:36 -0500281 if (!(flags & RPCAUTH_LOOKUP_ROOTCREDS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 nr = acred->uid & RPC_CREDCACHE_MASK;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400283
284 rcu_read_lock();
285 hlist_for_each_entry_rcu(entry, pos, &cache->hashtable[nr], cr_hash) {
286 if (!entry->cr_ops->crmatch(acred, entry, flags))
287 continue;
Trond Myklebust9499b432007-06-24 15:57:57 -0400288 spin_lock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400289 if (test_bit(RPCAUTH_CRED_HASHED, &entry->cr_flags) == 0) {
Trond Myklebust9499b432007-06-24 15:57:57 -0400290 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400291 continue;
292 }
293 cred = get_rpccred(entry);
Trond Myklebust9499b432007-06-24 15:57:57 -0400294 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400295 break;
296 }
297 rcu_read_unlock();
298
Trond Myklebust9499b432007-06-24 15:57:57 -0400299 if (cred != NULL)
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400300 goto found;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400301
302 new = auth->au_ops->crcreate(auth, acred, flags);
303 if (IS_ERR(new)) {
304 cred = new;
305 goto out;
306 }
307
Trond Myklebust9499b432007-06-24 15:57:57 -0400308 spin_lock(&cache->lock);
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400309 hlist_for_each_entry(entry, pos, &cache->hashtable[nr], cr_hash) {
310 if (!entry->cr_ops->crmatch(acred, entry, flags))
311 continue;
312 cred = get_rpccred(entry);
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400313 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400315 if (cred == NULL) {
Trond Myklebust5fe47552007-06-23 19:55:31 -0400316 cred = new;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400317 set_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
318 hlist_add_head_rcu(&cred->cr_hash, &cache->hashtable[nr]);
319 } else
320 list_add_tail(&new->cr_lru, &free);
Trond Myklebust9499b432007-06-24 15:57:57 -0400321 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400322found:
323 if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags)
Trond Myklebustfba3bad2006-02-01 12:19:27 -0500324 && cred->cr_ops->cr_init != NULL
325 && !(flags & RPCAUTH_LOOKUP_NEW)) {
326 int res = cred->cr_ops->cr_init(auth, cred);
327 if (res < 0) {
328 put_rpccred(cred);
329 cred = ERR_PTR(res);
330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400332 rpcauth_destroy_credlist(&free);
333out:
334 return cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
337struct rpc_cred *
Trond Myklebust8a317762006-02-01 12:18:36 -0500338rpcauth_lookupcred(struct rpc_auth *auth, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
340 struct auth_cred acred = {
341 .uid = current->fsuid,
342 .gid = current->fsgid,
343 .group_info = current->group_info,
344 };
345 struct rpc_cred *ret;
346
Chuck Lever46121cf2007-01-31 12:14:08 -0500347 dprintk("RPC: looking up %s cred\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 auth->au_ops->au_name);
349 get_group_info(acred.group_info);
Trond Myklebust8a317762006-02-01 12:18:36 -0500350 ret = auth->au_ops->lookup_cred(auth, &acred, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 put_group_info(acred.group_info);
352 return ret;
353}
354
Trond Myklebust5fe47552007-06-23 19:55:31 -0400355void
356rpcauth_init_cred(struct rpc_cred *cred, const struct auth_cred *acred,
357 struct rpc_auth *auth, const struct rpc_credops *ops)
358{
359 INIT_HLIST_NODE(&cred->cr_hash);
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400360 INIT_LIST_HEAD(&cred->cr_lru);
Trond Myklebust5fe47552007-06-23 19:55:31 -0400361 atomic_set(&cred->cr_count, 1);
362 cred->cr_auth = auth;
363 cred->cr_ops = ops;
364 cred->cr_expire = jiffies;
365#ifdef RPC_DEBUG
366 cred->cr_magic = RPCAUTH_CRED_MAGIC;
367#endif
368 cred->cr_uid = acred->uid;
369}
370EXPORT_SYMBOL(rpcauth_init_cred);
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372struct rpc_cred *
373rpcauth_bindcred(struct rpc_task *task)
374{
Trond Myklebust1be27f32007-06-27 14:29:04 -0400375 struct rpc_auth *auth = task->tk_client->cl_auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 struct auth_cred acred = {
377 .uid = current->fsuid,
378 .gid = current->fsgid,
379 .group_info = current->group_info,
380 };
381 struct rpc_cred *ret;
Trond Myklebust8a317762006-02-01 12:18:36 -0500382 int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Chuck Lever46121cf2007-01-31 12:14:08 -0500384 dprintk("RPC: %5u looking up %s cred\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400385 task->tk_pid, task->tk_client->cl_auth->au_ops->au_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 get_group_info(acred.group_info);
Trond Myklebust8a317762006-02-01 12:18:36 -0500387 if (task->tk_flags & RPC_TASK_ROOTCREDS)
388 flags |= RPCAUTH_LOOKUP_ROOTCREDS;
389 ret = auth->au_ops->lookup_cred(auth, &acred, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (!IS_ERR(ret))
391 task->tk_msg.rpc_cred = ret;
392 else
393 task->tk_status = PTR_ERR(ret);
394 put_group_info(acred.group_info);
395 return ret;
396}
397
398void
399rpcauth_holdcred(struct rpc_task *task)
400{
Trond Myklebust1be27f32007-06-27 14:29:04 -0400401 struct rpc_cred *cred = task->tk_msg.rpc_cred;
402 if (cred != NULL) {
403 get_rpccred(cred);
404 dprintk("RPC: %5u holding %s cred %p\n", task->tk_pid,
405 cred->cr_auth->au_ops->au_name, cred);
406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
409void
410put_rpccred(struct rpc_cred *cred)
411{
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400412 /* Fast path for unhashed credentials */
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400413 if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0)
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400414 goto need_lock;
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if (!atomic_dec_and_test(&cred->cr_count))
417 return;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400418 goto out_destroy;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400419need_lock:
420 if (!atomic_dec_and_lock(&cred->cr_count, &rpc_credcache_lock))
421 return;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400422 if (!list_empty(&cred->cr_lru)) {
423 number_cred_unused--;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400424 list_del_init(&cred->cr_lru);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400425 }
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400426 if (test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) == 0)
Trond Myklebust9499b432007-06-24 15:57:57 -0400427 rpcauth_unhash_cred(cred);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400428 else if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) {
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400429 cred->cr_expire = jiffies;
430 list_add_tail(&cred->cr_lru, &cred_unused);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400431 number_cred_unused++;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400432 spin_unlock(&rpc_credcache_lock);
433 return;
434 }
435 spin_unlock(&rpc_credcache_lock);
436out_destroy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 cred->cr_ops->crdestroy(cred);
438}
439
440void
441rpcauth_unbindcred(struct rpc_task *task)
442{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 struct rpc_cred *cred = task->tk_msg.rpc_cred;
444
Chuck Lever46121cf2007-01-31 12:14:08 -0500445 dprintk("RPC: %5u releasing %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400446 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 put_rpccred(cred);
449 task->tk_msg.rpc_cred = NULL;
450}
451
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700452__be32 *
453rpcauth_marshcred(struct rpc_task *task, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 struct rpc_cred *cred = task->tk_msg.rpc_cred;
456
Chuck Lever46121cf2007-01-31 12:14:08 -0500457 dprintk("RPC: %5u marshaling %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400458 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return cred->cr_ops->crmarshal(task, p);
461}
462
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700463__be32 *
464rpcauth_checkverf(struct rpc_task *task, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 struct rpc_cred *cred = task->tk_msg.rpc_cred;
467
Chuck Lever46121cf2007-01-31 12:14:08 -0500468 dprintk("RPC: %5u validating %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400469 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return cred->cr_ops->crvalidate(task, p);
472}
473
474int
475rpcauth_wrap_req(struct rpc_task *task, kxdrproc_t encode, void *rqstp,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700476 __be32 *data, void *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
478 struct rpc_cred *cred = task->tk_msg.rpc_cred;
J. Bruce Fieldsd8558f92007-07-10 15:19:26 -0400479 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Chuck Lever46121cf2007-01-31 12:14:08 -0500481 dprintk("RPC: %5u using %s cred %p to wrap rpc data\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 task->tk_pid, cred->cr_ops->cr_name, cred);
483 if (cred->cr_ops->crwrap_req)
484 return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj);
485 /* By default, we encode the arguments normally. */
J. Bruce Fieldsd8558f92007-07-10 15:19:26 -0400486 lock_kernel();
487 ret = encode(rqstp, data, obj);
488 unlock_kernel();
489 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
492int
493rpcauth_unwrap_resp(struct rpc_task *task, kxdrproc_t decode, void *rqstp,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700494 __be32 *data, void *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 struct rpc_cred *cred = task->tk_msg.rpc_cred;
J. Bruce Fieldsd8558f92007-07-10 15:19:26 -0400497 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Chuck Lever46121cf2007-01-31 12:14:08 -0500499 dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 task->tk_pid, cred->cr_ops->cr_name, cred);
501 if (cred->cr_ops->crunwrap_resp)
502 return cred->cr_ops->crunwrap_resp(task, decode, rqstp,
503 data, obj);
504 /* By default, we decode the arguments normally. */
J. Bruce Fieldsd8558f92007-07-10 15:19:26 -0400505 lock_kernel();
506 ret = decode(rqstp, data, obj);
507 unlock_kernel();
508 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
511int
512rpcauth_refreshcred(struct rpc_task *task)
513{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 struct rpc_cred *cred = task->tk_msg.rpc_cred;
515 int err;
516
Chuck Lever46121cf2007-01-31 12:14:08 -0500517 dprintk("RPC: %5u refreshing %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400518 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 err = cred->cr_ops->crrefresh(task);
521 if (err < 0)
522 task->tk_status = err;
523 return err;
524}
525
526void
527rpcauth_invalcred(struct rpc_task *task)
528{
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400529 struct rpc_cred *cred = task->tk_msg.rpc_cred;
530
Chuck Lever46121cf2007-01-31 12:14:08 -0500531 dprintk("RPC: %5u invalidating %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400532 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400533 if (cred)
534 clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
537int
538rpcauth_uptodatecred(struct rpc_task *task)
539{
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400540 struct rpc_cred *cred = task->tk_msg.rpc_cred;
541
542 return cred == NULL ||
543 test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
Trond Myklebustf5c21872007-06-25 17:11:20 -0400545
Rusty Russell8e1f9362007-07-17 04:03:17 -0700546static struct shrinker rpc_cred_shrinker = {
547 .shrink = rpcauth_cache_shrinker,
548 .seeks = DEFAULT_SEEKS,
549};
Trond Myklebustf5c21872007-06-25 17:11:20 -0400550
551void __init rpcauth_init_module(void)
552{
553 rpc_init_authunix();
Rusty Russell8e1f9362007-07-17 04:03:17 -0700554 register_shrinker(&rpc_cred_shrinker);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400555}
556
557void __exit rpcauth_remove_module(void)
558{
Rusty Russell8e1f9362007-07-17 04:03:17 -0700559 unregister_shrinker(&rpc_cred_shrinker);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400560}