linux 2.6.16.38 w/ vs2.0.3-rc1
[linux-2.6.git] / net / sunrpc / auth.c
1 /*
2  * linux/net/sunrpc/auth.c
3  *
4  * Generic RPC client authentication API.
5  *
6  * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
7  */
8
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>
17
18 #ifdef RPC_DEBUG
19 # define RPCDBG_FACILITY        RPCDBG_AUTH
20 #endif
21
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 */
26 };
27
28 static u32
29 pseudoflavor_to_flavor(u32 flavor) {
30         if (flavor >= RPC_AUTH_MAXFLAVOR)
31                 return RPC_AUTH_GSS;
32         return flavor;
33 }
34
35 int
36 rpcauth_register(struct rpc_authops *ops)
37 {
38         rpc_authflavor_t flavor;
39
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;
46 }
47
48 int
49 rpcauth_unregister(struct rpc_authops *ops)
50 {
51         rpc_authflavor_t flavor;
52
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;
59 }
60
61 struct rpc_auth *
62 rpcauth_create(rpc_authflavor_t pseudoflavor, struct rpc_clnt *clnt)
63 {
64         struct rpc_auth         *auth;
65         struct rpc_authops      *ops;
66         u32                     flavor = pseudoflavor_to_flavor(pseudoflavor);
67
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;
77 }
78
79 void
80 rpcauth_destroy(struct rpc_auth *auth)
81 {
82         if (!atomic_dec_and_test(&auth->au_count))
83                 return;
84         auth->au_ops->destroy(auth);
85 }
86
87 static DEFINE_SPINLOCK(rpc_credcache_lock);
88
89 /*
90  * Initialize RPC credential cache
91  */
92 int
93 rpcauth_init_credcache(struct rpc_auth *auth, unsigned long expire)
94 {
95         struct rpc_cred_cache *new;
96         int i;
97
98         new = 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;
107 }
108
109 /*
110  * Destroy a list of credentials
111  */
112 static inline
113 void rpcauth_destroy_credlist(struct hlist_head *head)
114 {
115         struct rpc_cred *cred;
116
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);
121         }
122 }
123
124 /*
125  * Clear the RPC credential cache, and delete those credentials
126  * that are not referenced.
127  */
128 void
129 rpcauth_free_credcache(struct rpc_auth *auth)
130 {
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;
136
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);
143                 }
144         }
145         spin_unlock(&rpc_credcache_lock);
146         rpcauth_destroy_credlist(&free);
147 }
148
149 static void
150 rpcauth_prune_expired(struct rpc_auth *auth, struct rpc_cred *cred, struct hlist_head *free)
151 {
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);
159         }
160 }
161
162 /*
163  * Remove stale credentials. Avoid sleeping inside the loop.
164  */
165 static void
166 rpcauth_gc_credcache(struct rpc_auth *auth, struct hlist_head *free)
167 {
168         struct rpc_cred_cache *cache = auth->au_credcache;
169         struct hlist_node *pos, *next;
170         struct rpc_cred *cred;
171         int             i;
172
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);
178                 }
179         }
180         cache->nextgc = jiffies + cache->expire;
181 }
182
183 /*
184  * Look up a process' credentials in the authentication cache
185  */
186 struct rpc_cred *
187 rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred,
188                 int flags)
189 {
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;
196
197         if (!(flags & RPCAUTH_LOOKUP_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, flags)) {
207                         hlist_del(&entry->cr_hash);
208                         cred = entry;
209                         break;
210                 }
211                 rpcauth_prune_expired(auth, entry, &free);
212         }
213         if (new) {
214                 if (cred)
215                         hlist_add_head(&new->cr_hash, &free);
216                 else
217                         cred = new;
218         }
219         if (cred) {
220                 hlist_add_head(&cred->cr_hash, &cache->hashtable[nr]);
221                 get_rpccred(cred);
222         }
223         spin_unlock(&rpc_credcache_lock);
224
225         rpcauth_destroy_credlist(&free);
226
227         if (!cred) {
228                 new = auth->au_ops->crcreate(auth, acred, flags);
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;
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);
240                 if (res < 0) {
241                         put_rpccred(cred);
242                         cred = ERR_PTR(res);
243                 }
244         }
245
246         return (struct rpc_cred *) cred;
247 }
248
249 struct rpc_cred *
250 rpcauth_lookupcred(struct rpc_auth *auth, int flags)
251 {
252         struct auth_cred acred = {
253                 .uid = current->fsuid,
254                 .gid = current->fsgid,
255                 .xid = vx_current_xid(),
256                 .group_info = current->group_info,
257         };
258         struct rpc_cred *ret;
259
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);
265         return ret;
266 }
267
268 struct rpc_cred *
269 rpcauth_bindcred(struct rpc_task *task)
270 {
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,
277         };
278         struct rpc_cred *ret;
279         int flags = 0;
280
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);
287         if (!IS_ERR(ret))
288                 task->tk_msg.rpc_cred = ret;
289         else
290                 task->tk_status = PTR_ERR(ret);
291         put_group_info(acred.group_info);
292         return ret;
293 }
294
295 void
296 rpcauth_holdcred(struct rpc_task *task)
297 {
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);
302 }
303
304 void
305 put_rpccred(struct rpc_cred *cred)
306 {
307         cred->cr_expire = jiffies;
308         if (!atomic_dec_and_test(&cred->cr_count))
309                 return;
310         cred->cr_ops->crdestroy(cred);
311 }
312
313 void
314 rpcauth_unbindcred(struct rpc_task *task)
315 {
316         struct rpc_cred *cred = task->tk_msg.rpc_cred;
317
318         dprintk("RPC: %4d releasing %s cred %p\n",
319                 task->tk_pid, task->tk_auth->au_ops->au_name, cred);
320
321         put_rpccred(cred);
322         task->tk_msg.rpc_cred = NULL;
323 }
324
325 u32 *
326 rpcauth_marshcred(struct rpc_task *task, u32 *p)
327 {
328         struct rpc_cred *cred = task->tk_msg.rpc_cred;
329
330         dprintk("RPC: %4d marshaling %s cred %p\n",
331                 task->tk_pid, task->tk_auth->au_ops->au_name, cred);
332
333         return cred->cr_ops->crmarshal(task, p);
334 }
335
336 u32 *
337 rpcauth_checkverf(struct rpc_task *task, u32 *p)
338 {
339         struct rpc_cred *cred = task->tk_msg.rpc_cred;
340
341         dprintk("RPC: %4d validating %s cred %p\n",
342                 task->tk_pid, task->tk_auth->au_ops->au_name, cred);
343
344         return cred->cr_ops->crvalidate(task, p);
345 }
346
347 int
348 rpcauth_wrap_req(struct rpc_task *task, kxdrproc_t encode, void *rqstp,
349                 u32 *data, void *obj)
350 {
351         struct rpc_cred *cred = task->tk_msg.rpc_cred;
352
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);
359 }
360
361 int
362 rpcauth_unwrap_resp(struct rpc_task *task, kxdrproc_t decode, void *rqstp,
363                 u32 *data, void *obj)
364 {
365         struct rpc_cred *cred = task->tk_msg.rpc_cred;
366
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,
371                                                    data, obj);
372         /* By default, we decode the arguments normally. */
373         return decode(rqstp, data, obj);
374 }
375
376 int
377 rpcauth_refreshcred(struct rpc_task *task)
378 {
379         struct rpc_cred *cred = task->tk_msg.rpc_cred;
380         int err;
381
382         dprintk("RPC: %4d refreshing %s cred %p\n",
383                 task->tk_pid, task->tk_auth->au_ops->au_name, cred);
384
385         err = cred->cr_ops->crrefresh(task);
386         if (err < 0)
387                 task->tk_status = err;
388         return err;
389 }
390
391 void
392 rpcauth_invalcred(struct rpc_task *task)
393 {
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);
400 }
401
402 int
403 rpcauth_uptodatecred(struct rpc_task *task)
404 {
405         return !(task->tk_msg.rpc_cred) ||
406                 (task->tk_msg.rpc_cred->cr_flags & RPCAUTH_CRED_UPTODATE);
407 }