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 */
29 pseudoflavor_to_flavor(u32 flavor) {
30 if (flavor >= RPC_AUTH_MAXFLAVOR)
36 rpcauth_register(const struct rpc_authops *ops)
38 rpc_authflavor_t flavor;
41 if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
43 spin_lock(&rpc_authflavor_lock);
44 if (auth_flavors[flavor] == NULL) {
45 auth_flavors[flavor] = ops;
48 spin_unlock(&rpc_authflavor_lock);
53 rpcauth_unregister(const struct rpc_authops *ops)
55 rpc_authflavor_t flavor;
58 if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
60 spin_lock(&rpc_authflavor_lock);
61 if (auth_flavors[flavor] == ops) {
62 auth_flavors[flavor] = NULL;
65 spin_unlock(&rpc_authflavor_lock);
70 rpcauth_create(rpc_authflavor_t pseudoflavor, struct rpc_clnt *clnt)
72 struct rpc_auth *auth;
73 const struct rpc_authops *ops;
74 u32 flavor = pseudoflavor_to_flavor(pseudoflavor);
76 auth = ERR_PTR(-EINVAL);
77 if (flavor >= RPC_AUTH_MAXFLAVOR)
81 if ((ops = auth_flavors[flavor]) == NULL)
82 request_module("rpc-auth-%u", flavor);
84 spin_lock(&rpc_authflavor_lock);
85 ops = auth_flavors[flavor];
86 if (ops == NULL || !try_module_get(ops->owner)) {
87 spin_unlock(&rpc_authflavor_lock);
90 spin_unlock(&rpc_authflavor_lock);
91 auth = ops->create(clnt, pseudoflavor);
92 module_put(ops->owner);
96 rpcauth_release(clnt->cl_auth);
104 rpcauth_release(struct rpc_auth *auth)
106 if (!atomic_dec_and_test(&auth->au_count))
108 auth->au_ops->destroy(auth);
111 static DEFINE_SPINLOCK(rpc_credcache_lock);
114 * Initialize RPC credential cache
117 rpcauth_init_credcache(struct rpc_auth *auth, unsigned long expire)
119 struct rpc_cred_cache *new;
122 new = kmalloc(sizeof(*new), GFP_KERNEL);
125 for (i = 0; i < RPC_CREDCACHE_NR; i++)
126 INIT_HLIST_HEAD(&new->hashtable[i]);
127 new->expire = expire;
128 new->nextgc = jiffies + (expire >> 1);
129 auth->au_credcache = new;
134 * Destroy a list of credentials
137 void rpcauth_destroy_credlist(struct hlist_head *head)
139 struct rpc_cred *cred;
141 while (!hlist_empty(head)) {
142 cred = hlist_entry(head->first, struct rpc_cred, cr_hash);
143 hlist_del_init(&cred->cr_hash);
149 * Clear the RPC credential cache, and delete those credentials
150 * that are not referenced.
153 rpcauth_clear_credcache(struct rpc_cred_cache *cache)
156 struct hlist_node *pos, *next;
157 struct rpc_cred *cred;
160 spin_lock(&rpc_credcache_lock);
161 for (i = 0; i < RPC_CREDCACHE_NR; i++) {
162 hlist_for_each_safe(pos, next, &cache->hashtable[i]) {
163 cred = hlist_entry(pos, struct rpc_cred, cr_hash);
164 __hlist_del(&cred->cr_hash);
165 hlist_add_head(&cred->cr_hash, &free);
168 spin_unlock(&rpc_credcache_lock);
169 rpcauth_destroy_credlist(&free);
173 * Destroy the RPC credential cache
176 rpcauth_destroy_credcache(struct rpc_auth *auth)
178 struct rpc_cred_cache *cache = auth->au_credcache;
181 auth->au_credcache = NULL;
182 rpcauth_clear_credcache(cache);
188 rpcauth_prune_expired(struct rpc_auth *auth, struct rpc_cred *cred, struct hlist_head *free)
190 if (atomic_read(&cred->cr_count) != 1)
192 if (time_after(jiffies, cred->cr_expire + auth->au_credcache->expire))
193 clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
194 if (test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) == 0) {
195 __hlist_del(&cred->cr_hash);
196 hlist_add_head(&cred->cr_hash, free);
201 * Remove stale credentials. Avoid sleeping inside the loop.
204 rpcauth_gc_credcache(struct rpc_auth *auth, struct hlist_head *free)
206 struct rpc_cred_cache *cache = auth->au_credcache;
207 struct hlist_node *pos, *next;
208 struct rpc_cred *cred;
211 dprintk("RPC: gc'ing RPC credentials for auth %p\n", auth);
212 for (i = 0; i < RPC_CREDCACHE_NR; i++) {
213 hlist_for_each_safe(pos, next, &cache->hashtable[i]) {
214 cred = hlist_entry(pos, struct rpc_cred, cr_hash);
215 rpcauth_prune_expired(auth, cred, free);
218 cache->nextgc = jiffies + cache->expire;
222 * Look up a process' credentials in the authentication cache
225 rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred,
228 struct rpc_cred_cache *cache = auth->au_credcache;
230 struct hlist_node *pos, *next;
231 struct rpc_cred *new = NULL,
235 if (!(flags & RPCAUTH_LOOKUP_ROOTCREDS))
236 nr = acred->uid & RPC_CREDCACHE_MASK;
238 spin_lock(&rpc_credcache_lock);
239 if (time_before(cache->nextgc, jiffies))
240 rpcauth_gc_credcache(auth, &free);
241 hlist_for_each_safe(pos, next, &cache->hashtable[nr]) {
242 struct rpc_cred *entry;
243 entry = hlist_entry(pos, struct rpc_cred, cr_hash);
244 if (entry->cr_ops->crmatch(acred, entry, flags)) {
245 hlist_del(&entry->cr_hash);
249 rpcauth_prune_expired(auth, entry, &free);
253 hlist_add_head(&new->cr_hash, &free);
258 hlist_add_head(&cred->cr_hash, &cache->hashtable[nr]);
261 spin_unlock(&rpc_credcache_lock);
263 rpcauth_destroy_credlist(&free);
266 new = auth->au_ops->crcreate(auth, acred, flags);
270 } else if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags)
271 && cred->cr_ops->cr_init != NULL
272 && !(flags & RPCAUTH_LOOKUP_NEW)) {
273 int res = cred->cr_ops->cr_init(auth, cred);
280 return (struct rpc_cred *) cred;
284 rpcauth_lookupcred(struct rpc_auth *auth, int flags)
286 struct auth_cred acred = {
287 .uid = current->fsuid,
288 .gid = current->fsgid,
289 .group_info = current->group_info,
291 struct rpc_cred *ret;
293 dprintk("RPC: looking up %s cred\n",
294 auth->au_ops->au_name);
295 get_group_info(acred.group_info);
296 ret = auth->au_ops->lookup_cred(auth, &acred, flags);
297 put_group_info(acred.group_info);
302 rpcauth_init_cred(struct rpc_cred *cred, const struct auth_cred *acred,
303 struct rpc_auth *auth, const struct rpc_credops *ops)
305 INIT_HLIST_NODE(&cred->cr_hash);
306 atomic_set(&cred->cr_count, 1);
307 cred->cr_auth = auth;
309 cred->cr_expire = jiffies;
311 cred->cr_magic = RPCAUTH_CRED_MAGIC;
313 cred->cr_uid = acred->uid;
315 EXPORT_SYMBOL(rpcauth_init_cred);
318 rpcauth_bindcred(struct rpc_task *task)
320 struct rpc_auth *auth = task->tk_auth;
321 struct auth_cred acred = {
322 .uid = current->fsuid,
323 .gid = current->fsgid,
324 .group_info = current->group_info,
326 struct rpc_cred *ret;
329 dprintk("RPC: %5u looking up %s cred\n",
330 task->tk_pid, task->tk_auth->au_ops->au_name);
331 get_group_info(acred.group_info);
332 if (task->tk_flags & RPC_TASK_ROOTCREDS)
333 flags |= RPCAUTH_LOOKUP_ROOTCREDS;
334 ret = auth->au_ops->lookup_cred(auth, &acred, flags);
336 task->tk_msg.rpc_cred = ret;
338 task->tk_status = PTR_ERR(ret);
339 put_group_info(acred.group_info);
344 rpcauth_holdcred(struct rpc_task *task)
346 dprintk("RPC: %5u holding %s cred %p\n",
347 task->tk_pid, task->tk_auth->au_ops->au_name,
348 task->tk_msg.rpc_cred);
349 if (task->tk_msg.rpc_cred)
350 get_rpccred(task->tk_msg.rpc_cred);
354 put_rpccred(struct rpc_cred *cred)
356 cred->cr_expire = jiffies;
357 if (!atomic_dec_and_test(&cred->cr_count))
359 cred->cr_ops->crdestroy(cred);
363 rpcauth_unbindcred(struct rpc_task *task)
365 struct rpc_cred *cred = task->tk_msg.rpc_cred;
367 dprintk("RPC: %5u releasing %s cred %p\n",
368 task->tk_pid, task->tk_auth->au_ops->au_name, cred);
371 task->tk_msg.rpc_cred = NULL;
375 rpcauth_marshcred(struct rpc_task *task, __be32 *p)
377 struct rpc_cred *cred = task->tk_msg.rpc_cred;
379 dprintk("RPC: %5u marshaling %s cred %p\n",
380 task->tk_pid, task->tk_auth->au_ops->au_name, cred);
382 return cred->cr_ops->crmarshal(task, p);
386 rpcauth_checkverf(struct rpc_task *task, __be32 *p)
388 struct rpc_cred *cred = task->tk_msg.rpc_cred;
390 dprintk("RPC: %5u validating %s cred %p\n",
391 task->tk_pid, task->tk_auth->au_ops->au_name, cred);
393 return cred->cr_ops->crvalidate(task, p);
397 rpcauth_wrap_req(struct rpc_task *task, kxdrproc_t encode, void *rqstp,
398 __be32 *data, void *obj)
400 struct rpc_cred *cred = task->tk_msg.rpc_cred;
402 dprintk("RPC: %5u using %s cred %p to wrap rpc data\n",
403 task->tk_pid, cred->cr_ops->cr_name, cred);
404 if (cred->cr_ops->crwrap_req)
405 return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj);
406 /* By default, we encode the arguments normally. */
407 return encode(rqstp, data, obj);
411 rpcauth_unwrap_resp(struct rpc_task *task, kxdrproc_t decode, void *rqstp,
412 __be32 *data, void *obj)
414 struct rpc_cred *cred = task->tk_msg.rpc_cred;
416 dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n",
417 task->tk_pid, cred->cr_ops->cr_name, cred);
418 if (cred->cr_ops->crunwrap_resp)
419 return cred->cr_ops->crunwrap_resp(task, decode, rqstp,
421 /* By default, we decode the arguments normally. */
422 return decode(rqstp, data, obj);
426 rpcauth_refreshcred(struct rpc_task *task)
428 struct rpc_cred *cred = task->tk_msg.rpc_cred;
431 dprintk("RPC: %5u refreshing %s cred %p\n",
432 task->tk_pid, task->tk_auth->au_ops->au_name, cred);
434 err = cred->cr_ops->crrefresh(task);
436 task->tk_status = err;
441 rpcauth_invalcred(struct rpc_task *task)
443 struct rpc_cred *cred = task->tk_msg.rpc_cred;
445 dprintk("RPC: %5u invalidating %s cred %p\n",
446 task->tk_pid, task->tk_auth->au_ops->au_name, cred);
448 clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
452 rpcauth_uptodatecred(struct rpc_task *task)
454 struct rpc_cred *cred = task->tk_msg.rpc_cred;
456 return cred == NULL ||
457 test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0;