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/socket.h>
15 #include <linux/sunrpc/clnt.h>
16 #include <linux/spinlock.h>
17 #include <linux/vserver/xid.h>
20 # define RPCDBG_FACILITY RPCDBG_AUTH
23 static struct rpc_authops * auth_flavors[RPC_AUTH_MAXFLAVOR] = {
24 &authnull_ops, /* AUTH_NULL */
25 &authunix_ops, /* AUTH_UNIX */
26 NULL, /* others can be loadable modules */
30 pseudoflavor_to_flavor(u32 flavor) {
31 if (flavor >= RPC_AUTH_MAXFLAVOR)
37 rpcauth_register(struct rpc_authops *ops)
39 rpc_authflavor_t flavor;
41 if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
43 if (auth_flavors[flavor] != NULL)
44 return -EPERM; /* what else? */
45 auth_flavors[flavor] = ops;
50 rpcauth_unregister(struct rpc_authops *ops)
52 rpc_authflavor_t flavor;
54 if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
56 if (auth_flavors[flavor] != ops)
57 return -EPERM; /* what else? */
58 auth_flavors[flavor] = NULL;
63 rpcauth_create(rpc_authflavor_t pseudoflavor, struct rpc_clnt *clnt)
65 struct rpc_auth *auth;
66 struct rpc_authops *ops;
67 u32 flavor = pseudoflavor_to_flavor(pseudoflavor);
69 if (flavor >= RPC_AUTH_MAXFLAVOR || !(ops = auth_flavors[flavor]))
71 auth = ops->create(clnt, pseudoflavor);
75 rpcauth_destroy(clnt->cl_auth);
81 rpcauth_destroy(struct rpc_auth *auth)
83 if (!atomic_dec_and_test(&auth->au_count))
85 auth->au_ops->destroy(auth);
88 static DEFINE_SPINLOCK(rpc_credcache_lock);
91 * Initialize RPC credential cache
94 rpcauth_init_credcache(struct rpc_auth *auth, unsigned long expire)
96 struct rpc_cred_cache *new;
99 new = (struct rpc_cred_cache *)kmalloc(sizeof(*new), GFP_KERNEL);
102 for (i = 0; i < RPC_CREDCACHE_NR; i++)
103 INIT_HLIST_HEAD(&new->hashtable[i]);
104 new->expire = expire;
105 new->nextgc = jiffies + (expire >> 1);
106 auth->au_credcache = new;
111 * Destroy a list of credentials
114 void rpcauth_destroy_credlist(struct hlist_head *head)
116 struct rpc_cred *cred;
118 while (!hlist_empty(head)) {
119 cred = hlist_entry(head->first, struct rpc_cred, cr_hash);
120 hlist_del_init(&cred->cr_hash);
126 * Clear the RPC credential cache, and delete those credentials
127 * that are not referenced.
130 rpcauth_free_credcache(struct rpc_auth *auth)
132 struct rpc_cred_cache *cache = auth->au_credcache;
134 struct hlist_node *pos, *next;
135 struct rpc_cred *cred;
138 spin_lock(&rpc_credcache_lock);
139 for (i = 0; i < RPC_CREDCACHE_NR; i++) {
140 hlist_for_each_safe(pos, next, &cache->hashtable[i]) {
141 cred = hlist_entry(pos, struct rpc_cred, cr_hash);
142 __hlist_del(&cred->cr_hash);
143 hlist_add_head(&cred->cr_hash, &free);
146 spin_unlock(&rpc_credcache_lock);
147 rpcauth_destroy_credlist(&free);
151 rpcauth_prune_expired(struct rpc_auth *auth, struct rpc_cred *cred, struct hlist_head *free)
153 if (atomic_read(&cred->cr_count) != 1)
155 if (time_after(jiffies, cred->cr_expire + auth->au_credcache->expire))
156 cred->cr_flags &= ~RPCAUTH_CRED_UPTODATE;
157 if (!(cred->cr_flags & RPCAUTH_CRED_UPTODATE)) {
158 __hlist_del(&cred->cr_hash);
159 hlist_add_head(&cred->cr_hash, free);
164 * Remove stale credentials. Avoid sleeping inside the loop.
167 rpcauth_gc_credcache(struct rpc_auth *auth, struct hlist_head *free)
169 struct rpc_cred_cache *cache = auth->au_credcache;
170 struct hlist_node *pos, *next;
171 struct rpc_cred *cred;
174 dprintk("RPC: gc'ing RPC credentials for auth %p\n", auth);
175 for (i = 0; i < RPC_CREDCACHE_NR; i++) {
176 hlist_for_each_safe(pos, next, &cache->hashtable[i]) {
177 cred = hlist_entry(pos, struct rpc_cred, cr_hash);
178 rpcauth_prune_expired(auth, cred, free);
181 cache->nextgc = jiffies + cache->expire;
185 * Look up a process' credentials in the authentication cache
188 rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred,
191 struct rpc_cred_cache *cache = auth->au_credcache;
193 struct hlist_node *pos, *next;
194 struct rpc_cred *new = NULL,
198 if (!(taskflags & RPC_TASK_ROOTCREDS))
199 nr = acred->uid & RPC_CREDCACHE_MASK;
201 spin_lock(&rpc_credcache_lock);
202 if (time_before(cache->nextgc, jiffies))
203 rpcauth_gc_credcache(auth, &free);
204 hlist_for_each_safe(pos, next, &cache->hashtable[nr]) {
205 struct rpc_cred *entry;
206 entry = hlist_entry(pos, struct rpc_cred, cr_hash);
207 if (entry->cr_ops->crmatch(acred, entry, taskflags)) {
208 hlist_del(&entry->cr_hash);
212 rpcauth_prune_expired(auth, entry, &free);
216 hlist_add_head(&new->cr_hash, &free);
221 hlist_add_head(&cred->cr_hash, &cache->hashtable[nr]);
224 spin_unlock(&rpc_credcache_lock);
226 rpcauth_destroy_credlist(&free);
229 new = auth->au_ops->crcreate(auth, acred, taskflags);
232 new->cr_magic = RPCAUTH_CRED_MAGIC;
239 return (struct rpc_cred *) cred;
243 rpcauth_lookupcred(struct rpc_auth *auth, int taskflags)
245 struct auth_cred acred = {
246 .uid = current->fsuid,
247 .gid = current->fsgid,
248 .xid = vx_current_xid(),
249 .group_info = current->group_info,
251 struct rpc_cred *ret;
253 dprintk("RPC: looking up %s cred\n",
254 auth->au_ops->au_name);
255 get_group_info(acred.group_info);
256 ret = auth->au_ops->lookup_cred(auth, &acred, taskflags);
257 put_group_info(acred.group_info);
262 rpcauth_bindcred(struct rpc_task *task)
264 struct rpc_auth *auth = task->tk_auth;
265 struct auth_cred acred = {
266 .uid = current->fsuid,
267 .gid = current->fsgid,
268 .xid = vx_current_xid(),
269 .group_info = current->group_info,
271 struct rpc_cred *ret;
273 dprintk("RPC: %4d looking up %s cred\n",
274 task->tk_pid, task->tk_auth->au_ops->au_name);
275 get_group_info(acred.group_info);
276 ret = auth->au_ops->lookup_cred(auth, &acred, task->tk_flags);
278 task->tk_msg.rpc_cred = ret;
280 task->tk_status = PTR_ERR(ret);
281 put_group_info(acred.group_info);
286 rpcauth_holdcred(struct rpc_task *task)
288 dprintk("RPC: %4d holding %s cred %p\n",
289 task->tk_pid, task->tk_auth->au_ops->au_name, task->tk_msg.rpc_cred);
290 if (task->tk_msg.rpc_cred)
291 get_rpccred(task->tk_msg.rpc_cred);
295 put_rpccred(struct rpc_cred *cred)
297 cred->cr_expire = jiffies;
298 if (!atomic_dec_and_test(&cred->cr_count))
300 cred->cr_ops->crdestroy(cred);
304 rpcauth_unbindcred(struct rpc_task *task)
306 struct rpc_auth *auth = task->tk_auth;
307 struct rpc_cred *cred = task->tk_msg.rpc_cred;
309 dprintk("RPC: %4d releasing %s cred %p\n",
310 task->tk_pid, auth->au_ops->au_name, cred);
313 task->tk_msg.rpc_cred = NULL;
317 rpcauth_marshcred(struct rpc_task *task, u32 *p)
319 struct rpc_auth *auth = task->tk_auth;
320 struct rpc_cred *cred = task->tk_msg.rpc_cred;
322 dprintk("RPC: %4d marshaling %s cred %p\n",
323 task->tk_pid, auth->au_ops->au_name, cred);
324 return cred->cr_ops->crmarshal(task, p);
328 rpcauth_checkverf(struct rpc_task *task, u32 *p)
330 struct rpc_auth *auth = task->tk_auth;
331 struct rpc_cred *cred = task->tk_msg.rpc_cred;
333 dprintk("RPC: %4d validating %s cred %p\n",
334 task->tk_pid, auth->au_ops->au_name, cred);
335 return cred->cr_ops->crvalidate(task, p);
339 rpcauth_wrap_req(struct rpc_task *task, kxdrproc_t encode, void *rqstp,
340 u32 *data, void *obj)
342 struct rpc_cred *cred = task->tk_msg.rpc_cred;
344 dprintk("RPC: %4d using %s cred %p to wrap rpc data\n",
345 task->tk_pid, cred->cr_ops->cr_name, cred);
346 if (cred->cr_ops->crwrap_req)
347 return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj);
348 /* By default, we encode the arguments normally. */
349 return encode(rqstp, data, obj);
353 rpcauth_unwrap_resp(struct rpc_task *task, kxdrproc_t decode, void *rqstp,
354 u32 *data, void *obj)
356 struct rpc_cred *cred = task->tk_msg.rpc_cred;
358 dprintk("RPC: %4d using %s cred %p to unwrap rpc data\n",
359 task->tk_pid, cred->cr_ops->cr_name, cred);
360 if (cred->cr_ops->crunwrap_resp)
361 return cred->cr_ops->crunwrap_resp(task, decode, rqstp,
363 /* By default, we decode the arguments normally. */
364 return decode(rqstp, data, obj);
368 rpcauth_refreshcred(struct rpc_task *task)
370 struct rpc_auth *auth = task->tk_auth;
371 struct rpc_cred *cred = task->tk_msg.rpc_cred;
374 dprintk("RPC: %4d refreshing %s cred %p\n",
375 task->tk_pid, auth->au_ops->au_name, cred);
376 err = cred->cr_ops->crrefresh(task);
378 task->tk_status = err;
383 rpcauth_invalcred(struct rpc_task *task)
385 dprintk("RPC: %4d invalidating %s cred %p\n",
386 task->tk_pid, task->tk_auth->au_ops->au_name, task->tk_msg.rpc_cred);
387 spin_lock(&rpc_credcache_lock);
388 if (task->tk_msg.rpc_cred)
389 task->tk_msg.rpc_cred->cr_flags &= ~RPCAUTH_CRED_UPTODATE;
390 spin_unlock(&rpc_credcache_lock);
394 rpcauth_uptodatecred(struct rpc_task *task)
396 return !(task->tk_msg.rpc_cred) ||
397 (task->tk_msg.rpc_cred->cr_flags & RPCAUTH_CRED_UPTODATE);