vserver 1.9.5.x5
[linux-2.6.git] / net / sunrpc / auth_gss / auth_gss.c
1 /*
2  * linux/net/sunrpc/auth_gss.c
3  *
4  * RPCSEC_GSS client authentication.
5  * 
6  *  Copyright (c) 2000 The Regents of the University of Michigan.
7  *  All rights reserved.
8  *
9  *  Dug Song       <dugsong@monkey.org>
10  *  Andy Adamson   <andros@umich.edu>
11  *
12  *  Redistribution and use in source and binary forms, with or without
13  *  modification, are permitted provided that the following conditions
14  *  are met:
15  *
16  *  1. Redistributions of source code must retain the above copyright
17  *     notice, this list of conditions and the following disclaimer.
18  *  2. Redistributions in binary form must reproduce the above copyright
19  *     notice, this list of conditions and the following disclaimer in the
20  *     documentation and/or other materials provided with the distribution.
21  *  3. Neither the name of the University nor the names of its
22  *     contributors may be used to endorse or promote products derived
23  *     from this software without specific prior written permission.
24  *
25  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28  *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32  *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  *
37  * $Id$
38  */
39
40
41 #include <linux/module.h>
42 #include <linux/init.h>
43 #include <linux/types.h>
44 #include <linux/slab.h>
45 #include <linux/socket.h>
46 #include <linux/in.h>
47 #include <linux/sched.h>
48 #include <linux/sunrpc/clnt.h>
49 #include <linux/sunrpc/auth.h>
50 #include <linux/sunrpc/auth_gss.h>
51 #include <linux/sunrpc/svcauth_gss.h>
52 #include <linux/sunrpc/gss_err.h>
53 #include <linux/workqueue.h>
54 #include <linux/sunrpc/rpc_pipe_fs.h>
55 #include <linux/sunrpc/gss_api.h>
56 #include <asm/uaccess.h>
57
58 static struct rpc_authops authgss_ops;
59
60 static struct rpc_credops gss_credops;
61
62 #ifdef RPC_DEBUG
63 # define RPCDBG_FACILITY        RPCDBG_AUTH
64 #endif
65
66 #define NFS_NGROUPS     16
67
68 #define GSS_CRED_EXPIRE         (60 * HZ)       /* XXX: reasonable? */
69 #define GSS_CRED_SLACK          1024            /* XXX: unused */
70 /* length of a krb5 verifier (48), plus data added before arguments when
71  * using integrity (two 4-byte integers): */
72 #define GSS_VERF_SLACK          56
73
74 /* XXX this define must match the gssd define
75 * as it is passed to gssd to signal the use of
76 * machine creds should be part of the shared rpc interface */
77
78 #define CA_RUN_AS_MACHINE  0x00000200 
79
80 /* dump the buffer in `emacs-hexl' style */
81 #define isprint(c)      ((c > 0x1f) && (c < 0x7f))
82
83 static DEFINE_RWLOCK(gss_ctx_lock);
84
85 struct gss_auth {
86         struct rpc_auth rpc_auth;
87         struct gss_api_mech *mech;
88         struct list_head upcalls;
89         struct dentry *dentry;
90         char path[48];
91         spinlock_t lock;
92 };
93
94 static void gss_destroy_ctx(struct gss_cl_ctx *);
95 static struct rpc_pipe_ops gss_upcall_ops;
96
97 void
98 print_hexl(u32 *p, u_int length, u_int offset)
99 {
100         u_int i, j, jm;
101         u8 c, *cp;
102         
103         dprintk("RPC: print_hexl: length %d\n",length);
104         dprintk("\n");
105         cp = (u8 *) p;
106         
107         for (i = 0; i < length; i += 0x10) {
108                 dprintk("  %04x: ", (u_int)(i + offset));
109                 jm = length - i;
110                 jm = jm > 16 ? 16 : jm;
111                 
112                 for (j = 0; j < jm; j++) {
113                         if ((j % 2) == 1)
114                                 dprintk("%02x ", (u_int)cp[i+j]);
115                         else
116                                 dprintk("%02x", (u_int)cp[i+j]);
117                 }
118                 for (; j < 16; j++) {
119                         if ((j % 2) == 1)
120                                 dprintk("   ");
121                         else
122                                 dprintk("  ");
123                 }
124                 dprintk(" ");
125                 
126                 for (j = 0; j < jm; j++) {
127                         c = cp[i+j];
128                         c = isprint(c) ? c : '.';
129                         dprintk("%c", c);
130                 }
131                 dprintk("\n");
132         }
133 }
134
135 EXPORT_SYMBOL(print_hexl);
136
137 static inline struct gss_cl_ctx *
138 gss_get_ctx(struct gss_cl_ctx *ctx)
139 {
140         atomic_inc(&ctx->count);
141         return ctx;
142 }
143
144 static inline void
145 gss_put_ctx(struct gss_cl_ctx *ctx)
146 {
147         if (atomic_dec_and_test(&ctx->count))
148                 gss_destroy_ctx(ctx);
149 }
150
151 static void
152 gss_cred_set_ctx(struct rpc_cred *cred, struct gss_cl_ctx *ctx)
153 {
154         struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
155         struct gss_cl_ctx *old;
156         write_lock(&gss_ctx_lock);
157         old = gss_cred->gc_ctx;
158         gss_cred->gc_ctx = ctx;
159         cred->cr_flags |= RPCAUTH_CRED_UPTODATE;
160         write_unlock(&gss_ctx_lock);
161         if (old)
162                 gss_put_ctx(old);
163 }
164
165 static int
166 gss_cred_is_uptodate_ctx(struct rpc_cred *cred)
167 {
168         struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
169         int res = 0;
170
171         read_lock(&gss_ctx_lock);
172         if ((cred->cr_flags & RPCAUTH_CRED_UPTODATE) && gss_cred->gc_ctx)
173                 res = 1;
174         read_unlock(&gss_ctx_lock);
175         return res;
176 }
177
178 static inline int
179 simple_get_bytes(char **ptr, const char *end, void *res, int len)
180 {
181         char *p, *q;
182         p = *ptr;
183         q = p + len;
184         if (q > end || q < p)
185                 return -1;
186         memcpy(res, p, len);
187         *ptr = q;
188         return 0;
189 }
190
191 static inline int
192 simple_get_netobj(char **ptr, const char *end, struct xdr_netobj *res)
193 {
194         char *p, *q;
195         p = *ptr;
196         if (simple_get_bytes(&p, end, &res->len, sizeof(res->len)))
197                 return -1;
198         q = p + res->len;
199         if (q > end || q < p)
200                 return -1;
201         res->data = p;
202         *ptr = q;
203         return 0;
204 }
205
206 static int
207 dup_netobj(struct xdr_netobj *source, struct xdr_netobj *dest)
208 {
209         dest->len = source->len;
210         if (!(dest->data = kmalloc(dest->len, GFP_KERNEL)))
211                 return -1;
212         memcpy(dest->data, source->data, dest->len);
213         return 0;
214 }
215
216 static struct gss_cl_ctx *
217 gss_cred_get_ctx(struct rpc_cred *cred)
218 {
219         struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
220         struct gss_cl_ctx *ctx = NULL;
221
222         read_lock(&gss_ctx_lock);
223         if (gss_cred->gc_ctx)
224                 ctx = gss_get_ctx(gss_cred->gc_ctx);
225         read_unlock(&gss_ctx_lock);
226         return ctx;
227 }
228
229 static int
230 gss_parse_init_downcall(struct gss_api_mech *gm, struct xdr_netobj *buf,
231                 struct gss_cl_ctx **gc, uid_t *uid, int *gss_err)
232 {
233         char *end = buf->data + buf->len;
234         char *p = buf->data;
235         struct gss_cl_ctx *ctx;
236         struct xdr_netobj tmp_buf;
237         unsigned int timeout;
238         int err = -EIO;
239
240         if (!(ctx = kmalloc(sizeof(*ctx), GFP_KERNEL))) {
241                 err = -ENOMEM;
242                 goto err;
243         }
244         ctx->gc_proc = RPC_GSS_PROC_DATA;
245         ctx->gc_seq = 1;        /* NetApp 6.4R1 doesn't accept seq. no. 0 */
246         spin_lock_init(&ctx->gc_seq_lock);
247         atomic_set(&ctx->count,1);
248
249         if (simple_get_bytes(&p, end, uid, sizeof(*uid)))
250                 goto err_free_ctx;
251         /* FIXME: discarded timeout for now */
252         if (simple_get_bytes(&p, end, &timeout, sizeof(timeout)))
253                 goto err_free_ctx;
254         *gss_err = 0;
255         if (simple_get_bytes(&p, end, &ctx->gc_win, sizeof(ctx->gc_win)))
256                 goto err_free_ctx;
257         /* gssd signals an error by passing ctx->gc_win = 0: */
258         if (!ctx->gc_win) {
259                 /* in which case the next int is an error code: */
260                 if (simple_get_bytes(&p, end, gss_err, sizeof(*gss_err)))
261                         goto err_free_ctx;
262                 err = 0;
263                 goto err_free_ctx;
264         }
265         if (simple_get_netobj(&p, end, &tmp_buf))
266                 goto err_free_ctx;
267         if (dup_netobj(&tmp_buf, &ctx->gc_wire_ctx)) {
268                 err = -ENOMEM;
269                 goto err_free_ctx;
270         }
271         if (simple_get_netobj(&p, end, &tmp_buf))
272                 goto err_free_wire_ctx;
273         if (p != end)
274                 goto err_free_wire_ctx;
275         if (gss_import_sec_context(&tmp_buf, gm, &ctx->gc_gss_ctx))
276                 goto err_free_wire_ctx;
277         *gc = ctx;
278         return 0;
279 err_free_wire_ctx:
280         kfree(ctx->gc_wire_ctx.data);
281 err_free_ctx:
282         kfree(ctx);
283 err:
284         *gc = NULL;
285         dprintk("RPC:      gss_parse_init_downcall returning %d\n", err);
286         return err;
287 }
288
289
290 struct gss_upcall_msg {
291         struct rpc_pipe_msg msg;
292         struct list_head list;
293         struct gss_auth *auth;
294         struct rpc_wait_queue waitq;
295         uid_t   uid;
296         atomic_t count;
297 };
298
299 static void
300 gss_release_msg(struct gss_upcall_msg *gss_msg)
301 {
302         if (!atomic_dec_and_test(&gss_msg->count))
303                 return;
304         BUG_ON(!list_empty(&gss_msg->list));
305         kfree(gss_msg);
306 }
307
308 static struct gss_upcall_msg *
309 __gss_find_upcall(struct gss_auth *gss_auth, uid_t uid)
310 {
311         struct gss_upcall_msg *pos;
312         list_for_each_entry(pos, &gss_auth->upcalls, list) {
313                 if (pos->uid != uid)
314                         continue;
315                 atomic_inc(&pos->count);
316                 dprintk("RPC:      gss_find_upcall found msg %p\n", pos);
317                 return pos;
318         }
319         dprintk("RPC:      gss_find_upcall found nothing\n");
320         return NULL;
321 }
322
323 static void
324 __gss_unhash_msg(struct gss_upcall_msg *gss_msg)
325 {
326         if (list_empty(&gss_msg->list))
327                 return;
328         list_del_init(&gss_msg->list);
329         if (gss_msg->msg.errno < 0)
330                 rpc_wake_up_status(&gss_msg->waitq, gss_msg->msg.errno);
331         else
332                 rpc_wake_up(&gss_msg->waitq);
333         atomic_dec(&gss_msg->count);
334 }
335
336 static void
337 gss_unhash_msg(struct gss_upcall_msg *gss_msg)
338 {
339         struct gss_auth *gss_auth = gss_msg->auth;
340
341         spin_lock(&gss_auth->lock);
342         __gss_unhash_msg(gss_msg);
343         spin_unlock(&gss_auth->lock);
344 }
345
346 static int
347 gss_upcall(struct rpc_clnt *clnt, struct rpc_task *task, struct rpc_cred *cred)
348 {
349         struct gss_auth *gss_auth = container_of(clnt->cl_auth,
350                         struct gss_auth, rpc_auth);
351         struct gss_upcall_msg *gss_msg, *gss_new = NULL;
352         struct rpc_pipe_msg *msg;
353         struct dentry *dentry = gss_auth->dentry;
354         uid_t uid = cred->cr_uid;
355         int res = 0;
356
357         dprintk("RPC: %4u gss_upcall for uid %u\n", task->tk_pid, uid);
358
359 retry:
360         spin_lock(&gss_auth->lock);
361         gss_msg = __gss_find_upcall(gss_auth, uid);
362         if (gss_msg)
363                 goto out_sleep;
364         if (gss_new == NULL) {
365                 spin_unlock(&gss_auth->lock);
366                 gss_new = kmalloc(sizeof(*gss_new), GFP_KERNEL);
367                 if (!gss_new) {
368                         dprintk("RPC: %4u gss_upcall -ENOMEM\n", task->tk_pid);
369                         return -ENOMEM;
370                 }
371                 goto retry;
372         }
373         gss_msg = gss_new;
374         memset(gss_new, 0, sizeof(*gss_new));
375         INIT_LIST_HEAD(&gss_new->list);
376         rpc_init_wait_queue(&gss_new->waitq, "RPCSEC_GSS upcall waitq");
377         atomic_set(&gss_new->count, 2);
378         msg = &gss_new->msg;
379         msg->data = &gss_new->uid;
380         msg->len = sizeof(gss_new->uid);
381         gss_new->uid = uid;
382         gss_new->auth = gss_auth;
383         list_add(&gss_new->list, &gss_auth->upcalls);
384         gss_new = NULL;
385         /* Has someone updated the credential behind our back? */
386         if (!gss_cred_is_uptodate_ctx(cred)) {
387                 /* No, so do upcall and sleep */
388                 task->tk_timeout = 0;
389                 rpc_sleep_on(&gss_msg->waitq, task, NULL, NULL);
390                 spin_unlock(&gss_auth->lock);
391                 res = rpc_queue_upcall(dentry->d_inode, msg);
392                 if (res)
393                         gss_unhash_msg(gss_msg);
394         } else {
395                 /* Yes, so cancel upcall */
396                 __gss_unhash_msg(gss_msg);
397                 spin_unlock(&gss_auth->lock);
398         }
399         gss_release_msg(gss_msg);
400         dprintk("RPC: %4u gss_upcall for uid %u result %d\n", task->tk_pid,
401                         uid, res);
402         return res;
403 out_sleep:
404         task->tk_timeout = 0;
405         rpc_sleep_on(&gss_msg->waitq, task, NULL, NULL);
406         spin_unlock(&gss_auth->lock);
407         dprintk("RPC: %4u gss_upcall  sleeping\n", task->tk_pid);
408         if (gss_new)
409                 kfree(gss_new);
410         /* Note: we drop the reference here: we are automatically removed
411          * from the queue when we're woken up, and we should in any case
412          * have no further responsabilities w.r.t. the upcall.
413          */
414         gss_release_msg(gss_msg);
415         return 0;
416 }
417
418 static ssize_t
419 gss_pipe_upcall(struct file *filp, struct rpc_pipe_msg *msg,
420                 char __user *dst, size_t buflen)
421 {
422         char *data = (char *)msg->data + msg->copied;
423         ssize_t mlen = msg->len;
424         ssize_t left;
425
426         if (mlen > buflen)
427                 mlen = buflen;
428         left = copy_to_user(dst, data, mlen);
429         if (left < 0) {
430                 msg->errno = left;
431                 return left;
432         }
433         mlen -= left;
434         msg->copied += mlen;
435         msg->errno = 0;
436         return mlen;
437 }
438
439 #define MSG_BUF_MAXSIZE 1024
440
441 static ssize_t
442 gss_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
443 {
444         struct xdr_netobj obj = {
445                 .len    = mlen,
446         };
447         struct inode *inode = filp->f_dentry->d_inode;
448         struct rpc_inode *rpci = RPC_I(inode);
449         struct rpc_clnt *clnt;
450         struct rpc_auth *auth;
451         struct gss_auth *gss_auth;
452         struct gss_api_mech *mech;
453         struct auth_cred acred = { 0 };
454         struct rpc_cred *cred;
455         struct gss_upcall_msg *gss_msg;
456         struct gss_cl_ctx *ctx = NULL;
457         ssize_t left;
458         int err;
459         int gss_err;
460
461         if (mlen > MSG_BUF_MAXSIZE)
462                 return -EFBIG;
463         obj.data = kmalloc(mlen, GFP_KERNEL);
464         if (!obj.data)
465                 return -ENOMEM;
466         left = copy_from_user(obj.data, src, mlen);
467         if (left) {
468                 err = -EFAULT;
469                 goto out;
470         }
471         clnt = rpci->private;
472         atomic_inc(&clnt->cl_users);
473         auth = clnt->cl_auth;
474         gss_auth = container_of(auth, struct gss_auth, rpc_auth);
475         mech = gss_auth->mech;
476         err = gss_parse_init_downcall(mech, &obj, &ctx, &acred.uid, &gss_err);
477         if (err)
478                 goto err;
479         cred = rpcauth_lookup_credcache(auth, &acred, 0);
480         if (!cred)
481                 goto err;
482         if (gss_err)
483                 cred->cr_flags &= ~RPCAUTH_CRED_UPTODATE;
484         else
485                 gss_cred_set_ctx(cred, ctx);
486         spin_lock(&gss_auth->lock);
487         gss_msg = __gss_find_upcall(gss_auth, acred.uid);
488         if (gss_msg) {
489                 if (gss_err)
490                         gss_msg->msg.errno = -EACCES;
491                 __gss_unhash_msg(gss_msg);
492                 spin_unlock(&gss_auth->lock);
493                 gss_release_msg(gss_msg);
494         } else
495                 spin_unlock(&gss_auth->lock);
496         rpc_release_client(clnt);
497         kfree(obj.data);
498         dprintk("RPC:      gss_pipe_downcall returning length %Zu\n", mlen);
499         return mlen;
500 err:
501         if (ctx)
502                 gss_destroy_ctx(ctx);
503         rpc_release_client(clnt);
504 out:
505         kfree(obj.data);
506         dprintk("RPC:      gss_pipe_downcall returning %d\n", err);
507         return err;
508 }
509
510 static void
511 gss_pipe_release(struct inode *inode)
512 {
513         struct rpc_inode *rpci = RPC_I(inode);
514         struct rpc_clnt *clnt;
515         struct rpc_auth *auth;
516         struct gss_auth *gss_auth;
517
518         clnt = rpci->private;
519         auth = clnt->cl_auth;
520         gss_auth = container_of(auth, struct gss_auth, rpc_auth);
521         spin_lock(&gss_auth->lock);
522         while (!list_empty(&gss_auth->upcalls)) {
523                 struct gss_upcall_msg *gss_msg;
524
525                 gss_msg = list_entry(gss_auth->upcalls.next,
526                                 struct gss_upcall_msg, list);
527                 gss_msg->msg.errno = -EPIPE;
528                 atomic_inc(&gss_msg->count);
529                 __gss_unhash_msg(gss_msg);
530                 spin_unlock(&gss_auth->lock);
531                 gss_release_msg(gss_msg);
532                 spin_lock(&gss_auth->lock);
533         }
534         spin_unlock(&gss_auth->lock);
535 }
536
537 static void
538 gss_pipe_destroy_msg(struct rpc_pipe_msg *msg)
539 {
540         struct gss_upcall_msg *gss_msg = container_of(msg, struct gss_upcall_msg, msg);
541         static unsigned long ratelimit;
542
543         if (msg->errno < 0) {
544                 dprintk("RPC:      gss_pipe_destroy_msg releasing msg %p\n",
545                                 gss_msg);
546                 atomic_inc(&gss_msg->count);
547                 gss_unhash_msg(gss_msg);
548                 if (msg->errno == -ETIMEDOUT || msg->errno == -EPIPE) {
549                         unsigned long now = jiffies;
550                         if (time_after(now, ratelimit)) {
551                                 printk(KERN_WARNING "RPC: AUTH_GSS upcall timed out.\n"
552                                                     "Please check user daemon is running!\n");
553                                 ratelimit = now + 15*HZ;
554                         }
555                 }
556                 gss_release_msg(gss_msg);
557         }
558 }
559
560 /* 
561  * NOTE: we have the opportunity to use different 
562  * parameters based on the input flavor (which must be a pseudoflavor)
563  */
564 static struct rpc_auth *
565 gss_create(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
566 {
567         struct gss_auth *gss_auth;
568         struct rpc_auth * auth;
569
570         dprintk("RPC:      creating GSS authenticator for client %p\n",clnt);
571
572         if (!(gss_auth = kmalloc(sizeof(*gss_auth), GFP_KERNEL)))
573                 goto out_dec;
574         gss_auth->mech = gss_mech_get_by_pseudoflavor(flavor);
575         if (!gss_auth->mech) {
576                 printk(KERN_WARNING "%s: Pseudoflavor %d not found!",
577                                 __FUNCTION__, flavor);
578                 goto err_free;
579         }
580         INIT_LIST_HEAD(&gss_auth->upcalls);
581         spin_lock_init(&gss_auth->lock);
582         auth = &gss_auth->rpc_auth;
583         auth->au_cslack = GSS_CRED_SLACK >> 2;
584         auth->au_rslack = GSS_VERF_SLACK >> 2;
585         auth->au_expire = GSS_CRED_EXPIRE;
586         auth->au_ops = &authgss_ops;
587         auth->au_flavor = flavor;
588
589         rpcauth_init_credcache(auth);
590
591         snprintf(gss_auth->path, sizeof(gss_auth->path), "%s/%s",
592                         clnt->cl_pathname,
593                         gss_auth->mech->gm_name);
594         gss_auth->dentry = rpc_mkpipe(gss_auth->path, clnt, &gss_upcall_ops, RPC_PIPE_WAIT_FOR_OPEN);
595         if (IS_ERR(gss_auth->dentry))
596                 goto err_put_mech;
597
598         return auth;
599 err_put_mech:
600         gss_mech_put(gss_auth->mech);
601 err_free:
602         kfree(gss_auth);
603 out_dec:
604         return NULL;
605 }
606
607 static void
608 gss_destroy(struct rpc_auth *auth)
609 {
610         struct gss_auth *gss_auth;
611
612         dprintk("RPC:      destroying GSS authenticator %p flavor %d\n",
613                 auth, auth->au_flavor);
614
615         gss_auth = container_of(auth, struct gss_auth, rpc_auth);
616         rpc_unlink(gss_auth->path);
617         gss_mech_put(gss_auth->mech);
618
619         rpcauth_free_credcache(auth);
620 }
621
622 /* gss_destroy_cred (and gss_destroy_ctx) are used to clean up after failure
623  * to create a new cred or context, so they check that things have been
624  * allocated before freeing them. */
625 static void
626 gss_destroy_ctx(struct gss_cl_ctx *ctx)
627 {
628         dprintk("RPC:      gss_destroy_ctx\n");
629
630         if (ctx->gc_gss_ctx)
631                 gss_delete_sec_context(&ctx->gc_gss_ctx);
632
633         if (ctx->gc_wire_ctx.len > 0) {
634                 kfree(ctx->gc_wire_ctx.data);
635                 ctx->gc_wire_ctx.len = 0;
636         }
637
638         kfree(ctx);
639
640 }
641
642 static void
643 gss_destroy_cred(struct rpc_cred *rc)
644 {
645         struct gss_cred *cred = (struct gss_cred *)rc;
646
647         dprintk("RPC:      gss_destroy_cred \n");
648
649         if (cred->gc_ctx)
650                 gss_put_ctx(cred->gc_ctx);
651         kfree(cred);
652 }
653
654 static struct rpc_cred *
655 gss_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int taskflags)
656 {
657         struct gss_cred *cred = NULL;
658
659         dprintk("RPC:      gss_create_cred for uid %d, flavor %d\n",
660                 acred->uid, auth->au_flavor);
661
662         if (!(cred = kmalloc(sizeof(*cred), GFP_KERNEL)))
663                 goto out_err;
664
665         memset(cred, 0, sizeof(*cred));
666         atomic_set(&cred->gc_count, 0);
667         cred->gc_uid = acred->uid;
668         /*
669          * Note: in order to force a call to call_refresh(), we deliberately
670          * fail to flag the credential as RPCAUTH_CRED_UPTODATE.
671          */
672         cred->gc_flags = 0;
673         cred->gc_base.cr_ops = &gss_credops;
674         cred->gc_flavor = auth->au_flavor;
675
676         return (struct rpc_cred *) cred;
677
678 out_err:
679         dprintk("RPC:      gss_create_cred failed\n");
680         if (cred) gss_destroy_cred((struct rpc_cred *)cred);
681         return NULL;
682 }
683
684 static int
685 gss_match(struct auth_cred *acred, struct rpc_cred *rc, int taskflags)
686 {
687         return (rc->cr_uid == acred->uid);
688 }
689
690 /*
691 * Marshal credentials.
692 * Maybe we should keep a cached credential for performance reasons.
693 */
694 static u32 *
695 gss_marshal(struct rpc_task *task, u32 *p, int ruid)
696 {
697         struct rpc_cred *cred = task->tk_msg.rpc_cred;
698         struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
699                                                  gc_base);
700         struct gss_cl_ctx       *ctx = gss_cred_get_ctx(cred);
701         u32             *cred_len;
702         struct rpc_rqst *req = task->tk_rqstp;
703         u32             maj_stat = 0;
704         struct xdr_netobj mic;
705         struct kvec     iov;
706         struct xdr_buf  verf_buf;
707         u32             service;
708
709         dprintk("RPC: %4u gss_marshal\n", task->tk_pid);
710
711         *p++ = htonl(RPC_AUTH_GSS);
712         cred_len = p++;
713
714         service = gss_pseudoflavor_to_service(ctx->gc_gss_ctx->mech_type,
715                                                 gss_cred->gc_flavor);
716         if (service == 0) {
717                 dprintk("RPC: %4u Bad pseudoflavor %d in gss_marshal\n",
718                         task->tk_pid, gss_cred->gc_flavor);
719                 goto out_put_ctx;
720         }
721         spin_lock(&ctx->gc_seq_lock);
722         req->rq_seqno = ctx->gc_seq++;
723         spin_unlock(&ctx->gc_seq_lock);
724
725         *p++ = htonl((u32) RPC_GSS_VERSION);
726         *p++ = htonl((u32) ctx->gc_proc);
727         *p++ = htonl((u32) req->rq_seqno);
728         *p++ = htonl((u32) service);
729         p = xdr_encode_netobj(p, &ctx->gc_wire_ctx);
730         *cred_len = htonl((p - (cred_len + 1)) << 2);
731
732         /* We compute the checksum for the verifier over the xdr-encoded bytes
733          * starting with the xid and ending at the end of the credential: */
734         iov.iov_base = req->rq_snd_buf.head[0].iov_base;
735         if (task->tk_client->cl_xprt->stream)
736                 /* See clnt.c:call_header() */
737                 iov.iov_base += 4;
738         iov.iov_len = (u8 *)p - (u8 *)iov.iov_base;
739         xdr_buf_from_iov(&iov, &verf_buf);
740
741         /* set verifier flavor*/
742         *p++ = htonl(RPC_AUTH_GSS);
743
744         mic.data = (u8 *)(p + 1);
745         maj_stat = gss_get_mic(ctx->gc_gss_ctx,
746                                GSS_C_QOP_DEFAULT, 
747                                &verf_buf, &mic);
748         if (maj_stat == GSS_S_CONTEXT_EXPIRED) {
749                 cred->cr_flags &= ~RPCAUTH_CRED_UPTODATE;
750         } else if (maj_stat != 0) {
751                 printk("gss_marshal: gss_get_mic FAILED (%d)\n", maj_stat);
752                 goto out_put_ctx;
753         }
754         p = xdr_encode_opaque(p, NULL, mic.len);
755         gss_put_ctx(ctx);
756         return p;
757 out_put_ctx:
758         gss_put_ctx(ctx);
759         return NULL;
760 }
761
762 /*
763 * Refresh credentials. XXX - finish
764 */
765 static int
766 gss_refresh(struct rpc_task *task)
767 {
768         struct rpc_clnt *clnt = task->tk_client;
769         struct rpc_cred *cred = task->tk_msg.rpc_cred;
770
771         if (!gss_cred_is_uptodate_ctx(cred))
772                 return gss_upcall(clnt, task, cred);
773         return 0;
774 }
775
776 static u32 *
777 gss_validate(struct rpc_task *task, u32 *p)
778 {
779         struct rpc_cred *cred = task->tk_msg.rpc_cred;
780         struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
781                                                 gc_base);
782         struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
783         u32             seq, qop_state;
784         struct kvec     iov;
785         struct xdr_buf  verf_buf;
786         struct xdr_netobj mic;
787         u32             flav,len;
788         u32             service;
789         u32             maj_stat;
790
791         dprintk("RPC: %4u gss_validate\n", task->tk_pid);
792
793         flav = ntohl(*p++);
794         if ((len = ntohl(*p++)) > RPC_MAX_AUTH_SIZE)
795                 goto out_bad;
796         if (flav != RPC_AUTH_GSS)
797                 goto out_bad;
798         seq = htonl(task->tk_rqstp->rq_seqno);
799         iov.iov_base = &seq;
800         iov.iov_len = sizeof(seq);
801         xdr_buf_from_iov(&iov, &verf_buf);
802         mic.data = (u8 *)p;
803         mic.len = len;
804
805         maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &verf_buf, &mic, &qop_state);
806         if (maj_stat == GSS_S_CONTEXT_EXPIRED)
807                 cred->cr_flags &= ~RPCAUTH_CRED_UPTODATE;
808         if (maj_stat)
809                 goto out_bad;
810        service = gss_pseudoflavor_to_service(ctx->gc_gss_ctx->mech_type,
811                                         gss_cred->gc_flavor);
812        switch (service) {
813        case RPC_GSS_SVC_NONE:
814                /* verifier data, flavor, length: */
815                task->tk_auth->au_rslack = XDR_QUADLEN(len) + 2;
816                break;
817        case RPC_GSS_SVC_INTEGRITY:
818                /* verifier data, flavor, length, length, sequence number: */
819                task->tk_auth->au_rslack = XDR_QUADLEN(len) + 4;
820                break;
821        default:
822                goto out_bad;
823        }
824         gss_put_ctx(ctx);
825         dprintk("RPC: %4u GSS gss_validate: gss_verify_mic succeeded.\n",
826                         task->tk_pid);
827         return p + XDR_QUADLEN(len);
828 out_bad:
829         gss_put_ctx(ctx);
830         dprintk("RPC: %4u gss_validate failed.\n", task->tk_pid);
831         return NULL;
832 }
833
834 static inline int
835 gss_wrap_req_integ(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
836                 kxdrproc_t encode, struct rpc_rqst *rqstp, u32 *p, void *obj)
837 {
838         struct xdr_buf  *snd_buf = &rqstp->rq_snd_buf;
839         struct xdr_buf  integ_buf;
840         u32             *integ_len = NULL;
841         struct xdr_netobj mic;
842         u32             offset, *q;
843         struct kvec     *iov;
844         u32             maj_stat = 0;
845         int             status = -EIO;
846
847         integ_len = p++;
848         offset = (u8 *)p - (u8 *)snd_buf->head[0].iov_base;
849         *p++ = htonl(rqstp->rq_seqno);
850
851         status = encode(rqstp, p, obj);
852         if (status)
853                 return status;
854
855         if (xdr_buf_subsegment(snd_buf, &integ_buf,
856                                 offset, snd_buf->len - offset))
857                 return status;
858         *integ_len = htonl(integ_buf.len);
859
860         /* guess whether we're in the head or the tail: */
861         if (snd_buf->page_len || snd_buf->tail[0].iov_len) 
862                 iov = snd_buf->tail;
863         else
864                 iov = snd_buf->head;
865         p = iov->iov_base + iov->iov_len;
866         mic.data = (u8 *)(p + 1);
867
868         maj_stat = gss_get_mic(ctx->gc_gss_ctx,
869                         GSS_C_QOP_DEFAULT, &integ_buf, &mic);
870         status = -EIO; /* XXX? */
871         if (maj_stat == GSS_S_CONTEXT_EXPIRED)
872                 cred->cr_flags &= ~RPCAUTH_CRED_UPTODATE;
873         else if (maj_stat)
874                 return status;
875         q = xdr_encode_opaque(p, NULL, mic.len);
876
877         offset = (u8 *)q - (u8 *)p;
878         iov->iov_len += offset;
879         snd_buf->len += offset;
880         return 0;
881 }
882
883 static int
884 gss_wrap_req(struct rpc_task *task,
885              kxdrproc_t encode, void *rqstp, u32 *p, void *obj)
886 {
887         struct rpc_cred *cred = task->tk_msg.rpc_cred;
888         struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
889                         gc_base);
890         struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
891         int             status = -EIO;
892         u32             service;
893
894         dprintk("RPC: %4u gss_wrap_req\n", task->tk_pid);
895         if (ctx->gc_proc != RPC_GSS_PROC_DATA) {
896                 /* The spec seems a little ambiguous here, but I think that not
897                  * wrapping context destruction requests makes the most sense.
898                  */
899                 status = encode(rqstp, p, obj);
900                 goto out;
901         }
902         service = gss_pseudoflavor_to_service(ctx->gc_gss_ctx->mech_type,
903                                                 gss_cred->gc_flavor);
904         switch (service) {
905                 case RPC_GSS_SVC_NONE:
906                         status = encode(rqstp, p, obj);
907                         goto out;
908                 case RPC_GSS_SVC_INTEGRITY:
909                         status = gss_wrap_req_integ(cred, ctx, encode,
910                                                                 rqstp, p, obj);
911                         goto out;
912                 case RPC_GSS_SVC_PRIVACY:
913                 default:
914                         goto out;
915         }
916 out:
917         gss_put_ctx(ctx);
918         dprintk("RPC: %4u gss_wrap_req returning %d\n", task->tk_pid, status);
919         return status;
920 }
921
922 static inline int
923 gss_unwrap_resp_integ(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
924                 struct rpc_rqst *rqstp, u32 **p)
925 {
926         struct xdr_buf  *rcv_buf = &rqstp->rq_rcv_buf;
927         struct xdr_buf integ_buf;
928         struct xdr_netobj mic;
929         u32 data_offset, mic_offset;
930         u32 integ_len;
931         u32 maj_stat;
932         int status = -EIO;
933
934         integ_len = ntohl(*(*p)++);
935         if (integ_len & 3)
936                 return status;
937         data_offset = (u8 *)(*p) - (u8 *)rcv_buf->head[0].iov_base;
938         mic_offset = integ_len + data_offset;
939         if (mic_offset > rcv_buf->len)
940                 return status;
941         if (ntohl(*(*p)++) != rqstp->rq_seqno)
942                 return status;
943
944         if (xdr_buf_subsegment(rcv_buf, &integ_buf, data_offset,
945                                 mic_offset - data_offset))
946                 return status;
947
948         if (xdr_buf_read_netobj(rcv_buf, &mic, mic_offset))
949                 return status;
950
951         maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &integ_buf,
952                         &mic, NULL);
953         if (maj_stat == GSS_S_CONTEXT_EXPIRED)
954                 cred->cr_flags &= ~RPCAUTH_CRED_UPTODATE;
955         if (maj_stat != GSS_S_COMPLETE)
956                 return status;
957         return 0;
958 }
959
960 static int
961 gss_unwrap_resp(struct rpc_task *task,
962                 kxdrproc_t decode, void *rqstp, u32 *p, void *obj)
963 {
964         struct rpc_cred *cred = task->tk_msg.rpc_cred;
965         struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
966                         gc_base);
967         struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
968         int             status = -EIO;
969         u32             service;
970
971         if (ctx->gc_proc != RPC_GSS_PROC_DATA)
972                 goto out_decode;
973         service = gss_pseudoflavor_to_service(ctx->gc_gss_ctx->mech_type,
974                                                 gss_cred->gc_flavor);
975         switch (service) {
976                 case RPC_GSS_SVC_NONE:
977                         goto out_decode;
978                 case RPC_GSS_SVC_INTEGRITY:
979                         status = gss_unwrap_resp_integ(cred, ctx, rqstp, &p);
980                         if (status)
981                                 goto out;
982                         break;
983                 case RPC_GSS_SVC_PRIVACY:
984                 default:
985                         goto out;
986         }
987 out_decode:
988         status = decode(rqstp, p, obj);
989 out:
990         gss_put_ctx(ctx);
991         dprintk("RPC: %4u gss_unwrap_resp returning %d\n", task->tk_pid,
992                         status);
993         return status;
994 }
995   
996 static struct rpc_authops authgss_ops = {
997         .owner          = THIS_MODULE,
998         .au_flavor      = RPC_AUTH_GSS,
999 #ifdef RPC_DEBUG
1000         .au_name        = "RPCSEC_GSS",
1001 #endif
1002         .create         = gss_create,
1003         .destroy        = gss_destroy,
1004         .crcreate       = gss_create_cred
1005 };
1006
1007 static struct rpc_credops gss_credops = {
1008         .crdestroy      = gss_destroy_cred,
1009         .crmatch        = gss_match,
1010         .crmarshal      = gss_marshal,
1011         .crrefresh      = gss_refresh,
1012         .crvalidate     = gss_validate,
1013         .crwrap_req     = gss_wrap_req,
1014         .crunwrap_resp  = gss_unwrap_resp,
1015 };
1016
1017 static struct rpc_pipe_ops gss_upcall_ops = {
1018         .upcall         = gss_pipe_upcall,
1019         .downcall       = gss_pipe_downcall,
1020         .destroy_msg    = gss_pipe_destroy_msg,
1021         .release_pipe   = gss_pipe_release,
1022 };
1023
1024 /*
1025  * Initialize RPCSEC_GSS module
1026  */
1027 static int __init init_rpcsec_gss(void)
1028 {
1029         int err = 0;
1030
1031         err = rpcauth_register(&authgss_ops);
1032         if (err)
1033                 goto out;
1034         err = gss_svc_init();
1035         if (err)
1036                 goto out_unregister;
1037         return 0;
1038 out_unregister:
1039         rpcauth_unregister(&authgss_ops);
1040 out:
1041         return err;
1042 }
1043
1044 static void __exit exit_rpcsec_gss(void)
1045 {
1046         gss_svc_shutdown();
1047         rpcauth_unregister(&authgss_ops);
1048 }
1049
1050 MODULE_LICENSE("GPL");
1051 module_init(init_rpcsec_gss)
1052 module_exit(exit_rpcsec_gss)