patch-2_6_7-vs1_9_1_12
[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 rwlock_t gss_ctx_lock = RW_LOCK_UNLOCKED;
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", 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 *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 *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_DEAD;
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                 __gss_unhash_msg(gss_msg);
490                 spin_unlock(&gss_auth->lock);
491                 gss_release_msg(gss_msg);
492         } else
493                 spin_unlock(&gss_auth->lock);
494         rpc_release_client(clnt);
495         kfree(obj.data);
496         dprintk("RPC:      gss_pipe_downcall returning length %Zu\n", mlen);
497         return mlen;
498 err:
499         if (ctx)
500                 gss_destroy_ctx(ctx);
501         rpc_release_client(clnt);
502 out:
503         kfree(obj.data);
504         dprintk("RPC:      gss_pipe_downcall returning %d\n", err);
505         return err;
506 }
507
508 static void
509 gss_pipe_release(struct inode *inode)
510 {
511         struct rpc_inode *rpci = RPC_I(inode);
512         struct rpc_clnt *clnt;
513         struct rpc_auth *auth;
514         struct gss_auth *gss_auth;
515
516         clnt = rpci->private;
517         auth = clnt->cl_auth;
518         gss_auth = container_of(auth, struct gss_auth, rpc_auth);
519         spin_lock(&gss_auth->lock);
520         while (!list_empty(&gss_auth->upcalls)) {
521                 struct gss_upcall_msg *gss_msg;
522
523                 gss_msg = list_entry(gss_auth->upcalls.next,
524                                 struct gss_upcall_msg, list);
525                 gss_msg->msg.errno = -EPIPE;
526                 atomic_inc(&gss_msg->count);
527                 __gss_unhash_msg(gss_msg);
528                 spin_unlock(&gss_auth->lock);
529                 gss_release_msg(gss_msg);
530                 spin_lock(&gss_auth->lock);
531         }
532         spin_unlock(&gss_auth->lock);
533 }
534
535 void
536 gss_pipe_destroy_msg(struct rpc_pipe_msg *msg)
537 {
538         struct gss_upcall_msg *gss_msg = container_of(msg, struct gss_upcall_msg, msg);
539         static unsigned long ratelimit;
540
541         if (msg->errno < 0) {
542                 dprintk("RPC:      gss_pipe_destroy_msg releasing msg %p\n",
543                                 gss_msg);
544                 atomic_inc(&gss_msg->count);
545                 gss_unhash_msg(gss_msg);
546                 if (msg->errno == -ETIMEDOUT || msg->errno == -EPIPE) {
547                         unsigned long now = jiffies;
548                         if (time_after(now, ratelimit)) {
549                                 printk(KERN_WARNING "RPC: AUTH_GSS upcall timed out.\n"
550                                                     "Please check user daemon is running!\n");
551                                 ratelimit = now + 15*HZ;
552                         }
553                 }
554                 gss_release_msg(gss_msg);
555         }
556 }
557
558 /* 
559  * NOTE: we have the opportunity to use different 
560  * parameters based on the input flavor (which must be a pseudoflavor)
561  */
562 static struct rpc_auth *
563 gss_create(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
564 {
565         struct gss_auth *gss_auth;
566         struct rpc_auth * auth;
567
568         dprintk("RPC:      creating GSS authenticator for client %p\n",clnt);
569
570         if (!(gss_auth = kmalloc(sizeof(*gss_auth), GFP_KERNEL)))
571                 goto out_dec;
572         gss_auth->mech = gss_mech_get_by_pseudoflavor(flavor);
573         if (!gss_auth->mech) {
574                 printk(KERN_WARNING "%s: Pseudoflavor %d not found!",
575                                 __FUNCTION__, flavor);
576                 goto err_free;
577         }
578         INIT_LIST_HEAD(&gss_auth->upcalls);
579         spin_lock_init(&gss_auth->lock);
580         auth = &gss_auth->rpc_auth;
581         auth->au_cslack = GSS_CRED_SLACK >> 2;
582         auth->au_rslack = GSS_VERF_SLACK >> 2;
583         auth->au_expire = GSS_CRED_EXPIRE;
584         auth->au_ops = &authgss_ops;
585         auth->au_flavor = flavor;
586
587         rpcauth_init_credcache(auth);
588
589         snprintf(gss_auth->path, sizeof(gss_auth->path), "%s/%s",
590                         clnt->cl_pathname,
591                         gss_auth->mech->gm_name);
592         gss_auth->dentry = rpc_mkpipe(gss_auth->path, clnt, &gss_upcall_ops, RPC_PIPE_WAIT_FOR_OPEN);
593         if (IS_ERR(gss_auth->dentry))
594                 goto err_free;
595
596         return auth;
597 err_free:
598         kfree(gss_auth);
599 out_dec:
600         return NULL;
601 }
602
603 static void
604 gss_destroy(struct rpc_auth *auth)
605 {
606         struct gss_auth *gss_auth;
607
608         dprintk("RPC:      destroying GSS authenticator %p flavor %d\n",
609                 auth, auth->au_flavor);
610
611         gss_auth = container_of(auth, struct gss_auth, rpc_auth);
612         rpc_unlink(gss_auth->path);
613
614         rpcauth_free_credcache(auth);
615 }
616
617 /* gss_destroy_cred (and gss_destroy_ctx) are used to clean up after failure
618  * to create a new cred or context, so they check that things have been
619  * allocated before freeing them. */
620 static void
621 gss_destroy_ctx(struct gss_cl_ctx *ctx)
622 {
623         dprintk("RPC:      gss_destroy_ctx\n");
624
625         if (ctx->gc_gss_ctx)
626                 gss_delete_sec_context(&ctx->gc_gss_ctx);
627
628         if (ctx->gc_wire_ctx.len > 0) {
629                 kfree(ctx->gc_wire_ctx.data);
630                 ctx->gc_wire_ctx.len = 0;
631         }
632
633         kfree(ctx);
634
635 }
636
637 static void
638 gss_destroy_cred(struct rpc_cred *rc)
639 {
640         struct gss_cred *cred = (struct gss_cred *)rc;
641
642         dprintk("RPC:      gss_destroy_cred \n");
643
644         if (cred->gc_ctx)
645                 gss_put_ctx(cred->gc_ctx);
646         kfree(cred);
647 }
648
649 static struct rpc_cred *
650 gss_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int taskflags)
651 {
652         struct gss_cred *cred = NULL;
653
654         dprintk("RPC:      gss_create_cred for uid %d, flavor %d\n",
655                 acred->uid, auth->au_flavor);
656
657         if (!(cred = kmalloc(sizeof(*cred), GFP_KERNEL)))
658                 goto out_err;
659
660         memset(cred, 0, sizeof(*cred));
661         atomic_set(&cred->gc_count, 0);
662         cred->gc_uid = acred->uid;
663         /*
664          * Note: in order to force a call to call_refresh(), we deliberately
665          * fail to flag the credential as RPCAUTH_CRED_UPTODATE.
666          */
667         cred->gc_flags = 0;
668         cred->gc_base.cr_ops = &gss_credops;
669         cred->gc_flavor = auth->au_flavor;
670
671         return (struct rpc_cred *) cred;
672
673 out_err:
674         dprintk("RPC:      gss_create_cred failed\n");
675         if (cred) gss_destroy_cred((struct rpc_cred *)cred);
676         return NULL;
677 }
678
679 static int
680 gss_match(struct auth_cred *acred, struct rpc_cred *rc, int taskflags)
681 {
682         return (rc->cr_uid == acred->uid);
683 }
684
685 /*
686 * Marshal credentials.
687 * Maybe we should keep a cached credential for performance reasons.
688 */
689 static u32 *
690 gss_marshal(struct rpc_task *task, u32 *p, int ruid)
691 {
692         struct rpc_cred *cred = task->tk_msg.rpc_cred;
693         struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
694                                                  gc_base);
695         struct gss_cl_ctx       *ctx = gss_cred_get_ctx(cred);
696         u32             *cred_len;
697         struct rpc_rqst *req = task->tk_rqstp;
698         u32             maj_stat = 0;
699         struct xdr_netobj mic;
700         struct iovec    iov;
701         struct xdr_buf  verf_buf;
702         u32             service;
703
704         dprintk("RPC: %4u gss_marshal\n", task->tk_pid);
705
706         *p++ = htonl(RPC_AUTH_GSS);
707         cred_len = p++;
708
709         service = gss_pseudoflavor_to_service(ctx->gc_gss_ctx->mech_type,
710                                                 gss_cred->gc_flavor);
711         if (service == 0) {
712                 dprintk("RPC: %4u Bad pseudoflavor %d in gss_marshal\n",
713                         task->tk_pid, gss_cred->gc_flavor);
714                 goto out_put_ctx;
715         }
716         spin_lock(&ctx->gc_seq_lock);
717         req->rq_seqno = ctx->gc_seq++;
718         spin_unlock(&ctx->gc_seq_lock);
719
720         *p++ = htonl((u32) RPC_GSS_VERSION);
721         *p++ = htonl((u32) ctx->gc_proc);
722         *p++ = htonl((u32) req->rq_seqno);
723         *p++ = htonl((u32) service);
724         p = xdr_encode_netobj(p, &ctx->gc_wire_ctx);
725         *cred_len = htonl((p - (cred_len + 1)) << 2);
726
727         /* We compute the checksum for the verifier over the xdr-encoded bytes
728          * starting with the xid and ending at the end of the credential: */
729         iov.iov_base = req->rq_snd_buf.head[0].iov_base;
730         if (task->tk_client->cl_xprt->stream)
731                 /* See clnt.c:call_header() */
732                 iov.iov_base += 4;
733         iov.iov_len = (u8 *)p - (u8 *)iov.iov_base;
734         xdr_buf_from_iov(&iov, &verf_buf);
735
736         /* set verifier flavor*/
737         *p++ = htonl(RPC_AUTH_GSS);
738
739         mic.data = (u8 *)(p + 1);
740         maj_stat = gss_get_mic(ctx->gc_gss_ctx,
741                                GSS_C_QOP_DEFAULT, 
742                                &verf_buf, &mic);
743         if(maj_stat != 0){
744                 printk("gss_marshal: gss_get_mic FAILED (%d)\n", maj_stat);
745                 goto out_put_ctx;
746         }
747         p = xdr_encode_opaque(p, NULL, mic.len);
748         gss_put_ctx(ctx);
749         return p;
750 out_put_ctx:
751         gss_put_ctx(ctx);
752         return NULL;
753 }
754
755 /*
756 * Refresh credentials. XXX - finish
757 */
758 static int
759 gss_refresh(struct rpc_task *task)
760 {
761         struct rpc_clnt *clnt = task->tk_client;
762         struct rpc_cred *cred = task->tk_msg.rpc_cred;
763
764         if (!gss_cred_is_uptodate_ctx(cred))
765                 return gss_upcall(clnt, task, cred);
766         return 0;
767 }
768
769 static u32 *
770 gss_validate(struct rpc_task *task, u32 *p)
771 {
772         struct rpc_cred *cred = task->tk_msg.rpc_cred;
773         struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
774                                                 gc_base);
775         struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
776         u32             seq, qop_state;
777         struct iovec    iov;
778         struct xdr_buf  verf_buf;
779         struct xdr_netobj mic;
780         u32             flav,len;
781         u32             service;
782
783         dprintk("RPC: %4u gss_validate\n", task->tk_pid);
784
785         flav = ntohl(*p++);
786         if ((len = ntohl(*p++)) > RPC_MAX_AUTH_SIZE)
787                 goto out_bad;
788         if (flav != RPC_AUTH_GSS)
789                 goto out_bad;
790         seq = htonl(task->tk_rqstp->rq_seqno);
791         iov.iov_base = &seq;
792         iov.iov_len = sizeof(seq);
793         xdr_buf_from_iov(&iov, &verf_buf);
794         mic.data = (u8 *)p;
795         mic.len = len;
796
797         if (gss_verify_mic(ctx->gc_gss_ctx, &verf_buf, &mic, &qop_state))
798                goto out_bad;
799        service = gss_pseudoflavor_to_service(ctx->gc_gss_ctx->mech_type,
800                                         gss_cred->gc_flavor);
801        switch (service) {
802        case RPC_GSS_SVC_NONE:
803                /* verifier data, flavor, length: */
804                task->tk_auth->au_rslack = XDR_QUADLEN(len) + 2;
805                break;
806        case RPC_GSS_SVC_INTEGRITY:
807                /* verifier data, flavor, length, length, sequence number: */
808                task->tk_auth->au_rslack = XDR_QUADLEN(len) + 4;
809                break;
810        default:
811                goto out_bad;
812        }
813         gss_put_ctx(ctx);
814         dprintk("RPC: %4u GSS gss_validate: gss_verify_mic succeeded.\n",
815                         task->tk_pid);
816         return p + XDR_QUADLEN(len);
817 out_bad:
818         gss_put_ctx(ctx);
819         dprintk("RPC: %4u gss_validate failed.\n", task->tk_pid);
820         return NULL;
821 }
822
823 static inline int
824 gss_wrap_req_integ(struct gss_cl_ctx *ctx,
825                         kxdrproc_t encode, void *rqstp, u32 *p, void *obj)
826 {
827         struct rpc_rqst *req = (struct rpc_rqst *)rqstp;
828         struct xdr_buf  *snd_buf = &req->rq_snd_buf;
829         struct xdr_buf  integ_buf;
830         u32             *integ_len = NULL;
831         struct xdr_netobj mic;
832         u32             offset, *q;
833         struct iovec    *iov;
834         u32             maj_stat = 0;
835         int             status = -EIO;
836
837         integ_len = p++;
838         offset = (u8 *)p - (u8 *)snd_buf->head[0].iov_base;
839         *p++ = htonl(req->rq_seqno);
840
841         status = encode(rqstp, p, obj);
842         if (status)
843                 return status;
844
845         if (xdr_buf_subsegment(snd_buf, &integ_buf,
846                                 offset, snd_buf->len - offset))
847                 return status;
848         *integ_len = htonl(integ_buf.len);
849
850         /* guess whether we're in the head or the tail: */
851         if (snd_buf->page_len || snd_buf->tail[0].iov_len) 
852                 iov = snd_buf->tail;
853         else
854                 iov = snd_buf->head;
855         p = iov->iov_base + iov->iov_len;
856         mic.data = (u8 *)(p + 1);
857
858         maj_stat = gss_get_mic(ctx->gc_gss_ctx,
859                         GSS_C_QOP_DEFAULT, &integ_buf, &mic);
860         status = -EIO; /* XXX? */
861         if (maj_stat)
862                 return status;
863         q = xdr_encode_opaque(p, NULL, mic.len);
864
865         offset = (u8 *)q - (u8 *)p;
866         iov->iov_len += offset;
867         snd_buf->len += offset;
868         return 0;
869 }
870
871 static int
872 gss_wrap_req(struct rpc_task *task,
873              kxdrproc_t encode, void *rqstp, u32 *p, void *obj)
874 {
875         struct rpc_cred *cred = task->tk_msg.rpc_cred;
876         struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
877                         gc_base);
878         struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
879         int             status = -EIO;
880         u32             service;
881
882         dprintk("RPC: %4u gss_wrap_req\n", task->tk_pid);
883         if (ctx->gc_proc != RPC_GSS_PROC_DATA) {
884                 /* The spec seems a little ambiguous here, but I think that not
885                  * wrapping context destruction requests makes the most sense.
886                  */
887                 status = encode(rqstp, p, obj);
888                 goto out;
889         }
890         service = gss_pseudoflavor_to_service(ctx->gc_gss_ctx->mech_type,
891                                                 gss_cred->gc_flavor);
892         switch (service) {
893                 case RPC_GSS_SVC_NONE:
894                         status = encode(rqstp, p, obj);
895                         goto out;
896                 case RPC_GSS_SVC_INTEGRITY:
897                         status = gss_wrap_req_integ(ctx, encode, rqstp, p, obj);
898                         goto out;
899                 case RPC_GSS_SVC_PRIVACY:
900                 default:
901                         goto out;
902         }
903 out:
904         gss_put_ctx(ctx);
905         dprintk("RPC: %4u gss_wrap_req returning %d\n", task->tk_pid, status);
906         return status;
907 }
908
909 static inline int
910 gss_unwrap_resp_integ(struct gss_cl_ctx *ctx,
911                 kxdrproc_t decode, void *rqstp, u32 **p, void *obj)
912 {
913         struct rpc_rqst *req = (struct rpc_rqst *)rqstp;
914         struct xdr_buf  *rcv_buf = &req->rq_rcv_buf;
915         struct xdr_buf integ_buf;
916         struct xdr_netobj mic;
917         u32 data_offset, mic_offset;
918         u32 integ_len;
919         u32 maj_stat;
920         int status = -EIO;
921
922         integ_len = ntohl(*(*p)++);
923         if (integ_len & 3)
924                 return status;
925         data_offset = (u8 *)(*p) - (u8 *)rcv_buf->head[0].iov_base;
926         mic_offset = integ_len + data_offset;
927         if (mic_offset > rcv_buf->len)
928                 return status;
929         if (ntohl(*(*p)++) != req->rq_seqno)
930                 return status;
931
932         if (xdr_buf_subsegment(rcv_buf, &integ_buf, data_offset,
933                                 mic_offset - data_offset))
934                 return status;
935
936         if (xdr_buf_read_netobj(rcv_buf, &mic, mic_offset))
937                 return status;
938
939         maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &integ_buf,
940                         &mic, NULL);
941         if (maj_stat != GSS_S_COMPLETE)
942                 return status;
943         return 0;
944 }
945
946 static int
947 gss_unwrap_resp(struct rpc_task *task,
948                 kxdrproc_t decode, void *rqstp, u32 *p, void *obj)
949 {
950         struct rpc_cred *cred = task->tk_msg.rpc_cred;
951         struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
952                         gc_base);
953         struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
954         int             status = -EIO;
955         u32             service;
956
957         if (ctx->gc_proc != RPC_GSS_PROC_DATA)
958                 goto out_decode;
959         service = gss_pseudoflavor_to_service(ctx->gc_gss_ctx->mech_type,
960                                                 gss_cred->gc_flavor);
961         switch (service) {
962                 case RPC_GSS_SVC_NONE:
963                         goto out_decode;
964                 case RPC_GSS_SVC_INTEGRITY:
965                         status = gss_unwrap_resp_integ(ctx, decode, 
966                                                         rqstp, &p, obj);
967                         if (status)
968                                 goto out;
969                         break;
970                 case RPC_GSS_SVC_PRIVACY:
971                 default:
972                         goto out;
973         }
974 out_decode:
975         status = decode(rqstp, p, obj);
976 out:
977         gss_put_ctx(ctx);
978         dprintk("RPC: %4u gss_unwrap_resp returning %d\n", task->tk_pid,
979                         status);
980         return status;
981 }
982   
983 static struct rpc_authops authgss_ops = {
984         .owner          = THIS_MODULE,
985         .au_flavor      = RPC_AUTH_GSS,
986 #ifdef RPC_DEBUG
987         .au_name        = "RPCSEC_GSS",
988 #endif
989         .create         = gss_create,
990         .destroy        = gss_destroy,
991         .crcreate       = gss_create_cred
992 };
993
994 static struct rpc_credops gss_credops = {
995         .crdestroy      = gss_destroy_cred,
996         .crmatch        = gss_match,
997         .crmarshal      = gss_marshal,
998         .crrefresh      = gss_refresh,
999         .crvalidate     = gss_validate,
1000         .crwrap_req     = gss_wrap_req,
1001         .crunwrap_resp  = gss_unwrap_resp,
1002 };
1003
1004 static struct rpc_pipe_ops gss_upcall_ops = {
1005         .upcall         = gss_pipe_upcall,
1006         .downcall       = gss_pipe_downcall,
1007         .destroy_msg    = gss_pipe_destroy_msg,
1008         .release_pipe   = gss_pipe_release,
1009 };
1010
1011 /*
1012  * Initialize RPCSEC_GSS module
1013  */
1014 static int __init init_rpcsec_gss(void)
1015 {
1016         int err = 0;
1017
1018         err = rpcauth_register(&authgss_ops);
1019         if (err)
1020                 goto out;
1021         err = gss_svc_init();
1022         if (err)
1023                 goto out_unregister;
1024         return 0;
1025 out_unregister:
1026         rpcauth_unregister(&authgss_ops);
1027 out:
1028         return err;
1029 }
1030
1031 static void __exit exit_rpcsec_gss(void)
1032 {
1033         gss_svc_shutdown();
1034         rpcauth_unregister(&authgss_ops);
1035 }
1036
1037 MODULE_LICENSE("GPL");
1038 module_init(init_rpcsec_gss)
1039 module_exit(exit_rpcsec_gss)