Pull now into release branch
[deliverable/linux.git] / net / sunrpc / auth.c
CommitLineData
1da177e4
LT
1/*
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>
1da177e4
LT
14#include <linux/sunrpc/clnt.h>
15#include <linux/spinlock.h>
16
17#ifdef RPC_DEBUG
18# define RPCDBG_FACILITY RPCDBG_AUTH
19#endif
20
fc1b356f 21static DEFINE_SPINLOCK(rpc_authflavor_lock);
f1c0a861 22static const struct rpc_authops *auth_flavors[RPC_AUTH_MAXFLAVOR] = {
1da177e4
LT
23 &authnull_ops, /* AUTH_NULL */
24 &authunix_ops, /* AUTH_UNIX */
25 NULL, /* others can be loadable modules */
26};
27
e092bdcd 28static LIST_HEAD(cred_unused);
f5c2187c 29static unsigned long number_cred_unused;
e092bdcd 30
1da177e4
LT
31static u32
32pseudoflavor_to_flavor(u32 flavor) {
33 if (flavor >= RPC_AUTH_MAXFLAVOR)
34 return RPC_AUTH_GSS;
35 return flavor;
36}
37
38int
f1c0a861 39rpcauth_register(const struct rpc_authops *ops)
1da177e4
LT
40{
41 rpc_authflavor_t flavor;
fc1b356f 42 int ret = -EPERM;
1da177e4
LT
43
44 if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
45 return -EINVAL;
fc1b356f
TM
46 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;
1da177e4
LT
53}
54
55int
f1c0a861 56rpcauth_unregister(const struct rpc_authops *ops)
1da177e4
LT
57{
58 rpc_authflavor_t flavor;
fc1b356f 59 int ret = -EPERM;
1da177e4
LT
60
61 if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
62 return -EINVAL;
fc1b356f
TM
63 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;
1da177e4
LT
70}
71
72struct rpc_auth *
73rpcauth_create(rpc_authflavor_t pseudoflavor, struct rpc_clnt *clnt)
74{
75 struct rpc_auth *auth;
f1c0a861 76 const struct rpc_authops *ops;
1da177e4
LT
77 u32 flavor = pseudoflavor_to_flavor(pseudoflavor);
78
f344f6df
OK
79 auth = ERR_PTR(-EINVAL);
80 if (flavor >= RPC_AUTH_MAXFLAVOR)
81 goto out;
82
f344f6df
OK
83#ifdef CONFIG_KMOD
84 if ((ops = auth_flavors[flavor]) == NULL)
85 request_module("rpc-auth-%u", flavor);
86#endif
fc1b356f
TM
87 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);
f344f6df 91 goto out;
fc1b356f
TM
92 }
93 spin_unlock(&rpc_authflavor_lock);
1da177e4 94 auth = ops->create(clnt, pseudoflavor);
fc1b356f 95 module_put(ops->owner);
6a19275a
BF
96 if (IS_ERR(auth))
97 return auth;
1da177e4 98 if (clnt->cl_auth)
de7a8ce3 99 rpcauth_release(clnt->cl_auth);
1da177e4 100 clnt->cl_auth = auth;
f344f6df
OK
101
102out:
1da177e4
LT
103 return auth;
104}
105
106void
de7a8ce3 107rpcauth_release(struct rpc_auth *auth)
1da177e4
LT
108{
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
31be5bf1
TM
116static 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
9499b434
TM
124static 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
1da177e4
LT
136/*
137 * Initialize RPC credential cache
138 */
139int
f5c2187c 140rpcauth_init_credcache(struct rpc_auth *auth)
1da177e4
LT
141{
142 struct rpc_cred_cache *new;
143 int i;
144
8b3a7005 145 new = kmalloc(sizeof(*new), GFP_KERNEL);
1da177e4
LT
146 if (!new)
147 return -ENOMEM;
148 for (i = 0; i < RPC_CREDCACHE_NR; i++)
149 INIT_HLIST_HEAD(&new->hashtable[i]);
9499b434 150 spin_lock_init(&new->lock);
1da177e4
LT
151 auth->au_credcache = new;
152 return 0;
153}
154
155/*
156 * Destroy a list of credentials
157 */
158static inline
e092bdcd 159void rpcauth_destroy_credlist(struct list_head *head)
1da177e4
LT
160{
161 struct rpc_cred *cred;
162
e092bdcd
TM
163 while (!list_empty(head)) {
164 cred = list_entry(head->next, struct rpc_cred, cr_lru);
165 list_del_init(&cred->cr_lru);
1da177e4
LT
166 put_rpccred(cred);
167 }
168}
169
170/*
171 * Clear the RPC credential cache, and delete those credentials
172 * that are not referenced.
173 */
174void
3ab9bb72 175rpcauth_clear_credcache(struct rpc_cred_cache *cache)
1da177e4 176{
e092bdcd
TM
177 LIST_HEAD(free);
178 struct hlist_head *head;
1da177e4
LT
179 struct rpc_cred *cred;
180 int i;
181
182 spin_lock(&rpc_credcache_lock);
9499b434 183 spin_lock(&cache->lock);
1da177e4 184 for (i = 0; i < RPC_CREDCACHE_NR; i++) {
e092bdcd
TM
185 head = &cache->hashtable[i];
186 while (!hlist_empty(head)) {
187 cred = hlist_entry(head->first, struct rpc_cred, cr_hash);
188 get_rpccred(cred);
f5c2187c
TM
189 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);
31be5bf1 194 rpcauth_unhash_cred_locked(cred);
1da177e4
LT
195 }
196 }
9499b434 197 spin_unlock(&cache->lock);
1da177e4
LT
198 spin_unlock(&rpc_credcache_lock);
199 rpcauth_destroy_credlist(&free);
200}
201
3ab9bb72
TM
202/*
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
e092bdcd
TM
217/*
218 * Remove stale credentials. Avoid sleeping inside the loop.
219 */
f5c2187c
TM
220static int
221rpcauth_prune_expired(struct list_head *free, int nr_to_scan)
1da177e4 222{
9499b434 223 spinlock_t *cache_lock;
e092bdcd
TM
224 struct rpc_cred *cred;
225
226 while (!list_empty(&cred_unused)) {
227 cred = list_entry(cred_unused.next, struct rpc_cred, cr_lru);
e092bdcd 228 list_del_init(&cred->cr_lru);
f5c2187c 229 number_cred_unused--;
e092bdcd
TM
230 if (atomic_read(&cred->cr_count) != 0)
231 continue;
9499b434
TM
232 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);
f5c2187c 238 nr_to_scan--;
9499b434
TM
239 }
240 spin_unlock(cache_lock);
f5c2187c
TM
241 if (nr_to_scan == 0)
242 break;
1da177e4 243 }
f5c2187c 244 return nr_to_scan;
1da177e4
LT
245}
246
247/*
f5c2187c 248 * Run memory cache shrinker.
1da177e4 249 */
f5c2187c
TM
250static int
251rpcauth_cache_shrinker(int nr_to_scan, gfp_t gfp_mask)
1da177e4 252{
f5c2187c
TM
253 LIST_HEAD(free);
254 int res;
255
256 if (list_empty(&cred_unused))
257 return 0;
31be5bf1 258 spin_lock(&rpc_credcache_lock);
f5c2187c
TM
259 nr_to_scan = rpcauth_prune_expired(&free, nr_to_scan);
260 res = (number_cred_unused / 100) * sysctl_vfs_cache_pressure;
31be5bf1 261 spin_unlock(&rpc_credcache_lock);
f5c2187c
TM
262 rpcauth_destroy_credlist(&free);
263 return res;
1da177e4
LT
264}
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,
8a317760 271 int flags)
1da177e4 272{
e092bdcd 273 LIST_HEAD(free);
1da177e4 274 struct rpc_cred_cache *cache = auth->au_credcache;
e092bdcd 275 struct hlist_node *pos;
31be5bf1
TM
276 struct rpc_cred *cred = NULL,
277 *entry, *new;
1da177e4
LT
278 int nr = 0;
279
8a317760 280 if (!(flags & RPCAUTH_LOOKUP_ROOTCREDS))
1da177e4 281 nr = acred->uid & RPC_CREDCACHE_MASK;
31be5bf1
TM
282
283 rcu_read_lock();
284 hlist_for_each_entry_rcu(entry, pos, &cache->hashtable[nr], cr_hash) {
e092bdcd
TM
285 if (!entry->cr_ops->crmatch(acred, entry, flags))
286 continue;
9499b434 287 spin_lock(&cache->lock);
31be5bf1 288 if (test_bit(RPCAUTH_CRED_HASHED, &entry->cr_flags) == 0) {
9499b434 289 spin_unlock(&cache->lock);
31be5bf1
TM
290 continue;
291 }
e092bdcd 292 cred = get_rpccred(entry);
9499b434 293 spin_unlock(&cache->lock);
e092bdcd 294 break;
1da177e4 295 }
31be5bf1
TM
296 rcu_read_unlock();
297
9499b434 298 if (cred != NULL)
31be5bf1 299 goto found;
1da177e4 300
31be5bf1
TM
301 new = auth->au_ops->crcreate(auth, acred, flags);
302 if (IS_ERR(new)) {
303 cred = new;
304 goto out;
305 }
1da177e4 306
9499b434 307 spin_lock(&cache->lock);
31be5bf1
TM
308 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);
312 break;
313 }
314 if (cred == NULL) {
5fe4755e 315 cred = new;
31be5bf1
TM
316 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);
9499b434 320 spin_unlock(&cache->lock);
31be5bf1
TM
321found:
322 if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags)
fba3bad4
TM
323 && 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 }
1da177e4 330 }
31be5bf1
TM
331 rpcauth_destroy_credlist(&free);
332out:
333 return cred;
1da177e4
LT
334}
335
336struct rpc_cred *
8a317760 337rpcauth_lookupcred(struct rpc_auth *auth, int flags)
1da177e4
LT
338{
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
46121cf7 346 dprintk("RPC: looking up %s cred\n",
1da177e4
LT
347 auth->au_ops->au_name);
348 get_group_info(acred.group_info);
8a317760 349 ret = auth->au_ops->lookup_cred(auth, &acred, flags);
1da177e4
LT
350 put_group_info(acred.group_info);
351 return ret;
352}
353
5fe4755e
TM
354void
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);
e092bdcd 359 INIT_LIST_HEAD(&cred->cr_lru);
5fe4755e
TM
360 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
1da177e4
LT
371struct rpc_cred *
372rpcauth_bindcred(struct rpc_task *task)
373{
1be27f36 374 struct rpc_auth *auth = task->tk_client->cl_auth;
1da177e4
LT
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;
8a317760 381 int flags = 0;
1da177e4 382
46121cf7 383 dprintk("RPC: %5u looking up %s cred\n",
1be27f36 384 task->tk_pid, task->tk_client->cl_auth->au_ops->au_name);
1da177e4 385 get_group_info(acred.group_info);
8a317760
TM
386 if (task->tk_flags & RPC_TASK_ROOTCREDS)
387 flags |= RPCAUTH_LOOKUP_ROOTCREDS;
388 ret = auth->au_ops->lookup_cred(auth, &acred, flags);
1da177e4
LT
389 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{
1be27f36
TM
400 struct rpc_cred *cred = task->tk_msg.rpc_cred;
401 if (cred != NULL) {
402 get_rpccred(cred);
403 dprintk("RPC: %5u holding %s cred %p\n", task->tk_pid,
404 cred->cr_auth->au_ops->au_name, cred);
405 }
1da177e4
LT
406}
407
408void
409put_rpccred(struct rpc_cred *cred)
410{
e092bdcd 411 /* Fast path for unhashed credentials */
31be5bf1 412 if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0)
e092bdcd
TM
413 goto need_lock;
414
1da177e4
LT
415 if (!atomic_dec_and_test(&cred->cr_count))
416 return;
e092bdcd 417 goto out_destroy;
e092bdcd
TM
418need_lock:
419 if (!atomic_dec_and_lock(&cred->cr_count, &rpc_credcache_lock))
420 return;
f5c2187c
TM
421 if (!list_empty(&cred->cr_lru)) {
422 number_cred_unused--;
e092bdcd 423 list_del_init(&cred->cr_lru);
f5c2187c 424 }
e092bdcd 425 if (test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) == 0)
9499b434 426 rpcauth_unhash_cred(cred);
31be5bf1 427 else if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) {
e092bdcd
TM
428 cred->cr_expire = jiffies;
429 list_add_tail(&cred->cr_lru, &cred_unused);
f5c2187c 430 number_cred_unused++;
e092bdcd
TM
431 spin_unlock(&rpc_credcache_lock);
432 return;
433 }
434 spin_unlock(&rpc_credcache_lock);
435out_destroy:
1da177e4
LT
436 cred->cr_ops->crdestroy(cred);
437}
438
439void
440rpcauth_unbindcred(struct rpc_task *task)
441{
1da177e4
LT
442 struct rpc_cred *cred = task->tk_msg.rpc_cred;
443
46121cf7 444 dprintk("RPC: %5u releasing %s cred %p\n",
1be27f36 445 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
1da177e4
LT
446
447 put_rpccred(cred);
448 task->tk_msg.rpc_cred = NULL;
449}
450
d8ed029d
AD
451__be32 *
452rpcauth_marshcred(struct rpc_task *task, __be32 *p)
1da177e4 453{
1da177e4
LT
454 struct rpc_cred *cred = task->tk_msg.rpc_cred;
455
46121cf7 456 dprintk("RPC: %5u marshaling %s cred %p\n",
1be27f36 457 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
0bbacc40 458
1da177e4
LT
459 return cred->cr_ops->crmarshal(task, p);
460}
461
d8ed029d
AD
462__be32 *
463rpcauth_checkverf(struct rpc_task *task, __be32 *p)
1da177e4 464{
1da177e4
LT
465 struct rpc_cred *cred = task->tk_msg.rpc_cred;
466
46121cf7 467 dprintk("RPC: %5u validating %s cred %p\n",
1be27f36 468 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
0bbacc40 469
1da177e4
LT
470 return cred->cr_ops->crvalidate(task, p);
471}
472
473int
474rpcauth_wrap_req(struct rpc_task *task, kxdrproc_t encode, void *rqstp,
d8ed029d 475 __be32 *data, void *obj)
1da177e4
LT
476{
477 struct rpc_cred *cred = task->tk_msg.rpc_cred;
478
46121cf7 479 dprintk("RPC: %5u using %s cred %p to wrap rpc data\n",
1da177e4
LT
480 task->tk_pid, cred->cr_ops->cr_name, cred);
481 if (cred->cr_ops->crwrap_req)
482 return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj);
483 /* By default, we encode the arguments normally. */
be879c4e 484 return rpc_call_xdrproc(encode, rqstp, data, obj);
1da177e4
LT
485}
486
487int
488rpcauth_unwrap_resp(struct rpc_task *task, kxdrproc_t decode, void *rqstp,
d8ed029d 489 __be32 *data, void *obj)
1da177e4
LT
490{
491 struct rpc_cred *cred = task->tk_msg.rpc_cred;
492
46121cf7 493 dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n",
1da177e4
LT
494 task->tk_pid, cred->cr_ops->cr_name, cred);
495 if (cred->cr_ops->crunwrap_resp)
496 return cred->cr_ops->crunwrap_resp(task, decode, rqstp,
497 data, obj);
498 /* By default, we decode the arguments normally. */
be879c4e 499 return rpc_call_xdrproc(decode, rqstp, data, obj);
1da177e4
LT
500}
501
502int
503rpcauth_refreshcred(struct rpc_task *task)
504{
1da177e4
LT
505 struct rpc_cred *cred = task->tk_msg.rpc_cred;
506 int err;
507
46121cf7 508 dprintk("RPC: %5u refreshing %s cred %p\n",
1be27f36 509 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
0bbacc40 510
1da177e4
LT
511 err = cred->cr_ops->crrefresh(task);
512 if (err < 0)
513 task->tk_status = err;
514 return err;
515}
516
517void
518rpcauth_invalcred(struct rpc_task *task)
519{
fc432dd9
TM
520 struct rpc_cred *cred = task->tk_msg.rpc_cred;
521
46121cf7 522 dprintk("RPC: %5u invalidating %s cred %p\n",
1be27f36 523 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
fc432dd9
TM
524 if (cred)
525 clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
1da177e4
LT
526}
527
528int
529rpcauth_uptodatecred(struct rpc_task *task)
530{
fc432dd9
TM
531 struct rpc_cred *cred = task->tk_msg.rpc_cred;
532
533 return cred == NULL ||
534 test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0;
1da177e4 535}
f5c2187c 536
8e1f936b
RR
537static struct shrinker rpc_cred_shrinker = {
538 .shrink = rpcauth_cache_shrinker,
539 .seeks = DEFAULT_SEEKS,
540};
f5c2187c
TM
541
542void __init rpcauth_init_module(void)
543{
544 rpc_init_authunix();
8e1f936b 545 register_shrinker(&rpc_cred_shrinker);
f5c2187c
TM
546}
547
548void __exit rpcauth_remove_module(void)
549{
8e1f936b 550 unregister_shrinker(&rpc_cred_shrinker);
f5c2187c 551}
This page took 0.409286 seconds and 5 git commands to generate.