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>
16 #include <linux/vserver/xid.h>
19 # define RPCDBG_FACILITY RPCDBG_AUTH
22 static 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(struct rpc_authops *ops)
38 rpc_authflavor_t flavor;
40 if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
42 if (auth_flavors[flavor] != NULL)
43 return -EPERM; /* what else? */
44 auth_flavors[flavor] = ops;
49 rpcauth_unregister(struct rpc_authops *ops)
51 rpc_authflavor_t flavor;
53 if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
55 if (auth_flavors[flavor] != ops)
56 return -EPERM; /* what else? */
57 auth_flavors[flavor] = NULL;
62 rpcauth_create(rpc_authflavor_t pseudoflavor, struct rpc_clnt *clnt)
64 struct rpc_auth *auth;
65 struct rpc_authops *ops;
66 u32 flavor = pseudoflavor_to_flavor(pseudoflavor);
68 auth = ERR_PTR(-EINVAL);
69 if (flavor >= RPC_AUTH_MAXFLAVOR)
72 /* FIXME - auth_flavors[] really needs an rw lock,
73 * and module refcounting. */
75 if ((ops = auth_flavors[flavor]) == NULL)
76 request_module("rpc-auth-%u", flavor);
78 if ((ops = auth_flavors[flavor]) == NULL)
80 auth = ops->create(clnt, pseudoflavor);
84 rpcauth_destroy(clnt->cl_auth);
92 rpcauth_destroy(struct rpc_auth *auth)
94 if (!atomic_dec_and_test(&auth->au_count))
96 auth->au_ops->destroy(auth);
99 static DEFINE_SPINLOCK(rpc_credcache_lock);
102 * Initialize RPC credential cache
105 rpcauth_init_credcache(struct rpc_auth *auth, unsigned long expire)
107 struct rpc_cred_cache *new;
110 new = kmalloc(sizeof(*new), GFP_KERNEL);
113 for (i = 0; i < RPC_CREDCACHE_NR; i++)
114 INIT_HLIST_HEAD(&new->hashtable[i]);
115 new->expire = expire;
116 new->nextgc = jiffies + (expire >> 1);
117 auth->au_credcache = new;
122 * Destroy a list of credentials
125 void rpcauth_destroy_credlist(struct hlist_head *head)
127 struct rpc_cred *cred;
129 while (!hlist_empty(head)) {
130 cred = hlist_entry(head->first, struct rpc_cred, cr_hash);
131 hlist_del_init(&cred->cr_hash);
137 * Clear the RPC credential cache, and delete those credentials
138 * that are not referenced.
141 rpcauth_free_credcache(struct rpc_auth *auth)
143 struct rpc_cred_cache *cache = auth->au_credcache;
145 struct hlist_node *pos, *next;
146 struct rpc_cred *cred;
149 spin_lock(&rpc_credcache_lock);
150 for (i = 0; i < RPC_CREDCACHE_NR; i++) {
151 hlist_for_each_safe(pos, next, &cache->hashtable[i]) {
152 cred = hlist_entry(pos, struct rpc_cred, cr_hash);
153 __hlist_del(&cred->cr_hash);
154 hlist_add_head(&cred->cr_hash, &free);
157 spin_unlock(&rpc_credcache_lock);
158 rpcauth_destroy_credlist(&free);
162 rpcauth_prune_expired(struct rpc_auth *auth, struct rpc_cred *cred, struct hlist_head *free)
164 if (atomic_read(&cred->cr_count) != 1)
166 if (time_after(jiffies, cred->cr_expire + auth->au_credcache->expire))
167 cred->cr_flags &= ~RPCAUTH_CRED_UPTODATE;
168 if (!(cred->cr_flags & RPCAUTH_CRED_UPTODATE)) {
169 __hlist_del(&cred->cr_hash);
170 hlist_add_head(&cred->cr_hash, free);
175 * Remove stale credentials. Avoid sleeping inside the loop.
178 rpcauth_gc_credcache(struct rpc_auth *auth, struct hlist_head *free)
180 struct rpc_cred_cache *cache = auth->au_credcache;
181 struct hlist_node *pos, *next;
182 struct rpc_cred *cred;
185 dprintk("RPC: gc'ing RPC credentials for auth %p\n", auth);
186 for (i = 0; i < RPC_CREDCACHE_NR; i++) {
187 hlist_for_each_safe(pos, next, &cache->hashtable[i]) {
188 cred = hlist_entry(pos, struct rpc_cred, cr_hash);
189 rpcauth_prune_expired(auth, cred, free);
192 cache->nextgc = jiffies + cache->expire;
196 * Look up a process' credentials in the authentication cache
199 rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred,
202 struct rpc_cred_cache *cache = auth->au_credcache;
204 struct hlist_node *pos, *next;
205 struct rpc_cred *new = NULL,
209 if (!(flags & RPCAUTH_LOOKUP_ROOTCREDS))
210 nr = acred->uid & RPC_CREDCACHE_MASK;
212 spin_lock(&rpc_credcache_lock);
213 if (time_before(cache->nextgc, jiffies))
214 rpcauth_gc_credcache(auth, &free);
215 hlist_for_each_safe(pos, next, &cache->hashtable[nr]) {
216 struct rpc_cred *entry;
217 entry = hlist_entry(pos, struct rpc_cred, cr_hash);
218 if (entry->cr_ops->crmatch(acred, entry, flags)) {
219 hlist_del(&entry->cr_hash);
223 rpcauth_prune_expired(auth, entry, &free);
227 hlist_add_head(&new->cr_hash, &free);
232 hlist_add_head(&cred->cr_hash, &cache->hashtable[nr]);
235 spin_unlock(&rpc_credcache_lock);
237 rpcauth_destroy_credlist(&free);
240 new = auth->au_ops->crcreate(auth, acred, flags);
243 new->cr_magic = RPCAUTH_CRED_MAGIC;
248 } else if ((cred->cr_flags & RPCAUTH_CRED_NEW)
249 && cred->cr_ops->cr_init != NULL
250 && !(flags & RPCAUTH_LOOKUP_NEW)) {
251 int res = cred->cr_ops->cr_init(auth, cred);
258 return (struct rpc_cred *) cred;
262 rpcauth_lookupcred(struct rpc_auth *auth, int flags)
264 struct auth_cred acred = {
265 .uid = current->fsuid,
266 .gid = current->fsgid,
267 .xid = vx_current_xid(),
268 .group_info = current->group_info,
270 struct rpc_cred *ret;
272 dprintk("RPC: looking up %s cred\n",
273 auth->au_ops->au_name);
274 get_group_info(acred.group_info);
275 ret = auth->au_ops->lookup_cred(auth, &acred, flags);
276 put_group_info(acred.group_info);
281 rpcauth_bindcred(struct rpc_task *task)
283 struct rpc_auth *auth = task->tk_auth;
284 struct auth_cred acred = {
285 .uid = current->fsuid,
286 .gid = current->fsgid,
287 .xid = vx_current_xid(),
288 .group_info = current->group_info,
290 struct rpc_cred *ret;
293 dprintk("RPC: %4d looking up %s cred\n",
294 task->tk_pid, task->tk_auth->au_ops->au_name);
295 get_group_info(acred.group_info);
296 if (task->tk_flags & RPC_TASK_ROOTCREDS)
297 flags |= RPCAUTH_LOOKUP_ROOTCREDS;
298 ret = auth->au_ops->lookup_cred(auth, &acred, flags);
300 task->tk_msg.rpc_cred = ret;
302 task->tk_status = PTR_ERR(ret);
303 put_group_info(acred.group_info);
308 rpcauth_holdcred(struct rpc_task *task)
310 dprintk("RPC: %4d holding %s cred %p\n",
311 task->tk_pid, task->tk_auth->au_ops->au_name, task->tk_msg.rpc_cred);
312 if (task->tk_msg.rpc_cred)
313 get_rpccred(task->tk_msg.rpc_cred);
317 put_rpccred(struct rpc_cred *cred)
319 cred->cr_expire = jiffies;
320 if (!atomic_dec_and_test(&cred->cr_count))
322 cred->cr_ops->crdestroy(cred);
326 rpcauth_unbindcred(struct rpc_task *task)
328 struct rpc_cred *cred = task->tk_msg.rpc_cred;
330 dprintk("RPC: %4d releasing %s cred %p\n",
331 task->tk_pid, task->tk_auth->au_ops->au_name, cred);
334 task->tk_msg.rpc_cred = NULL;
338 rpcauth_marshcred(struct rpc_task *task, u32 *p)
340 struct rpc_cred *cred = task->tk_msg.rpc_cred;
342 dprintk("RPC: %4d marshaling %s cred %p\n",
343 task->tk_pid, task->tk_auth->au_ops->au_name, cred);
345 return cred->cr_ops->crmarshal(task, p);
349 rpcauth_checkverf(struct rpc_task *task, u32 *p)
351 struct rpc_cred *cred = task->tk_msg.rpc_cred;
353 dprintk("RPC: %4d validating %s cred %p\n",
354 task->tk_pid, task->tk_auth->au_ops->au_name, cred);
356 return cred->cr_ops->crvalidate(task, p);
360 rpcauth_wrap_req(struct rpc_task *task, kxdrproc_t encode, void *rqstp,
361 u32 *data, void *obj)
363 struct rpc_cred *cred = task->tk_msg.rpc_cred;
365 dprintk("RPC: %4d using %s cred %p to wrap rpc data\n",
366 task->tk_pid, cred->cr_ops->cr_name, cred);
367 if (cred->cr_ops->crwrap_req)
368 return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj);
369 /* By default, we encode the arguments normally. */
370 return encode(rqstp, data, obj);
374 rpcauth_unwrap_resp(struct rpc_task *task, kxdrproc_t decode, void *rqstp,
375 u32 *data, void *obj)
377 struct rpc_cred *cred = task->tk_msg.rpc_cred;
379 dprintk("RPC: %4d using %s cred %p to unwrap rpc data\n",
380 task->tk_pid, cred->cr_ops->cr_name, cred);
381 if (cred->cr_ops->crunwrap_resp)
382 return cred->cr_ops->crunwrap_resp(task, decode, rqstp,
384 /* By default, we decode the arguments normally. */
385 return decode(rqstp, data, obj);
389 rpcauth_refreshcred(struct rpc_task *task)
391 struct rpc_cred *cred = task->tk_msg.rpc_cred;
394 dprintk("RPC: %4d refreshing %s cred %p\n",
395 task->tk_pid, task->tk_auth->au_ops->au_name, cred);
397 err = cred->cr_ops->crrefresh(task);
399 task->tk_status = err;
404 rpcauth_invalcred(struct rpc_task *task)
406 dprintk("RPC: %4d invalidating %s cred %p\n",
407 task->tk_pid, task->tk_auth->au_ops->au_name, task->tk_msg.rpc_cred);
408 spin_lock(&rpc_credcache_lock);
409 if (task->tk_msg.rpc_cred)
410 task->tk_msg.rpc_cred->cr_flags &= ~RPCAUTH_CRED_UPTODATE;
411 spin_unlock(&rpc_credcache_lock);
415 rpcauth_uptodatecred(struct rpc_task *task)
417 return !(task->tk_msg.rpc_cred) ||
418 (task->tk_msg.rpc_cred->cr_flags & RPCAUTH_CRED_UPTODATE);