[PATCH] VM: add page_state info to per-node meminfo
[linux-ginger.git] / net / sunrpc / auth.c
blob505e2d4b3d6259e3363c8a1c428da3535533132a
1 /*
2 * linux/net/sunrpc/auth.c
4 * Generic RPC client authentication API.
6 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
7 */
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>
18 #ifdef RPC_DEBUG
19 # define RPCDBG_FACILITY RPCDBG_AUTH
20 #endif
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 */
28 static u32
29 pseudoflavor_to_flavor(u32 flavor) {
30 if (flavor >= RPC_AUTH_MAXFLAVOR)
31 return RPC_AUTH_GSS;
32 return flavor;
35 int
36 rpcauth_register(struct rpc_authops *ops)
38 rpc_authflavor_t flavor;
40 if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
41 return -EINVAL;
42 if (auth_flavors[flavor] != NULL)
43 return -EPERM; /* what else? */
44 auth_flavors[flavor] = ops;
45 return 0;
48 int
49 rpcauth_unregister(struct rpc_authops *ops)
51 rpc_authflavor_t flavor;
53 if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
54 return -EINVAL;
55 if (auth_flavors[flavor] != ops)
56 return -EPERM; /* what else? */
57 auth_flavors[flavor] = NULL;
58 return 0;
61 struct rpc_auth *
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);
71 if (IS_ERR(auth))
72 return auth;
73 if (clnt->cl_auth)
74 rpcauth_destroy(clnt->cl_auth);
75 clnt->cl_auth = auth;
76 return auth;
79 void
80 rpcauth_destroy(struct rpc_auth *auth)
82 if (!atomic_dec_and_test(&auth->au_count))
83 return;
84 auth->au_ops->destroy(auth);
87 static DEFINE_SPINLOCK(rpc_credcache_lock);
90 * Initialize RPC credential cache
92 int
93 rpcauth_init_credcache(struct rpc_auth *auth, unsigned long expire)
95 struct rpc_cred_cache *new;
96 int i;
98 new = (struct rpc_cred_cache *)kmalloc(sizeof(*new), GFP_KERNEL);
99 if (!new)
100 return -ENOMEM;
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;
106 return 0;
110 * Destroy a list of credentials
112 static inline
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);
120 put_rpccred(cred);
125 * Clear the RPC credential cache, and delete those credentials
126 * that are not referenced.
128 void
129 rpcauth_free_credcache(struct rpc_auth *auth)
131 struct rpc_cred_cache *cache = auth->au_credcache;
132 HLIST_HEAD(free);
133 struct hlist_node *pos, *next;
134 struct rpc_cred *cred;
135 int i;
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);
149 static void
150 rpcauth_prune_expired(struct rpc_auth *auth, struct rpc_cred *cred, struct hlist_head *free)
152 if (atomic_read(&cred->cr_count) != 1)
153 return;
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.
165 static void
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;
171 int i;
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
186 struct rpc_cred *
187 rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred,
188 int taskflags)
190 struct rpc_cred_cache *cache = auth->au_credcache;
191 HLIST_HEAD(free);
192 struct hlist_node *pos, *next;
193 struct rpc_cred *new = NULL,
194 *cred = NULL;
195 int nr = 0;
197 if (!(taskflags & RPC_TASK_ROOTCREDS))
198 nr = acred->uid & RPC_CREDCACHE_MASK;
199 retry:
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, taskflags)) {
207 hlist_del(&entry->cr_hash);
208 cred = entry;
209 break;
211 rpcauth_prune_expired(auth, entry, &free);
213 if (new) {
214 if (cred)
215 hlist_add_head(&new->cr_hash, &free);
216 else
217 cred = new;
219 if (cred) {
220 hlist_add_head(&cred->cr_hash, &cache->hashtable[nr]);
221 get_rpccred(cred);
223 spin_unlock(&rpc_credcache_lock);
225 rpcauth_destroy_credlist(&free);
227 if (!cred) {
228 new = auth->au_ops->crcreate(auth, acred, taskflags);
229 if (!IS_ERR(new)) {
230 #ifdef RPC_DEBUG
231 new->cr_magic = RPCAUTH_CRED_MAGIC;
232 #endif
233 goto retry;
234 } else
235 cred = new;
238 return (struct rpc_cred *) cred;
241 struct rpc_cred *
242 rpcauth_lookupcred(struct rpc_auth *auth, int taskflags)
244 struct auth_cred acred = {
245 .uid = current->fsuid,
246 .gid = current->fsgid,
247 .group_info = current->group_info,
249 struct rpc_cred *ret;
251 dprintk("RPC: looking up %s cred\n",
252 auth->au_ops->au_name);
253 get_group_info(acred.group_info);
254 ret = auth->au_ops->lookup_cred(auth, &acred, taskflags);
255 put_group_info(acred.group_info);
256 return ret;
259 struct rpc_cred *
260 rpcauth_bindcred(struct rpc_task *task)
262 struct rpc_auth *auth = task->tk_auth;
263 struct auth_cred acred = {
264 .uid = current->fsuid,
265 .gid = current->fsgid,
266 .group_info = current->group_info,
268 struct rpc_cred *ret;
270 dprintk("RPC: %4d looking up %s cred\n",
271 task->tk_pid, task->tk_auth->au_ops->au_name);
272 get_group_info(acred.group_info);
273 ret = auth->au_ops->lookup_cred(auth, &acred, task->tk_flags);
274 if (!IS_ERR(ret))
275 task->tk_msg.rpc_cred = ret;
276 else
277 task->tk_status = PTR_ERR(ret);
278 put_group_info(acred.group_info);
279 return ret;
282 void
283 rpcauth_holdcred(struct rpc_task *task)
285 dprintk("RPC: %4d holding %s cred %p\n",
286 task->tk_pid, task->tk_auth->au_ops->au_name, task->tk_msg.rpc_cred);
287 if (task->tk_msg.rpc_cred)
288 get_rpccred(task->tk_msg.rpc_cred);
291 void
292 put_rpccred(struct rpc_cred *cred)
294 cred->cr_expire = jiffies;
295 if (!atomic_dec_and_test(&cred->cr_count))
296 return;
297 cred->cr_ops->crdestroy(cred);
300 void
301 rpcauth_unbindcred(struct rpc_task *task)
303 struct rpc_auth *auth = task->tk_auth;
304 struct rpc_cred *cred = task->tk_msg.rpc_cred;
306 dprintk("RPC: %4d releasing %s cred %p\n",
307 task->tk_pid, auth->au_ops->au_name, cred);
309 put_rpccred(cred);
310 task->tk_msg.rpc_cred = NULL;
313 u32 *
314 rpcauth_marshcred(struct rpc_task *task, u32 *p)
316 struct rpc_auth *auth = task->tk_auth;
317 struct rpc_cred *cred = task->tk_msg.rpc_cred;
319 dprintk("RPC: %4d marshaling %s cred %p\n",
320 task->tk_pid, auth->au_ops->au_name, cred);
321 return cred->cr_ops->crmarshal(task, p);
324 u32 *
325 rpcauth_checkverf(struct rpc_task *task, u32 *p)
327 struct rpc_auth *auth = task->tk_auth;
328 struct rpc_cred *cred = task->tk_msg.rpc_cred;
330 dprintk("RPC: %4d validating %s cred %p\n",
331 task->tk_pid, auth->au_ops->au_name, cred);
332 return cred->cr_ops->crvalidate(task, p);
336 rpcauth_wrap_req(struct rpc_task *task, kxdrproc_t encode, void *rqstp,
337 u32 *data, void *obj)
339 struct rpc_cred *cred = task->tk_msg.rpc_cred;
341 dprintk("RPC: %4d using %s cred %p to wrap rpc data\n",
342 task->tk_pid, cred->cr_ops->cr_name, cred);
343 if (cred->cr_ops->crwrap_req)
344 return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj);
345 /* By default, we encode the arguments normally. */
346 return encode(rqstp, data, obj);
350 rpcauth_unwrap_resp(struct rpc_task *task, kxdrproc_t decode, void *rqstp,
351 u32 *data, void *obj)
353 struct rpc_cred *cred = task->tk_msg.rpc_cred;
355 dprintk("RPC: %4d using %s cred %p to unwrap rpc data\n",
356 task->tk_pid, cred->cr_ops->cr_name, cred);
357 if (cred->cr_ops->crunwrap_resp)
358 return cred->cr_ops->crunwrap_resp(task, decode, rqstp,
359 data, obj);
360 /* By default, we decode the arguments normally. */
361 return decode(rqstp, data, obj);
365 rpcauth_refreshcred(struct rpc_task *task)
367 struct rpc_auth *auth = task->tk_auth;
368 struct rpc_cred *cred = task->tk_msg.rpc_cred;
369 int err;
371 dprintk("RPC: %4d refreshing %s cred %p\n",
372 task->tk_pid, auth->au_ops->au_name, cred);
373 err = cred->cr_ops->crrefresh(task);
374 if (err < 0)
375 task->tk_status = err;
376 return err;
379 void
380 rpcauth_invalcred(struct rpc_task *task)
382 dprintk("RPC: %4d invalidating %s cred %p\n",
383 task->tk_pid, task->tk_auth->au_ops->au_name, task->tk_msg.rpc_cred);
384 spin_lock(&rpc_credcache_lock);
385 if (task->tk_msg.rpc_cred)
386 task->tk_msg.rpc_cred->cr_flags &= ~RPCAUTH_CRED_UPTODATE;
387 spin_unlock(&rpc_credcache_lock);
391 rpcauth_uptodatecred(struct rpc_task *task)
393 return !(task->tk_msg.rpc_cred) ||
394 (task->tk_msg.rpc_cred->cr_flags & RPCAUTH_CRED_UPTODATE);