Commit | Line | Data |
---|---|---|
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> | |
25337fdc | 14 | #include <linux/hash.h> |
1da177e4 LT |
15 | #include <linux/sunrpc/clnt.h> |
16 | #include <linux/spinlock.h> | |
17 | ||
18 | #ifdef RPC_DEBUG | |
19 | # define RPCDBG_FACILITY RPCDBG_AUTH | |
20 | #endif | |
21 | ||
241269bd TM |
22 | #define RPC_CREDCACHE_DEFAULT_HASHBITS (4) |
23 | struct rpc_cred_cache { | |
24 | struct hlist_head *hashtable; | |
25 | unsigned int hashbits; | |
26 | spinlock_t lock; | |
27 | }; | |
28 | ||
29 | static unsigned int auth_hashbits = RPC_CREDCACHE_DEFAULT_HASHBITS; | |
30 | ||
fc1b356f | 31 | static DEFINE_SPINLOCK(rpc_authflavor_lock); |
f1c0a861 | 32 | static const struct rpc_authops *auth_flavors[RPC_AUTH_MAXFLAVOR] = { |
1da177e4 LT |
33 | &authnull_ops, /* AUTH_NULL */ |
34 | &authunix_ops, /* AUTH_UNIX */ | |
35 | NULL, /* others can be loadable modules */ | |
36 | }; | |
37 | ||
e092bdcd | 38 | static LIST_HEAD(cred_unused); |
f5c2187c | 39 | static unsigned long number_cred_unused; |
e092bdcd | 40 | |
241269bd TM |
41 | #define MAX_HASHTABLE_BITS (10) |
42 | static int param_set_hashtbl_sz(const char *val, struct kernel_param *kp) | |
43 | { | |
44 | unsigned long num; | |
45 | unsigned int nbits; | |
46 | int ret; | |
47 | ||
48 | if (!val) | |
49 | goto out_inval; | |
50 | ret = strict_strtoul(val, 0, &num); | |
51 | if (ret == -EINVAL) | |
52 | goto out_inval; | |
53 | nbits = fls(num); | |
54 | if (num > (1U << nbits)) | |
55 | nbits++; | |
56 | if (nbits > MAX_HASHTABLE_BITS || nbits < 2) | |
57 | goto out_inval; | |
58 | *(unsigned int *)kp->arg = nbits; | |
59 | return 0; | |
60 | out_inval: | |
61 | return -EINVAL; | |
62 | } | |
63 | ||
64 | static int param_get_hashtbl_sz(char *buffer, struct kernel_param *kp) | |
65 | { | |
66 | unsigned int nbits; | |
67 | ||
68 | nbits = *(unsigned int *)kp->arg; | |
69 | return sprintf(buffer, "%u", 1U << nbits); | |
70 | } | |
71 | ||
72 | #define param_check_hashtbl_sz(name, p) __param_check(name, p, unsigned int); | |
73 | ||
74 | module_param_named(auth_hashtable_size, auth_hashbits, hashtbl_sz, 0644); | |
75 | MODULE_PARM_DESC(auth_hashtable_size, "RPC credential cache hashtable size"); | |
76 | ||
1da177e4 LT |
77 | static u32 |
78 | pseudoflavor_to_flavor(u32 flavor) { | |
79 | if (flavor >= RPC_AUTH_MAXFLAVOR) | |
80 | return RPC_AUTH_GSS; | |
81 | return flavor; | |
82 | } | |
83 | ||
84 | int | |
f1c0a861 | 85 | rpcauth_register(const struct rpc_authops *ops) |
1da177e4 LT |
86 | { |
87 | rpc_authflavor_t flavor; | |
fc1b356f | 88 | int ret = -EPERM; |
1da177e4 LT |
89 | |
90 | if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR) | |
91 | return -EINVAL; | |
fc1b356f TM |
92 | spin_lock(&rpc_authflavor_lock); |
93 | if (auth_flavors[flavor] == NULL) { | |
94 | auth_flavors[flavor] = ops; | |
95 | ret = 0; | |
96 | } | |
97 | spin_unlock(&rpc_authflavor_lock); | |
98 | return ret; | |
1da177e4 | 99 | } |
e8914c65 | 100 | EXPORT_SYMBOL_GPL(rpcauth_register); |
1da177e4 LT |
101 | |
102 | int | |
f1c0a861 | 103 | rpcauth_unregister(const struct rpc_authops *ops) |
1da177e4 LT |
104 | { |
105 | rpc_authflavor_t flavor; | |
fc1b356f | 106 | int ret = -EPERM; |
1da177e4 LT |
107 | |
108 | if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR) | |
109 | return -EINVAL; | |
fc1b356f TM |
110 | spin_lock(&rpc_authflavor_lock); |
111 | if (auth_flavors[flavor] == ops) { | |
112 | auth_flavors[flavor] = NULL; | |
113 | ret = 0; | |
114 | } | |
115 | spin_unlock(&rpc_authflavor_lock); | |
116 | return ret; | |
1da177e4 | 117 | } |
e8914c65 | 118 | EXPORT_SYMBOL_GPL(rpcauth_unregister); |
1da177e4 LT |
119 | |
120 | struct rpc_auth * | |
121 | rpcauth_create(rpc_authflavor_t pseudoflavor, struct rpc_clnt *clnt) | |
122 | { | |
123 | struct rpc_auth *auth; | |
f1c0a861 | 124 | const struct rpc_authops *ops; |
1da177e4 LT |
125 | u32 flavor = pseudoflavor_to_flavor(pseudoflavor); |
126 | ||
f344f6df OK |
127 | auth = ERR_PTR(-EINVAL); |
128 | if (flavor >= RPC_AUTH_MAXFLAVOR) | |
129 | goto out; | |
130 | ||
f344f6df OK |
131 | if ((ops = auth_flavors[flavor]) == NULL) |
132 | request_module("rpc-auth-%u", flavor); | |
fc1b356f TM |
133 | spin_lock(&rpc_authflavor_lock); |
134 | ops = auth_flavors[flavor]; | |
135 | if (ops == NULL || !try_module_get(ops->owner)) { | |
136 | spin_unlock(&rpc_authflavor_lock); | |
f344f6df | 137 | goto out; |
fc1b356f TM |
138 | } |
139 | spin_unlock(&rpc_authflavor_lock); | |
1da177e4 | 140 | auth = ops->create(clnt, pseudoflavor); |
fc1b356f | 141 | module_put(ops->owner); |
6a19275a BF |
142 | if (IS_ERR(auth)) |
143 | return auth; | |
1da177e4 | 144 | if (clnt->cl_auth) |
de7a8ce3 | 145 | rpcauth_release(clnt->cl_auth); |
1da177e4 | 146 | clnt->cl_auth = auth; |
f344f6df OK |
147 | |
148 | out: | |
1da177e4 LT |
149 | return auth; |
150 | } | |
e8914c65 | 151 | EXPORT_SYMBOL_GPL(rpcauth_create); |
1da177e4 LT |
152 | |
153 | void | |
de7a8ce3 | 154 | rpcauth_release(struct rpc_auth *auth) |
1da177e4 LT |
155 | { |
156 | if (!atomic_dec_and_test(&auth->au_count)) | |
157 | return; | |
158 | auth->au_ops->destroy(auth); | |
159 | } | |
160 | ||
161 | static DEFINE_SPINLOCK(rpc_credcache_lock); | |
162 | ||
31be5bf1 TM |
163 | static void |
164 | rpcauth_unhash_cred_locked(struct rpc_cred *cred) | |
165 | { | |
166 | hlist_del_rcu(&cred->cr_hash); | |
167 | smp_mb__before_clear_bit(); | |
168 | clear_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags); | |
169 | } | |
170 | ||
f0380f3d | 171 | static int |
9499b434 TM |
172 | rpcauth_unhash_cred(struct rpc_cred *cred) |
173 | { | |
174 | spinlock_t *cache_lock; | |
f0380f3d | 175 | int ret; |
9499b434 TM |
176 | |
177 | cache_lock = &cred->cr_auth->au_credcache->lock; | |
178 | spin_lock(cache_lock); | |
f0380f3d TM |
179 | ret = atomic_read(&cred->cr_count) == 0; |
180 | if (ret) | |
9499b434 TM |
181 | rpcauth_unhash_cred_locked(cred); |
182 | spin_unlock(cache_lock); | |
f0380f3d | 183 | return ret; |
9499b434 TM |
184 | } |
185 | ||
1da177e4 LT |
186 | /* |
187 | * Initialize RPC credential cache | |
188 | */ | |
189 | int | |
f5c2187c | 190 | rpcauth_init_credcache(struct rpc_auth *auth) |
1da177e4 LT |
191 | { |
192 | struct rpc_cred_cache *new; | |
988664a0 | 193 | unsigned int hashsize; |
1da177e4 | 194 | |
8b3a7005 | 195 | new = kmalloc(sizeof(*new), GFP_KERNEL); |
1da177e4 | 196 | if (!new) |
241269bd TM |
197 | goto out_nocache; |
198 | new->hashbits = auth_hashbits; | |
988664a0 | 199 | hashsize = 1U << new->hashbits; |
241269bd TM |
200 | new->hashtable = kcalloc(hashsize, sizeof(new->hashtable[0]), GFP_KERNEL); |
201 | if (!new->hashtable) | |
202 | goto out_nohashtbl; | |
9499b434 | 203 | spin_lock_init(&new->lock); |
1da177e4 LT |
204 | auth->au_credcache = new; |
205 | return 0; | |
241269bd TM |
206 | out_nohashtbl: |
207 | kfree(new); | |
208 | out_nocache: | |
209 | return -ENOMEM; | |
1da177e4 | 210 | } |
e8914c65 | 211 | EXPORT_SYMBOL_GPL(rpcauth_init_credcache); |
1da177e4 LT |
212 | |
213 | /* | |
214 | * Destroy a list of credentials | |
215 | */ | |
216 | static inline | |
e092bdcd | 217 | void rpcauth_destroy_credlist(struct list_head *head) |
1da177e4 LT |
218 | { |
219 | struct rpc_cred *cred; | |
220 | ||
e092bdcd TM |
221 | while (!list_empty(head)) { |
222 | cred = list_entry(head->next, struct rpc_cred, cr_lru); | |
223 | list_del_init(&cred->cr_lru); | |
1da177e4 LT |
224 | put_rpccred(cred); |
225 | } | |
226 | } | |
227 | ||
228 | /* | |
229 | * Clear the RPC credential cache, and delete those credentials | |
230 | * that are not referenced. | |
231 | */ | |
232 | void | |
3ab9bb72 | 233 | rpcauth_clear_credcache(struct rpc_cred_cache *cache) |
1da177e4 | 234 | { |
e092bdcd TM |
235 | LIST_HEAD(free); |
236 | struct hlist_head *head; | |
1da177e4 | 237 | struct rpc_cred *cred; |
988664a0 | 238 | unsigned int hashsize = 1U << cache->hashbits; |
1da177e4 LT |
239 | int i; |
240 | ||
241 | spin_lock(&rpc_credcache_lock); | |
9499b434 | 242 | spin_lock(&cache->lock); |
988664a0 | 243 | for (i = 0; i < hashsize; i++) { |
e092bdcd TM |
244 | head = &cache->hashtable[i]; |
245 | while (!hlist_empty(head)) { | |
246 | cred = hlist_entry(head->first, struct rpc_cred, cr_hash); | |
247 | get_rpccred(cred); | |
f5c2187c TM |
248 | if (!list_empty(&cred->cr_lru)) { |
249 | list_del(&cred->cr_lru); | |
250 | number_cred_unused--; | |
251 | } | |
252 | list_add_tail(&cred->cr_lru, &free); | |
31be5bf1 | 253 | rpcauth_unhash_cred_locked(cred); |
1da177e4 LT |
254 | } |
255 | } | |
9499b434 | 256 | spin_unlock(&cache->lock); |
1da177e4 LT |
257 | spin_unlock(&rpc_credcache_lock); |
258 | rpcauth_destroy_credlist(&free); | |
259 | } | |
260 | ||
3ab9bb72 TM |
261 | /* |
262 | * Destroy the RPC credential cache | |
263 | */ | |
264 | void | |
265 | rpcauth_destroy_credcache(struct rpc_auth *auth) | |
266 | { | |
267 | struct rpc_cred_cache *cache = auth->au_credcache; | |
268 | ||
269 | if (cache) { | |
270 | auth->au_credcache = NULL; | |
271 | rpcauth_clear_credcache(cache); | |
241269bd | 272 | kfree(cache->hashtable); |
3ab9bb72 TM |
273 | kfree(cache); |
274 | } | |
275 | } | |
e8914c65 | 276 | EXPORT_SYMBOL_GPL(rpcauth_destroy_credcache); |
3ab9bb72 | 277 | |
d2b83141 TM |
278 | |
279 | #define RPC_AUTH_EXPIRY_MORATORIUM (60 * HZ) | |
280 | ||
e092bdcd TM |
281 | /* |
282 | * Remove stale credentials. Avoid sleeping inside the loop. | |
283 | */ | |
f5c2187c TM |
284 | static int |
285 | rpcauth_prune_expired(struct list_head *free, int nr_to_scan) | |
1da177e4 | 286 | { |
9499b434 | 287 | spinlock_t *cache_lock; |
eac0d18d | 288 | struct rpc_cred *cred, *next; |
d2b83141 | 289 | unsigned long expired = jiffies - RPC_AUTH_EXPIRY_MORATORIUM; |
e092bdcd | 290 | |
eac0d18d TM |
291 | list_for_each_entry_safe(cred, next, &cred_unused, cr_lru) { |
292 | ||
20673406 TM |
293 | if (nr_to_scan-- == 0) |
294 | break; | |
93a05e65 TM |
295 | /* |
296 | * Enforce a 60 second garbage collection moratorium | |
297 | * Note that the cred_unused list must be time-ordered. | |
298 | */ | |
3d7b0894 | 299 | if (time_in_range(cred->cr_expire, expired, jiffies) && |
eac0d18d | 300 | test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) |
93a05e65 | 301 | return 0; |
eac0d18d | 302 | |
e092bdcd | 303 | list_del_init(&cred->cr_lru); |
f5c2187c | 304 | number_cred_unused--; |
e092bdcd TM |
305 | if (atomic_read(&cred->cr_count) != 0) |
306 | continue; | |
eac0d18d | 307 | |
9499b434 TM |
308 | cache_lock = &cred->cr_auth->au_credcache->lock; |
309 | spin_lock(cache_lock); | |
310 | if (atomic_read(&cred->cr_count) == 0) { | |
311 | get_rpccred(cred); | |
312 | list_add_tail(&cred->cr_lru, free); | |
313 | rpcauth_unhash_cred_locked(cred); | |
314 | } | |
315 | spin_unlock(cache_lock); | |
1da177e4 | 316 | } |
93a05e65 | 317 | return (number_cred_unused / 100) * sysctl_vfs_cache_pressure; |
1da177e4 LT |
318 | } |
319 | ||
320 | /* | |
f5c2187c | 321 | * Run memory cache shrinker. |
1da177e4 | 322 | */ |
f5c2187c TM |
323 | static int |
324 | rpcauth_cache_shrinker(int nr_to_scan, gfp_t gfp_mask) | |
1da177e4 | 325 | { |
f5c2187c TM |
326 | LIST_HEAD(free); |
327 | int res; | |
328 | ||
d300a41e TM |
329 | if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL) |
330 | return (nr_to_scan == 0) ? 0 : -1; | |
f5c2187c TM |
331 | if (list_empty(&cred_unused)) |
332 | return 0; | |
31be5bf1 | 333 | spin_lock(&rpc_credcache_lock); |
93a05e65 | 334 | res = rpcauth_prune_expired(&free, nr_to_scan); |
31be5bf1 | 335 | spin_unlock(&rpc_credcache_lock); |
f5c2187c TM |
336 | rpcauth_destroy_credlist(&free); |
337 | return res; | |
1da177e4 LT |
338 | } |
339 | ||
340 | /* | |
341 | * Look up a process' credentials in the authentication cache | |
342 | */ | |
343 | struct rpc_cred * | |
344 | rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred, | |
8a317760 | 345 | int flags) |
1da177e4 | 346 | { |
e092bdcd | 347 | LIST_HEAD(free); |
1da177e4 | 348 | struct rpc_cred_cache *cache = auth->au_credcache; |
e092bdcd | 349 | struct hlist_node *pos; |
31be5bf1 TM |
350 | struct rpc_cred *cred = NULL, |
351 | *entry, *new; | |
25337fdc TM |
352 | unsigned int nr; |
353 | ||
988664a0 | 354 | nr = hash_long(acred->uid, cache->hashbits); |
1da177e4 | 355 | |
31be5bf1 TM |
356 | rcu_read_lock(); |
357 | hlist_for_each_entry_rcu(entry, pos, &cache->hashtable[nr], cr_hash) { | |
e092bdcd TM |
358 | if (!entry->cr_ops->crmatch(acred, entry, flags)) |
359 | continue; | |
9499b434 | 360 | spin_lock(&cache->lock); |
31be5bf1 | 361 | if (test_bit(RPCAUTH_CRED_HASHED, &entry->cr_flags) == 0) { |
9499b434 | 362 | spin_unlock(&cache->lock); |
31be5bf1 TM |
363 | continue; |
364 | } | |
e092bdcd | 365 | cred = get_rpccred(entry); |
9499b434 | 366 | spin_unlock(&cache->lock); |
e092bdcd | 367 | break; |
1da177e4 | 368 | } |
31be5bf1 TM |
369 | rcu_read_unlock(); |
370 | ||
9499b434 | 371 | if (cred != NULL) |
31be5bf1 | 372 | goto found; |
1da177e4 | 373 | |
31be5bf1 TM |
374 | new = auth->au_ops->crcreate(auth, acred, flags); |
375 | if (IS_ERR(new)) { | |
376 | cred = new; | |
377 | goto out; | |
378 | } | |
1da177e4 | 379 | |
9499b434 | 380 | spin_lock(&cache->lock); |
31be5bf1 TM |
381 | hlist_for_each_entry(entry, pos, &cache->hashtable[nr], cr_hash) { |
382 | if (!entry->cr_ops->crmatch(acred, entry, flags)) | |
383 | continue; | |
384 | cred = get_rpccred(entry); | |
385 | break; | |
386 | } | |
387 | if (cred == NULL) { | |
5fe4755e | 388 | cred = new; |
31be5bf1 TM |
389 | set_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags); |
390 | hlist_add_head_rcu(&cred->cr_hash, &cache->hashtable[nr]); | |
391 | } else | |
392 | list_add_tail(&new->cr_lru, &free); | |
9499b434 | 393 | spin_unlock(&cache->lock); |
31be5bf1 | 394 | found: |
f64f9e71 JP |
395 | if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) && |
396 | cred->cr_ops->cr_init != NULL && | |
397 | !(flags & RPCAUTH_LOOKUP_NEW)) { | |
fba3bad4 TM |
398 | int res = cred->cr_ops->cr_init(auth, cred); |
399 | if (res < 0) { | |
400 | put_rpccred(cred); | |
401 | cred = ERR_PTR(res); | |
402 | } | |
1da177e4 | 403 | } |
31be5bf1 TM |
404 | rpcauth_destroy_credlist(&free); |
405 | out: | |
406 | return cred; | |
1da177e4 | 407 | } |
e8914c65 | 408 | EXPORT_SYMBOL_GPL(rpcauth_lookup_credcache); |
1da177e4 LT |
409 | |
410 | struct rpc_cred * | |
8a317760 | 411 | rpcauth_lookupcred(struct rpc_auth *auth, int flags) |
1da177e4 | 412 | { |
86a264ab | 413 | struct auth_cred acred; |
1da177e4 | 414 | struct rpc_cred *ret; |
86a264ab | 415 | const struct cred *cred = current_cred(); |
1da177e4 | 416 | |
46121cf7 | 417 | dprintk("RPC: looking up %s cred\n", |
1da177e4 | 418 | auth->au_ops->au_name); |
86a264ab DH |
419 | |
420 | memset(&acred, 0, sizeof(acred)); | |
421 | acred.uid = cred->fsuid; | |
422 | acred.gid = cred->fsgid; | |
423 | acred.group_info = get_group_info(((struct cred *)cred)->group_info); | |
424 | ||
8a317760 | 425 | ret = auth->au_ops->lookup_cred(auth, &acred, flags); |
1da177e4 LT |
426 | put_group_info(acred.group_info); |
427 | return ret; | |
428 | } | |
429 | ||
5fe4755e TM |
430 | void |
431 | rpcauth_init_cred(struct rpc_cred *cred, const struct auth_cred *acred, | |
432 | struct rpc_auth *auth, const struct rpc_credops *ops) | |
433 | { | |
434 | INIT_HLIST_NODE(&cred->cr_hash); | |
e092bdcd | 435 | INIT_LIST_HEAD(&cred->cr_lru); |
5fe4755e TM |
436 | atomic_set(&cred->cr_count, 1); |
437 | cred->cr_auth = auth; | |
438 | cred->cr_ops = ops; | |
439 | cred->cr_expire = jiffies; | |
440 | #ifdef RPC_DEBUG | |
441 | cred->cr_magic = RPCAUTH_CRED_MAGIC; | |
442 | #endif | |
443 | cred->cr_uid = acred->uid; | |
444 | } | |
e8914c65 | 445 | EXPORT_SYMBOL_GPL(rpcauth_init_cred); |
5fe4755e | 446 | |
5c691044 | 447 | void |
5d351754 | 448 | rpcauth_generic_bind_cred(struct rpc_task *task, struct rpc_cred *cred, int lookupflags) |
4ccda2cd TM |
449 | { |
450 | task->tk_msg.rpc_cred = get_rpccred(cred); | |
451 | dprintk("RPC: %5u holding %s cred %p\n", task->tk_pid, | |
452 | cred->cr_auth->au_ops->au_name, cred); | |
453 | } | |
5c691044 | 454 | EXPORT_SYMBOL_GPL(rpcauth_generic_bind_cred); |
4ccda2cd TM |
455 | |
456 | static void | |
5d351754 | 457 | rpcauth_bind_root_cred(struct rpc_task *task, int lookupflags) |
1da177e4 | 458 | { |
1be27f36 | 459 | struct rpc_auth *auth = task->tk_client->cl_auth; |
1da177e4 | 460 | struct auth_cred acred = { |
af093835 TM |
461 | .uid = 0, |
462 | .gid = 0, | |
1da177e4 LT |
463 | }; |
464 | struct rpc_cred *ret; | |
465 | ||
46121cf7 | 466 | dprintk("RPC: %5u looking up %s cred\n", |
1be27f36 | 467 | task->tk_pid, task->tk_client->cl_auth->au_ops->au_name); |
5d351754 | 468 | ret = auth->au_ops->lookup_cred(auth, &acred, lookupflags); |
af093835 TM |
469 | if (!IS_ERR(ret)) |
470 | task->tk_msg.rpc_cred = ret; | |
471 | else | |
472 | task->tk_status = PTR_ERR(ret); | |
473 | } | |
474 | ||
4ccda2cd | 475 | static void |
5d351754 | 476 | rpcauth_bind_new_cred(struct rpc_task *task, int lookupflags) |
af093835 TM |
477 | { |
478 | struct rpc_auth *auth = task->tk_client->cl_auth; | |
479 | struct rpc_cred *ret; | |
480 | ||
481 | dprintk("RPC: %5u looking up %s cred\n", | |
482 | task->tk_pid, auth->au_ops->au_name); | |
5d351754 | 483 | ret = rpcauth_lookupcred(auth, lookupflags); |
1da177e4 LT |
484 | if (!IS_ERR(ret)) |
485 | task->tk_msg.rpc_cred = ret; | |
486 | else | |
487 | task->tk_status = PTR_ERR(ret); | |
1da177e4 LT |
488 | } |
489 | ||
490 | void | |
4ccda2cd | 491 | rpcauth_bindcred(struct rpc_task *task, struct rpc_cred *cred, int flags) |
1da177e4 | 492 | { |
5d351754 TM |
493 | int lookupflags = 0; |
494 | ||
495 | if (flags & RPC_TASK_ASYNC) | |
496 | lookupflags |= RPCAUTH_LOOKUP_NEW; | |
4ccda2cd | 497 | if (cred != NULL) |
5d351754 | 498 | cred->cr_ops->crbind(task, cred, lookupflags); |
4ccda2cd | 499 | else if (flags & RPC_TASK_ROOTCREDS) |
5d351754 | 500 | rpcauth_bind_root_cred(task, lookupflags); |
4ccda2cd | 501 | else |
5d351754 | 502 | rpcauth_bind_new_cred(task, lookupflags); |
1da177e4 LT |
503 | } |
504 | ||
505 | void | |
506 | put_rpccred(struct rpc_cred *cred) | |
507 | { | |
e092bdcd | 508 | /* Fast path for unhashed credentials */ |
f0380f3d TM |
509 | if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) == 0) { |
510 | if (atomic_dec_and_test(&cred->cr_count)) | |
511 | cred->cr_ops->crdestroy(cred); | |
1da177e4 | 512 | return; |
f0380f3d TM |
513 | } |
514 | ||
e092bdcd TM |
515 | if (!atomic_dec_and_lock(&cred->cr_count, &rpc_credcache_lock)) |
516 | return; | |
f5c2187c TM |
517 | if (!list_empty(&cred->cr_lru)) { |
518 | number_cred_unused--; | |
e092bdcd | 519 | list_del_init(&cred->cr_lru); |
f5c2187c | 520 | } |
5f707eb4 | 521 | if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) { |
f0380f3d TM |
522 | if (test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0) { |
523 | cred->cr_expire = jiffies; | |
524 | list_add_tail(&cred->cr_lru, &cred_unused); | |
525 | number_cred_unused++; | |
526 | goto out_nodestroy; | |
527 | } | |
528 | if (!rpcauth_unhash_cred(cred)) { | |
529 | /* We were hashed and someone looked us up... */ | |
530 | goto out_nodestroy; | |
531 | } | |
e092bdcd TM |
532 | } |
533 | spin_unlock(&rpc_credcache_lock); | |
1da177e4 | 534 | cred->cr_ops->crdestroy(cred); |
f0380f3d TM |
535 | return; |
536 | out_nodestroy: | |
537 | spin_unlock(&rpc_credcache_lock); | |
1da177e4 | 538 | } |
e8914c65 | 539 | EXPORT_SYMBOL_GPL(put_rpccred); |
1da177e4 LT |
540 | |
541 | void | |
542 | rpcauth_unbindcred(struct rpc_task *task) | |
543 | { | |
1da177e4 LT |
544 | struct rpc_cred *cred = task->tk_msg.rpc_cred; |
545 | ||
46121cf7 | 546 | dprintk("RPC: %5u releasing %s cred %p\n", |
1be27f36 | 547 | task->tk_pid, cred->cr_auth->au_ops->au_name, cred); |
1da177e4 LT |
548 | |
549 | put_rpccred(cred); | |
550 | task->tk_msg.rpc_cred = NULL; | |
551 | } | |
552 | ||
d8ed029d AD |
553 | __be32 * |
554 | rpcauth_marshcred(struct rpc_task *task, __be32 *p) | |
1da177e4 | 555 | { |
1da177e4 LT |
556 | struct rpc_cred *cred = task->tk_msg.rpc_cred; |
557 | ||
46121cf7 | 558 | dprintk("RPC: %5u marshaling %s cred %p\n", |
1be27f36 | 559 | task->tk_pid, cred->cr_auth->au_ops->au_name, cred); |
0bbacc40 | 560 | |
1da177e4 LT |
561 | return cred->cr_ops->crmarshal(task, p); |
562 | } | |
563 | ||
d8ed029d AD |
564 | __be32 * |
565 | rpcauth_checkverf(struct rpc_task *task, __be32 *p) | |
1da177e4 | 566 | { |
1da177e4 LT |
567 | struct rpc_cred *cred = task->tk_msg.rpc_cred; |
568 | ||
46121cf7 | 569 | dprintk("RPC: %5u validating %s cred %p\n", |
1be27f36 | 570 | task->tk_pid, cred->cr_auth->au_ops->au_name, cred); |
0bbacc40 | 571 | |
1da177e4 LT |
572 | return cred->cr_ops->crvalidate(task, p); |
573 | } | |
574 | ||
575 | int | |
576 | rpcauth_wrap_req(struct rpc_task *task, kxdrproc_t encode, void *rqstp, | |
d8ed029d | 577 | __be32 *data, void *obj) |
1da177e4 LT |
578 | { |
579 | struct rpc_cred *cred = task->tk_msg.rpc_cred; | |
580 | ||
46121cf7 | 581 | dprintk("RPC: %5u using %s cred %p to wrap rpc data\n", |
1da177e4 LT |
582 | task->tk_pid, cred->cr_ops->cr_name, cred); |
583 | if (cred->cr_ops->crwrap_req) | |
584 | return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj); | |
585 | /* By default, we encode the arguments normally. */ | |
88a9fe8c | 586 | return encode(rqstp, data, obj); |
1da177e4 LT |
587 | } |
588 | ||
589 | int | |
590 | rpcauth_unwrap_resp(struct rpc_task *task, kxdrproc_t decode, void *rqstp, | |
d8ed029d | 591 | __be32 *data, void *obj) |
1da177e4 LT |
592 | { |
593 | struct rpc_cred *cred = task->tk_msg.rpc_cred; | |
594 | ||
46121cf7 | 595 | dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n", |
1da177e4 LT |
596 | task->tk_pid, cred->cr_ops->cr_name, cred); |
597 | if (cred->cr_ops->crunwrap_resp) | |
598 | return cred->cr_ops->crunwrap_resp(task, decode, rqstp, | |
599 | data, obj); | |
600 | /* By default, we decode the arguments normally. */ | |
88a9fe8c | 601 | return decode(rqstp, data, obj); |
1da177e4 LT |
602 | } |
603 | ||
604 | int | |
605 | rpcauth_refreshcred(struct rpc_task *task) | |
606 | { | |
1da177e4 LT |
607 | struct rpc_cred *cred = task->tk_msg.rpc_cred; |
608 | int err; | |
609 | ||
46121cf7 | 610 | dprintk("RPC: %5u refreshing %s cred %p\n", |
1be27f36 | 611 | task->tk_pid, cred->cr_auth->au_ops->au_name, cred); |
0bbacc40 | 612 | |
1da177e4 LT |
613 | err = cred->cr_ops->crrefresh(task); |
614 | if (err < 0) | |
615 | task->tk_status = err; | |
616 | return err; | |
617 | } | |
618 | ||
619 | void | |
620 | rpcauth_invalcred(struct rpc_task *task) | |
621 | { | |
fc432dd9 TM |
622 | struct rpc_cred *cred = task->tk_msg.rpc_cred; |
623 | ||
46121cf7 | 624 | dprintk("RPC: %5u invalidating %s cred %p\n", |
1be27f36 | 625 | task->tk_pid, cred->cr_auth->au_ops->au_name, cred); |
fc432dd9 TM |
626 | if (cred) |
627 | clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags); | |
1da177e4 LT |
628 | } |
629 | ||
630 | int | |
631 | rpcauth_uptodatecred(struct rpc_task *task) | |
632 | { | |
fc432dd9 TM |
633 | struct rpc_cred *cred = task->tk_msg.rpc_cred; |
634 | ||
635 | return cred == NULL || | |
636 | test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0; | |
1da177e4 | 637 | } |
f5c2187c | 638 | |
8e1f936b RR |
639 | static struct shrinker rpc_cred_shrinker = { |
640 | .shrink = rpcauth_cache_shrinker, | |
641 | .seeks = DEFAULT_SEEKS, | |
642 | }; | |
f5c2187c | 643 | |
5d8d9a4d | 644 | int __init rpcauth_init_module(void) |
f5c2187c | 645 | { |
5d8d9a4d TM |
646 | int err; |
647 | ||
648 | err = rpc_init_authunix(); | |
649 | if (err < 0) | |
650 | goto out1; | |
651 | err = rpc_init_generic_auth(); | |
652 | if (err < 0) | |
653 | goto out2; | |
8e1f936b | 654 | register_shrinker(&rpc_cred_shrinker); |
5d8d9a4d TM |
655 | return 0; |
656 | out2: | |
657 | rpc_destroy_authunix(); | |
658 | out1: | |
659 | return err; | |
f5c2187c TM |
660 | } |
661 | ||
662 | void __exit rpcauth_remove_module(void) | |
663 | { | |
5d8d9a4d TM |
664 | rpc_destroy_authunix(); |
665 | rpc_destroy_generic_auth(); | |
8e1f936b | 666 | unregister_shrinker(&rpc_cred_shrinker); |
f5c2187c | 667 | } |