2 * linux/net/sunrpc/auth_gss.c
4 * RPCSEC_GSS client authentication.
6 * Copyright (c) 2000 The Regents of the University of Michigan.
9 * Dug Song <dugsong@monkey.org>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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.
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>
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>
58 static struct rpc_authops authgss_ops;
60 static struct rpc_credops gss_credops;
63 # define RPCDBG_FACILITY RPCDBG_AUTH
66 #define NFS_NGROUPS 16
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
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 */
78 #define CA_RUN_AS_MACHINE 0x00000200
80 /* dump the buffer in `emacs-hexl' style */
81 #define isprint(c) ((c > 0x1f) && (c < 0x7f))
83 static DEFINE_RWLOCK(gss_ctx_lock);
86 struct rpc_auth rpc_auth;
87 struct gss_api_mech *mech;
88 struct list_head upcalls;
89 struct dentry *dentry;
94 static void gss_destroy_ctx(struct gss_cl_ctx *);
95 static struct rpc_pipe_ops gss_upcall_ops;
98 print_hexl(u32 *p, u_int length, u_int offset)
103 dprintk("RPC: print_hexl: length %d\n",length);
107 for (i = 0; i < length; i += 0x10) {
108 dprintk(" %04x: ", (u_int)(i + offset));
110 jm = jm > 16 ? 16 : jm;
112 for (j = 0; j < jm; j++) {
114 dprintk("%02x ", (u_int)cp[i+j]);
116 dprintk("%02x", (u_int)cp[i+j]);
118 for (; j < 16; j++) {
126 for (j = 0; j < jm; j++) {
128 c = isprint(c) ? c : '.';
135 EXPORT_SYMBOL(print_hexl);
137 static inline struct gss_cl_ctx *
138 gss_get_ctx(struct gss_cl_ctx *ctx)
140 atomic_inc(&ctx->count);
145 gss_put_ctx(struct gss_cl_ctx *ctx)
147 if (atomic_dec_and_test(&ctx->count))
148 gss_destroy_ctx(ctx);
152 gss_cred_set_ctx(struct rpc_cred *cred, struct gss_cl_ctx *ctx)
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);
166 gss_cred_is_uptodate_ctx(struct rpc_cred *cred)
168 struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
171 read_lock(&gss_ctx_lock);
172 if ((cred->cr_flags & RPCAUTH_CRED_UPTODATE) && gss_cred->gc_ctx)
174 read_unlock(&gss_ctx_lock);
179 simple_get_bytes(char **ptr, const char *end, void *res, int len)
184 if (q > end || q < p)
192 simple_get_netobj(char **ptr, const char *end, struct xdr_netobj *res)
196 if (simple_get_bytes(&p, end, &res->len, sizeof(res->len)))
199 if (q > end || q < p)
207 dup_netobj(struct xdr_netobj *source, struct xdr_netobj *dest)
209 dest->len = source->len;
210 if (!(dest->data = kmalloc(dest->len, GFP_KERNEL)))
212 memcpy(dest->data, source->data, dest->len);
216 static struct gss_cl_ctx *
217 gss_cred_get_ctx(struct rpc_cred *cred)
219 struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
220 struct gss_cl_ctx *ctx = NULL;
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);
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)
233 char *end = buf->data + buf->len;
235 struct gss_cl_ctx *ctx;
236 struct xdr_netobj tmp_buf;
237 unsigned int timeout;
240 if (!(ctx = kmalloc(sizeof(*ctx), GFP_KERNEL))) {
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);
249 if (simple_get_bytes(&p, end, uid, sizeof(*uid)))
251 /* FIXME: discarded timeout for now */
252 if (simple_get_bytes(&p, end, &timeout, sizeof(timeout)))
255 if (simple_get_bytes(&p, end, &ctx->gc_win, sizeof(ctx->gc_win)))
257 /* gssd signals an error by passing ctx->gc_win = 0: */
259 /* in which case the next int is an error code: */
260 if (simple_get_bytes(&p, end, gss_err, sizeof(*gss_err)))
265 if (simple_get_netobj(&p, end, &tmp_buf))
267 if (dup_netobj(&tmp_buf, &ctx->gc_wire_ctx)) {
271 if (simple_get_netobj(&p, end, &tmp_buf))
272 goto err_free_wire_ctx;
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;
280 kfree(ctx->gc_wire_ctx.data);
285 dprintk("RPC: gss_parse_init_downcall returning %d\n", err);
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;
300 gss_release_msg(struct gss_upcall_msg *gss_msg)
302 if (!atomic_dec_and_test(&gss_msg->count))
304 BUG_ON(!list_empty(&gss_msg->list));
308 static struct gss_upcall_msg *
309 __gss_find_upcall(struct gss_auth *gss_auth, uid_t uid)
311 struct gss_upcall_msg *pos;
312 list_for_each_entry(pos, &gss_auth->upcalls, list) {
315 atomic_inc(&pos->count);
316 dprintk("RPC: gss_find_upcall found msg %p\n", pos);
319 dprintk("RPC: gss_find_upcall found nothing\n");
324 __gss_unhash_msg(struct gss_upcall_msg *gss_msg)
326 if (list_empty(&gss_msg->list))
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);
332 rpc_wake_up(&gss_msg->waitq);
333 atomic_dec(&gss_msg->count);
337 gss_unhash_msg(struct gss_upcall_msg *gss_msg)
339 struct gss_auth *gss_auth = gss_msg->auth;
341 spin_lock(&gss_auth->lock);
342 __gss_unhash_msg(gss_msg);
343 spin_unlock(&gss_auth->lock);
347 gss_upcall(struct rpc_clnt *clnt, struct rpc_task *task, struct rpc_cred *cred)
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;
357 dprintk("RPC: %4u gss_upcall for uid %u\n", task->tk_pid, uid);
360 spin_lock(&gss_auth->lock);
361 gss_msg = __gss_find_upcall(gss_auth, uid);
364 if (gss_new == NULL) {
365 spin_unlock(&gss_auth->lock);
366 gss_new = kmalloc(sizeof(*gss_new), GFP_KERNEL);
368 dprintk("RPC: %4u gss_upcall -ENOMEM\n", task->tk_pid);
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);
379 msg->data = &gss_new->uid;
380 msg->len = sizeof(gss_new->uid);
382 gss_new->auth = gss_auth;
383 list_add(&gss_new->list, &gss_auth->upcalls);
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);
393 gss_unhash_msg(gss_msg);
395 /* Yes, so cancel upcall */
396 __gss_unhash_msg(gss_msg);
397 spin_unlock(&gss_auth->lock);
399 gss_release_msg(gss_msg);
400 dprintk("RPC: %4u gss_upcall for uid %u result %d\n", task->tk_pid,
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);
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.
414 gss_release_msg(gss_msg);
419 gss_pipe_upcall(struct file *filp, struct rpc_pipe_msg *msg,
420 char __user *dst, size_t buflen)
422 char *data = (char *)msg->data + msg->copied;
423 ssize_t mlen = msg->len;
428 left = copy_to_user(dst, data, mlen);
439 #define MSG_BUF_MAXSIZE 1024
442 gss_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
444 struct xdr_netobj obj = {
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;
461 if (mlen > MSG_BUF_MAXSIZE)
463 obj.data = kmalloc(mlen, GFP_KERNEL);
466 left = copy_from_user(obj.data, src, mlen);
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);
479 cred = rpcauth_lookup_credcache(auth, &acred, 0);
483 cred->cr_flags &= ~RPCAUTH_CRED_UPTODATE;
485 gss_cred_set_ctx(cred, ctx);
486 spin_lock(&gss_auth->lock);
487 gss_msg = __gss_find_upcall(gss_auth, acred.uid);
490 gss_msg->msg.errno = -EACCES;
491 __gss_unhash_msg(gss_msg);
492 spin_unlock(&gss_auth->lock);
493 gss_release_msg(gss_msg);
495 spin_unlock(&gss_auth->lock);
496 rpc_release_client(clnt);
498 dprintk("RPC: gss_pipe_downcall returning length %Zu\n", mlen);
502 gss_destroy_ctx(ctx);
503 rpc_release_client(clnt);
506 dprintk("RPC: gss_pipe_downcall returning %d\n", err);
511 gss_pipe_release(struct inode *inode)
513 struct rpc_inode *rpci = RPC_I(inode);
514 struct rpc_clnt *clnt;
515 struct rpc_auth *auth;
516 struct gss_auth *gss_auth;
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;
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);
534 spin_unlock(&gss_auth->lock);
538 gss_pipe_destroy_msg(struct rpc_pipe_msg *msg)
540 struct gss_upcall_msg *gss_msg = container_of(msg, struct gss_upcall_msg, msg);
541 static unsigned long ratelimit;
543 if (msg->errno < 0) {
544 dprintk("RPC: gss_pipe_destroy_msg releasing msg %p\n",
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;
556 gss_release_msg(gss_msg);
561 * NOTE: we have the opportunity to use different
562 * parameters based on the input flavor (which must be a pseudoflavor)
564 static struct rpc_auth *
565 gss_create(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
567 struct gss_auth *gss_auth;
568 struct rpc_auth * auth;
570 dprintk("RPC: creating GSS authenticator for client %p\n",clnt);
572 if (!(gss_auth = kmalloc(sizeof(*gss_auth), GFP_KERNEL)))
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);
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;
589 rpcauth_init_credcache(auth);
591 snprintf(gss_auth->path, sizeof(gss_auth->path), "%s/%s",
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))
600 gss_mech_put(gss_auth->mech);
608 gss_destroy(struct rpc_auth *auth)
610 struct gss_auth *gss_auth;
612 dprintk("RPC: destroying GSS authenticator %p flavor %d\n",
613 auth, auth->au_flavor);
615 gss_auth = container_of(auth, struct gss_auth, rpc_auth);
616 rpc_unlink(gss_auth->path);
617 gss_mech_put(gss_auth->mech);
619 rpcauth_free_credcache(auth);
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. */
626 gss_destroy_ctx(struct gss_cl_ctx *ctx)
628 dprintk("RPC: gss_destroy_ctx\n");
631 gss_delete_sec_context(&ctx->gc_gss_ctx);
633 if (ctx->gc_wire_ctx.len > 0) {
634 kfree(ctx->gc_wire_ctx.data);
635 ctx->gc_wire_ctx.len = 0;
643 gss_destroy_cred(struct rpc_cred *rc)
645 struct gss_cred *cred = (struct gss_cred *)rc;
647 dprintk("RPC: gss_destroy_cred \n");
650 gss_put_ctx(cred->gc_ctx);
654 static struct rpc_cred *
655 gss_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int taskflags)
657 struct gss_cred *cred = NULL;
659 dprintk("RPC: gss_create_cred for uid %d, flavor %d\n",
660 acred->uid, auth->au_flavor);
662 if (!(cred = kmalloc(sizeof(*cred), GFP_KERNEL)))
665 memset(cred, 0, sizeof(*cred));
666 atomic_set(&cred->gc_count, 0);
667 cred->gc_uid = acred->uid;
669 * Note: in order to force a call to call_refresh(), we deliberately
670 * fail to flag the credential as RPCAUTH_CRED_UPTODATE.
673 cred->gc_base.cr_ops = &gss_credops;
674 cred->gc_flavor = auth->au_flavor;
676 return (struct rpc_cred *) cred;
679 dprintk("RPC: gss_create_cred failed\n");
680 if (cred) gss_destroy_cred((struct rpc_cred *)cred);
685 gss_match(struct auth_cred *acred, struct rpc_cred *rc, int taskflags)
687 return (rc->cr_uid == acred->uid);
691 * Marshal credentials.
692 * Maybe we should keep a cached credential for performance reasons.
695 gss_marshal(struct rpc_task *task, u32 *p, int ruid)
697 struct rpc_cred *cred = task->tk_msg.rpc_cred;
698 struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
700 struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
702 struct rpc_rqst *req = task->tk_rqstp;
704 struct xdr_netobj mic;
706 struct xdr_buf verf_buf;
709 dprintk("RPC: %4u gss_marshal\n", task->tk_pid);
711 *p++ = htonl(RPC_AUTH_GSS);
714 service = gss_pseudoflavor_to_service(ctx->gc_gss_ctx->mech_type,
715 gss_cred->gc_flavor);
717 dprintk("RPC: %4u Bad pseudoflavor %d in gss_marshal\n",
718 task->tk_pid, gss_cred->gc_flavor);
721 spin_lock(&ctx->gc_seq_lock);
722 req->rq_seqno = ctx->gc_seq++;
723 spin_unlock(&ctx->gc_seq_lock);
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);
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() */
738 iov.iov_len = (u8 *)p - (u8 *)iov.iov_base;
739 xdr_buf_from_iov(&iov, &verf_buf);
741 /* set verifier flavor*/
742 *p++ = htonl(RPC_AUTH_GSS);
744 mic.data = (u8 *)(p + 1);
745 maj_stat = gss_get_mic(ctx->gc_gss_ctx,
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);
754 p = xdr_encode_opaque(p, NULL, mic.len);
763 * Refresh credentials. XXX - finish
766 gss_refresh(struct rpc_task *task)
768 struct rpc_clnt *clnt = task->tk_client;
769 struct rpc_cred *cred = task->tk_msg.rpc_cred;
771 if (!gss_cred_is_uptodate_ctx(cred))
772 return gss_upcall(clnt, task, cred);
777 gss_validate(struct rpc_task *task, u32 *p)
779 struct rpc_cred *cred = task->tk_msg.rpc_cred;
780 struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
782 struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
785 struct xdr_buf verf_buf;
786 struct xdr_netobj mic;
791 dprintk("RPC: %4u gss_validate\n", task->tk_pid);
794 if ((len = ntohl(*p++)) > RPC_MAX_AUTH_SIZE)
796 if (flav != RPC_AUTH_GSS)
798 seq = htonl(task->tk_rqstp->rq_seqno);
800 iov.iov_len = sizeof(seq);
801 xdr_buf_from_iov(&iov, &verf_buf);
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;
810 service = gss_pseudoflavor_to_service(ctx->gc_gss_ctx->mech_type,
811 gss_cred->gc_flavor);
813 case RPC_GSS_SVC_NONE:
814 /* verifier data, flavor, length: */
815 task->tk_auth->au_rslack = XDR_QUADLEN(len) + 2;
817 case RPC_GSS_SVC_INTEGRITY:
818 /* verifier data, flavor, length, length, sequence number: */
819 task->tk_auth->au_rslack = XDR_QUADLEN(len) + 4;
825 dprintk("RPC: %4u GSS gss_validate: gss_verify_mic succeeded.\n",
827 return p + XDR_QUADLEN(len);
830 dprintk("RPC: %4u gss_validate failed.\n", task->tk_pid);
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)
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;
848 offset = (u8 *)p - (u8 *)snd_buf->head[0].iov_base;
849 *p++ = htonl(rqstp->rq_seqno);
851 status = encode(rqstp, p, obj);
855 if (xdr_buf_subsegment(snd_buf, &integ_buf,
856 offset, snd_buf->len - offset))
858 *integ_len = htonl(integ_buf.len);
860 /* guess whether we're in the head or the tail: */
861 if (snd_buf->page_len || snd_buf->tail[0].iov_len)
865 p = iov->iov_base + iov->iov_len;
866 mic.data = (u8 *)(p + 1);
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;
875 q = xdr_encode_opaque(p, NULL, mic.len);
877 offset = (u8 *)q - (u8 *)p;
878 iov->iov_len += offset;
879 snd_buf->len += offset;
884 gss_wrap_req(struct rpc_task *task,
885 kxdrproc_t encode, void *rqstp, u32 *p, void *obj)
887 struct rpc_cred *cred = task->tk_msg.rpc_cred;
888 struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
890 struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
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.
899 status = encode(rqstp, p, obj);
902 service = gss_pseudoflavor_to_service(ctx->gc_gss_ctx->mech_type,
903 gss_cred->gc_flavor);
905 case RPC_GSS_SVC_NONE:
906 status = encode(rqstp, p, obj);
908 case RPC_GSS_SVC_INTEGRITY:
909 status = gss_wrap_req_integ(cred, ctx, encode,
912 case RPC_GSS_SVC_PRIVACY:
918 dprintk("RPC: %4u gss_wrap_req returning %d\n", task->tk_pid, status);
923 gss_unwrap_resp_integ(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
924 struct rpc_rqst *rqstp, u32 **p)
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;
934 integ_len = ntohl(*(*p)++);
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)
941 if (ntohl(*(*p)++) != rqstp->rq_seqno)
944 if (xdr_buf_subsegment(rcv_buf, &integ_buf, data_offset,
945 mic_offset - data_offset))
948 if (xdr_buf_read_netobj(rcv_buf, &mic, mic_offset))
951 maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &integ_buf,
953 if (maj_stat == GSS_S_CONTEXT_EXPIRED)
954 cred->cr_flags &= ~RPCAUTH_CRED_UPTODATE;
955 if (maj_stat != GSS_S_COMPLETE)
961 gss_unwrap_resp(struct rpc_task *task,
962 kxdrproc_t decode, void *rqstp, u32 *p, void *obj)
964 struct rpc_cred *cred = task->tk_msg.rpc_cred;
965 struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
967 struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
971 if (ctx->gc_proc != RPC_GSS_PROC_DATA)
973 service = gss_pseudoflavor_to_service(ctx->gc_gss_ctx->mech_type,
974 gss_cred->gc_flavor);
976 case RPC_GSS_SVC_NONE:
978 case RPC_GSS_SVC_INTEGRITY:
979 status = gss_unwrap_resp_integ(cred, ctx, rqstp, &p);
983 case RPC_GSS_SVC_PRIVACY:
988 status = decode(rqstp, p, obj);
991 dprintk("RPC: %4u gss_unwrap_resp returning %d\n", task->tk_pid,
996 static struct rpc_authops authgss_ops = {
997 .owner = THIS_MODULE,
998 .au_flavor = RPC_AUTH_GSS,
1000 .au_name = "RPCSEC_GSS",
1002 .create = gss_create,
1003 .destroy = gss_destroy,
1004 .crcreate = gss_create_cred
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,
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,
1025 * Initialize RPCSEC_GSS module
1027 static int __init init_rpcsec_gss(void)
1031 err = rpcauth_register(&authgss_ops);
1034 err = gss_svc_init();
1036 goto out_unregister;
1039 rpcauth_unregister(&authgss_ops);
1044 static void __exit exit_rpcsec_gss(void)
1047 rpcauth_unregister(&authgss_ops);
1050 MODULE_LICENSE("GPL");
1051 module_init(init_rpcsec_gss)
1052 module_exit(exit_rpcsec_gss)