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/vs_base.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 if (flavor >= RPC_AUTH_MAXFLAVOR || !(ops = auth_flavors[flavor]))
69 return ERR_PTR(-EINVAL);
70 auth = ops->create(clnt, pseudoflavor);
74 rpcauth_destroy(clnt->cl_auth);
80 rpcauth_destroy(struct rpc_auth *auth)
82 if (!atomic_dec_and_test(&auth->au_count))
84 auth->au_ops->destroy(auth);
87 static DEFINE_SPINLOCK(rpc_credcache_lock);
90 * Initialize RPC credential cache
93 rpcauth_init_credcache(struct rpc_auth *auth, unsigned long expire)
95 struct rpc_cred_cache *new;
98 new = kmalloc(sizeof(*new), GFP_KERNEL);
101 for (i = 0; i < RPC_CREDCACHE_NR; i++)
102 INIT_HLIST_HEAD(&new->hashtable[i]);
103 new->expire = expire;
104 new->nextgc = jiffies + (expire >> 1);
105 auth->au_credcache = new;
110 * Destroy a list of credentials
113 void rpcauth_destroy_credlist(struct hlist_head *head)
115 struct rpc_cred *cred;
117 while (!hlist_empty(head)) {
118 cred = hlist_entry(head->first, struct rpc_cred, cr_hash);
119 hlist_del_init(&cred->cr_hash);
125 * Clear the RPC credential cache, and delete those credentials
126 * that are not referenced.
129 rpcauth_free_credcache(struct rpc_auth *auth)
131 struct rpc_cred_cache *cache = auth->au_credcache;
133 struct hlist_node *pos, *next;
134 struct rpc_cred *cred;
137 spin_lock(&rpc_credcache_lock);
138 for (i = 0; i < RPC_CREDCACHE_NR; i++) {
139 hlist_for_each_safe(pos, next, &cache->hashtable[i]) {
140 cred = hlist_entry(pos, struct rpc_cred, cr_hash);
141 __hlist_del(&cred->cr_hash);
142 hlist_add_head(&cred->cr_hash, &free);
145 spin_unlock(&rpc_credcache_lock);
146 rpcauth_destroy_credlist(&free);
150 rpcauth_prune_expired(struct rpc_auth *auth, struct rpc_cred *cred, struct hlist_head *free)
152 if (atomic_read(&cred->cr_count) != 1)
154 if (time_after(jiffies, cred->cr_expire + auth->au_credcache->expire))
155 cred->cr_flags &= ~RPCAUTH_CRED_UPTODATE;
156 if (!(cred->cr_flags & RPCAUTH_CRED_UPTODATE)) {
157 __hlist_del(&cred->cr_hash);
158 hlist_add_head(&cred->cr_hash, free);
163 * Remove stale credentials. Avoid sleeping inside the loop.
166 rpcauth_gc_credcache(struct rpc_auth *auth, struct hlist_head *free)
168 struct rpc_cred_cache *cache = auth->au_credcache;
169 struct hlist_node *pos, *next;
170 struct rpc_cred *cred;
173 dprintk("RPC: gc'ing RPC credentials for auth %p\n", auth);
174 for (i = 0; i < RPC_CREDCACHE_NR; i++) {
175 hlist_for_each_safe(pos, next, &cache->hashtable[i]) {
176 cred = hlist_entry(pos, struct rpc_cred, cr_hash);
177 rpcauth_prune_expired(auth, cred, free);
180 cache->nextgc = jiffies + cache->expire;
184 * Look up a process' credentials in the authentication cache
187 rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred,
190 struct rpc_cred_cache *cache = auth->au_credcache;
192 struct hlist_node *pos, *next;
193 struct rpc_cred *new = NULL,
197 if (!(flags & RPCAUTH_LOOKUP_ROOTCREDS))
198 nr = acred->uid & RPC_CREDCACHE_MASK;
200 spin_lock(&rpc_credcache_lock);
201 if (time_before(cache->nextgc, jiffies))
202 rpcauth_gc_credcache(auth, &free);
203 hlist_for_each_safe(pos, next, &cache->hashtable[nr]) {
204 struct rpc_cred *entry;
205 entry = hlist_entry(pos, struct rpc_cred, cr_hash);
206 if (entry->cr_ops->crmatch(acred, entry, flags)) {
207 hlist_del(&entry->cr_hash);
211 rpcauth_prune_expired(auth, entry, &free);
215 hlist_add_head(&new->cr_hash, &free);
220 hlist_add_head(&cred->cr_hash, &cache->hashtable[nr]);
223 spin_unlock(&rpc_credcache_lock);
225 rpcauth_destroy_credlist(&free);
228 new = auth->au_ops->crcreate(auth, acred, flags);
231 new->cr_magic = RPCAUTH_CRED_MAGIC;
236 } else if ((cred->cr_flags & RPCAUTH_CRED_NEW)
237 && cred->cr_ops->cr_init != NULL
238 && !(flags & RPCAUTH_LOOKUP_NEW)) {
239 int res = cred->cr_ops->cr_init(auth, cred);
246 return (struct rpc_cred *) cred;
250 rpcauth_lookupcred(struct rpc_auth *auth, int flags)
252 struct auth_cred acred = {
253 .uid = current->fsuid,
254 .gid = current->fsgid,
255 .xid = vx_current_xid(),
256 .group_info = current->group_info,
258 struct rpc_cred *ret;
260 dprintk("RPC: looking up %s cred\n",
261 auth->au_ops->au_name);
262 get_group_info(acred.group_info);
263 ret = auth->au_ops->lookup_cred(auth, &acred, flags);
264 put_group_info(acred.group_info);
269 rpcauth_bindcred(struct rpc_task *task)
271 struct rpc_auth *auth = task->tk_auth;
272 struct auth_cred acred = {
273 .uid = current->fsuid,
274 .gid = current->fsgid,
275 .xid = vx_current_xid(),
276 .group_info = current->group_info,
278 struct rpc_cred *ret;
281 dprintk("RPC: %4d looking up %s cred\n",
282 task->tk_pid, task->tk_auth->au_ops->au_name);
283 get_group_info(acred.group_info);
284 if (task->tk_flags & RPC_TASK_ROOTCREDS)
285 flags |= RPCAUTH_LOOKUP_ROOTCREDS;
286 ret = auth->au_ops->lookup_cred(auth, &acred, flags);
288 task->tk_msg.rpc_cred = ret;
290 task->tk_status = PTR_ERR(ret);
291 put_group_info(acred.group_info);
296 rpcauth_holdcred(struct rpc_task *task)
298 dprintk("RPC: %4d holding %s cred %p\n",
299 task->tk_pid, task->tk_auth->au_ops->au_name, task->tk_msg.rpc_cred);
300 if (task->tk_msg.rpc_cred)
301 get_rpccred(task->tk_msg.rpc_cred);
305 put_rpccred(struct rpc_cred *cred)
307 cred->cr_expire = jiffies;
308 if (!atomic_dec_and_test(&cred->cr_count))
310 cred->cr_ops->crdestroy(cred);
314 rpcauth_unbindcred(struct rpc_task *task)
316 struct rpc_cred *cred = task->tk_msg.rpc_cred;
318 dprintk("RPC: %4d releasing %s cred %p\n",
319 task->tk_pid, task->tk_auth->au_ops->au_name, cred);
322 task->tk_msg.rpc_cred = NULL;
326 rpcauth_marshcred(struct rpc_task *task, u32 *p)
328 struct rpc_cred *cred = task->tk_msg.rpc_cred;
330 dprintk("RPC: %4d marshaling %s cred %p\n",
331 task->tk_pid, task->tk_auth->au_ops->au_name, cred);
333 return cred->cr_ops->crmarshal(task, p);
337 rpcauth_checkverf(struct rpc_task *task, u32 *p)
339 struct rpc_cred *cred = task->tk_msg.rpc_cred;
341 dprintk("RPC: %4d validating %s cred %p\n",
342 task->tk_pid, task->tk_auth->au_ops->au_name, cred);
344 return cred->cr_ops->crvalidate(task, p);
348 rpcauth_wrap_req(struct rpc_task *task, kxdrproc_t encode, void *rqstp,
349 u32 *data, void *obj)
351 struct rpc_cred *cred = task->tk_msg.rpc_cred;
353 dprintk("RPC: %4d using %s cred %p to wrap rpc data\n",
354 task->tk_pid, cred->cr_ops->cr_name, cred);
355 if (cred->cr_ops->crwrap_req)
356 return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj);
357 /* By default, we encode the arguments normally. */
358 return encode(rqstp, data, obj);
362 rpcauth_unwrap_resp(struct rpc_task *task, kxdrproc_t decode, void *rqstp,
363 u32 *data, void *obj)
365 struct rpc_cred *cred = task->tk_msg.rpc_cred;
367 dprintk("RPC: %4d using %s cred %p to unwrap rpc data\n",
368 task->tk_pid, cred->cr_ops->cr_name, cred);
369 if (cred->cr_ops->crunwrap_resp)
370 return cred->cr_ops->crunwrap_resp(task, decode, rqstp,
372 /* By default, we decode the arguments normally. */
373 return decode(rqstp, data, obj);
377 rpcauth_refreshcred(struct rpc_task *task)
379 struct rpc_cred *cred = task->tk_msg.rpc_cred;
382 dprintk("RPC: %4d refreshing %s cred %p\n",
383 task->tk_pid, task->tk_auth->au_ops->au_name, cred);
385 err = cred->cr_ops->crrefresh(task);
387 task->tk_status = err;
392 rpcauth_invalcred(struct rpc_task *task)
394 dprintk("RPC: %4d invalidating %s cred %p\n",
395 task->tk_pid, task->tk_auth->au_ops->au_name, task->tk_msg.rpc_cred);
396 spin_lock(&rpc_credcache_lock);
397 if (task->tk_msg.rpc_cred)
398 task->tk_msg.rpc_cred->cr_flags &= ~RPCAUTH_CRED_UPTODATE;
399 spin_unlock(&rpc_credcache_lock);
403 rpcauth_uptodatecred(struct rpc_task *task)
405 return !(task->tk_msg.rpc_cred) ||
406 (task->tk_msg.rpc_cred->cr_flags & RPCAUTH_CRED_UPTODATE);