2 * Neil Brown <neilb@cse.unsw.edu.au>
3 * J. Bruce Fields <bfields@umich.edu>
4 * Andy Adamson <andros@umich.edu>
5 * Dug Song <dugsong@monkey.org>
7 * RPCSEC_GSS server authentication.
8 * This implements RPCSEC_GSS as defined in rfc2203 (rpcsec_gss) and rfc2078
11 * The RPCSEC_GSS involves three stages:
14 * 3/ context destruction
16 * Context creation is handled largely by upcalls to user-space.
17 * In particular, GSS_Accept_sec_context is handled by an upcall
18 * Data exchange is handled entirely within the kernel
19 * In particular, GSS_GetMIC, GSS_VerifyMIC, GSS_Seal, GSS_Unseal are in-kernel.
20 * Context destruction is handled in-kernel
21 * GSS_Delete_sec_context is in-kernel
23 * Context creation is initiated by a RPCSEC_GSS_INIT request arriving.
24 * The context handle and gss_token are used as a key into the rpcsec_init cache.
25 * The content of this cache includes some of the outputs of GSS_Accept_sec_context,
26 * being major_status, minor_status, context_handle, reply_token.
27 * These are sent back to the client.
28 * Sequence window management is handled by the kernel. The window size if currently
29 * a compile time constant.
31 * When user-space is happy that a context is established, it places an entry
32 * in the rpcsec_context cache. The key for this cache is the context_handle.
33 * The content includes:
34 * uid/gidlist - for determining access rights
36 * mechanism specific information, such as a key
40 #include <linux/types.h>
41 #include <linux/module.h>
42 #include <linux/pagemap.h>
44 #include <linux/sunrpc/auth_gss.h>
45 #include <linux/sunrpc/svcauth.h>
46 #include <linux/sunrpc/gss_err.h>
47 #include <linux/sunrpc/svcauth.h>
48 #include <linux/sunrpc/svcauth_gss.h>
49 #include <linux/sunrpc/cache.h>
52 # define RPCDBG_FACILITY RPCDBG_AUTH
55 /* The rpcsec_init cache is used for mapping RPCSEC_GSS_{,CONT_}INIT requests
58 * Key is context handle (\x if empty) and gss_token.
59 * Content is major_status minor_status (integers) context_handle, reply_token.
63 static int netobj_equal(struct xdr_netobj *a, struct xdr_netobj *b)
65 return a->len == b->len && 0 == memcmp(a->data, b->data, a->len);
68 #define RSI_HASHBITS 6
69 #define RSI_HASHMAX (1<<RSI_HASHBITS)
70 #define RSI_HASHMASK (RSI_HASHMAX-1)
74 struct xdr_netobj in_handle, in_token;
75 struct xdr_netobj out_handle, out_token;
76 int major_status, minor_status;
79 static struct cache_head *rsi_table[RSI_HASHMAX];
80 static struct cache_detail rsi_cache;
81 static struct rsi *rsi_update(struct rsi *new, struct rsi *old);
82 static struct rsi *rsi_lookup(struct rsi *item);
84 static void rsi_free(struct rsi *rsii)
86 kfree(rsii->in_handle.data);
87 kfree(rsii->in_token.data);
88 kfree(rsii->out_handle.data);
89 kfree(rsii->out_token.data);
92 static void rsi_put(struct cache_head *item, struct cache_detail *cd)
94 struct rsi *rsii = container_of(item, struct rsi, h);
95 if (cache_put(item, cd)) {
101 static inline int rsi_hash(struct rsi *item)
103 return hash_mem(item->in_handle.data, item->in_handle.len, RSI_HASHBITS)
104 ^ hash_mem(item->in_token.data, item->in_token.len, RSI_HASHBITS);
107 static int rsi_match(struct cache_head *a, struct cache_head *b)
109 struct rsi *item = container_of(a, struct rsi, h);
110 struct rsi *tmp = container_of(b, struct rsi, h);
111 return netobj_equal(&item->in_handle, &tmp->in_handle)
112 && netobj_equal(&item->in_token, &tmp->in_token);
115 static int dup_to_netobj(struct xdr_netobj *dst, char *src, int len)
118 dst->data = (len ? kmalloc(len, GFP_KERNEL) : NULL);
120 memcpy(dst->data, src, len);
121 if (len && !dst->data)
126 static inline int dup_netobj(struct xdr_netobj *dst, struct xdr_netobj *src)
128 return dup_to_netobj(dst, src->data, src->len);
131 static void rsi_init(struct cache_head *cnew, struct cache_head *citem)
133 struct rsi *new = container_of(cnew, struct rsi, h);
134 struct rsi *item = container_of(citem, struct rsi, h);
136 new->out_handle.data = NULL;
137 new->out_handle.len = 0;
138 new->out_token.data = NULL;
139 new->out_token.len = 0;
140 new->in_handle.len = item->in_handle.len;
141 item->in_handle.len = 0;
142 new->in_token.len = item->in_token.len;
143 item->in_token.len = 0;
144 new->in_handle.data = item->in_handle.data;
145 item->in_handle.data = NULL;
146 new->in_token.data = item->in_token.data;
147 item->in_token.data = NULL;
150 static void update_rsi(struct cache_head *cnew, struct cache_head *citem)
152 struct rsi *new = container_of(cnew, struct rsi, h);
153 struct rsi *item = container_of(citem, struct rsi, h);
155 BUG_ON(new->out_handle.data || new->out_token.data);
156 new->out_handle.len = item->out_handle.len;
157 item->out_handle.len = 0;
158 new->out_token.len = item->out_token.len;
159 item->out_token.len = 0;
160 new->out_handle.data = item->out_handle.data;
161 item->out_handle.data = NULL;
162 new->out_token.data = item->out_token.data;
163 item->out_token.data = NULL;
165 new->major_status = item->major_status;
166 new->minor_status = item->minor_status;
169 static struct cache_head *rsi_alloc(void)
171 struct rsi *rsii = kmalloc(sizeof(*rsii), GFP_KERNEL);
178 static void rsi_request(struct cache_detail *cd,
179 struct cache_head *h,
180 char **bpp, int *blen)
182 struct rsi *rsii = container_of(h, struct rsi, h);
184 qword_addhex(bpp, blen, rsii->in_handle.data, rsii->in_handle.len);
185 qword_addhex(bpp, blen, rsii->in_token.data, rsii->in_token.len);
190 static int rsi_parse(struct cache_detail *cd,
191 char *mesg, int mlen)
193 /* context token expiry major minor context token */
197 struct rsi rsii, *rsip = NULL;
199 int status = -EINVAL;
201 memset(&rsii, 0, sizeof(rsii));
203 len = qword_get(&mesg, buf, mlen);
207 if (dup_to_netobj(&rsii.in_handle, buf, len))
211 len = qword_get(&mesg, buf, mlen);
216 if (dup_to_netobj(&rsii.in_token, buf, len))
219 rsip = rsi_lookup(&rsii);
225 expiry = get_expiry(&mesg);
231 len = qword_get(&mesg, buf, mlen);
237 rsii.major_status = simple_strtoul(buf, &ep, 10);
240 len = qword_get(&mesg, buf, mlen);
243 rsii.minor_status = simple_strtoul(buf, &ep, 10);
248 len = qword_get(&mesg, buf, mlen);
252 if (dup_to_netobj(&rsii.out_handle, buf, len))
256 len = qword_get(&mesg, buf, mlen);
261 if (dup_to_netobj(&rsii.out_token, buf, len))
264 rsii.h.expiry_time = expiry;
265 rsip = rsi_update(&rsii, rsip);
270 rsi_put(&rsip->h, &rsi_cache);
276 static struct cache_detail rsi_cache = {
277 .owner = THIS_MODULE,
278 .hash_size = RSI_HASHMAX,
279 .hash_table = rsi_table,
280 .name = "auth.rpcsec.init",
281 .cache_put = rsi_put,
282 .cache_request = rsi_request,
283 .cache_parse = rsi_parse,
286 .update = update_rsi,
290 static struct rsi *rsi_lookup(struct rsi *item)
292 struct cache_head *ch;
293 int hash = rsi_hash(item);
295 ch = sunrpc_cache_lookup(&rsi_cache, &item->h, hash);
297 return container_of(ch, struct rsi, h);
302 static struct rsi *rsi_update(struct rsi *new, struct rsi *old)
304 struct cache_head *ch;
305 int hash = rsi_hash(new);
307 ch = sunrpc_cache_update(&rsi_cache, &new->h,
310 return container_of(ch, struct rsi, h);
317 * The rpcsec_context cache is used to store a context that is
318 * used in data exchange.
319 * The key is a context handle. The content is:
320 * uid, gidlist, mechanism, service-set, mech-specific-data
323 #define RSC_HASHBITS 10
324 #define RSC_HASHMAX (1<<RSC_HASHBITS)
325 #define RSC_HASHMASK (RSC_HASHMAX-1)
327 #define GSS_SEQ_WIN 128
329 struct gss_svc_seq_data {
330 /* highest seq number seen so far: */
332 /* for i such that sd_max-GSS_SEQ_WIN < i <= sd_max, the i-th bit of
333 * sd_win is nonzero iff sequence number i has been seen already: */
334 unsigned long sd_win[GSS_SEQ_WIN/BITS_PER_LONG];
340 struct xdr_netobj handle;
341 struct svc_cred cred;
342 struct gss_svc_seq_data seqdata;
343 struct gss_ctx *mechctx;
346 static struct cache_head *rsc_table[RSC_HASHMAX];
347 static struct cache_detail rsc_cache;
348 static struct rsc *rsc_update(struct rsc *new, struct rsc *old);
349 static struct rsc *rsc_lookup(struct rsc *item);
351 static void rsc_free(struct rsc *rsci)
353 kfree(rsci->handle.data);
355 gss_delete_sec_context(&rsci->mechctx);
356 if (rsci->cred.cr_group_info)
357 put_group_info(rsci->cred.cr_group_info);
360 static void rsc_put(struct cache_head *item, struct cache_detail *cd)
362 struct rsc *rsci = container_of(item, struct rsc, h);
364 if (cache_put(item, cd)) {
371 rsc_hash(struct rsc *rsci)
373 return hash_mem(rsci->handle.data, rsci->handle.len, RSC_HASHBITS);
377 rsc_match(struct cache_head *a, struct cache_head *b)
379 struct rsc *new = container_of(a, struct rsc, h);
380 struct rsc *tmp = container_of(b, struct rsc, h);
382 return netobj_equal(&new->handle, &tmp->handle);
386 rsc_init(struct cache_head *cnew, struct cache_head *ctmp)
388 struct rsc *new = container_of(cnew, struct rsc, h);
389 struct rsc *tmp = container_of(ctmp, struct rsc, h);
391 new->handle.len = tmp->handle.len;
393 new->handle.data = tmp->handle.data;
394 tmp->handle.data = NULL;
396 new->cred.cr_group_info = NULL;
400 update_rsc(struct cache_head *cnew, struct cache_head *ctmp)
402 struct rsc *new = container_of(cnew, struct rsc, h);
403 struct rsc *tmp = container_of(ctmp, struct rsc, h);
405 new->mechctx = tmp->mechctx;
407 memset(&new->seqdata, 0, sizeof(new->seqdata));
408 spin_lock_init(&new->seqdata.sd_lock);
409 new->cred = tmp->cred;
410 tmp->cred.cr_group_info = NULL;
413 static struct cache_head *
416 struct rsc *rsci = kmalloc(sizeof(*rsci), GFP_KERNEL);
423 static int rsc_parse(struct cache_detail *cd,
424 char *mesg, int mlen)
426 /* contexthandle expiry [ uid gid N <n gids> mechname ...mechdata... ] */
429 struct rsc rsci, *rscp = NULL;
431 int status = -EINVAL;
433 memset(&rsci, 0, sizeof(rsci));
435 len = qword_get(&mesg, buf, mlen);
436 if (len < 0) goto out;
438 if (dup_to_netobj(&rsci.handle, buf, len))
443 expiry = get_expiry(&mesg);
448 rscp = rsc_lookup(&rsci);
452 /* uid, or NEGATIVE */
453 rv = get_int(&mesg, &rsci.cred.cr_uid);
457 set_bit(CACHE_NEGATIVE, &rsci.h.flags);
460 struct gss_api_mech *gm;
463 if (get_int(&mesg, &rsci.cred.cr_gid))
466 /* number of additional gid's */
467 if (get_int(&mesg, &N))
470 rsci.cred.cr_group_info = groups_alloc(N);
471 if (rsci.cred.cr_group_info == NULL)
476 for (i=0; i<N; i++) {
478 if (get_int(&mesg, &gid))
480 GROUP_AT(rsci.cred.cr_group_info, i) = gid;
484 len = qword_get(&mesg, buf, mlen);
487 gm = gss_mech_get_by_name(buf);
488 status = -EOPNOTSUPP;
493 /* mech-specific data: */
494 len = qword_get(&mesg, buf, mlen);
499 status = gss_import_sec_context(buf, len, gm, &rsci.mechctx);
506 rsci.h.expiry_time = expiry;
507 rscp = rsc_update(&rsci, rscp);
512 rsc_put(&rscp->h, &rsc_cache);
518 static struct cache_detail rsc_cache = {
519 .owner = THIS_MODULE,
520 .hash_size = RSC_HASHMAX,
521 .hash_table = rsc_table,
522 .name = "auth.rpcsec.context",
523 .cache_put = rsc_put,
524 .cache_parse = rsc_parse,
527 .update = update_rsc,
531 static struct rsc *rsc_lookup(struct rsc *item)
533 struct cache_head *ch;
534 int hash = rsc_hash(item);
536 ch = sunrpc_cache_lookup(&rsc_cache, &item->h, hash);
538 return container_of(ch, struct rsc, h);
543 static struct rsc *rsc_update(struct rsc *new, struct rsc *old)
545 struct cache_head *ch;
546 int hash = rsc_hash(new);
548 ch = sunrpc_cache_update(&rsc_cache, &new->h,
551 return container_of(ch, struct rsc, h);
558 gss_svc_searchbyctx(struct xdr_netobj *handle)
563 memset(&rsci, 0, sizeof(rsci));
564 if (dup_to_netobj(&rsci.handle, handle->data, handle->len))
566 found = rsc_lookup(&rsci);
570 if (cache_check(&rsc_cache, &found->h, NULL))
575 /* Implements sequence number algorithm as specified in RFC 2203. */
577 gss_check_seq_num(struct rsc *rsci, int seq_num)
579 struct gss_svc_seq_data *sd = &rsci->seqdata;
581 spin_lock(&sd->sd_lock);
582 if (seq_num > sd->sd_max) {
583 if (seq_num >= sd->sd_max + GSS_SEQ_WIN) {
584 memset(sd->sd_win,0,sizeof(sd->sd_win));
585 sd->sd_max = seq_num;
586 } else while (sd->sd_max < seq_num) {
588 __clear_bit(sd->sd_max % GSS_SEQ_WIN, sd->sd_win);
590 __set_bit(seq_num % GSS_SEQ_WIN, sd->sd_win);
592 } else if (seq_num <= sd->sd_max - GSS_SEQ_WIN) {
595 /* sd_max - GSS_SEQ_WIN < seq_num <= sd_max */
596 if (__test_and_set_bit(seq_num % GSS_SEQ_WIN, sd->sd_win))
599 spin_unlock(&sd->sd_lock);
602 spin_unlock(&sd->sd_lock);
606 static inline u32 round_up_to_quad(u32 i)
608 return (i + 3 ) & ~3;
612 svc_safe_getnetobj(struct kvec *argv, struct xdr_netobj *o)
616 if (argv->iov_len < 4)
618 o->len = ntohl(svc_getu32(argv));
619 l = round_up_to_quad(o->len);
620 if (argv->iov_len < l)
622 o->data = argv->iov_base;
629 svc_safe_putnetobj(struct kvec *resv, struct xdr_netobj *o)
633 if (resv->iov_len + 4 > PAGE_SIZE)
635 svc_putu32(resv, htonl(o->len));
636 p = resv->iov_base + resv->iov_len;
637 resv->iov_len += round_up_to_quad(o->len);
638 if (resv->iov_len > PAGE_SIZE)
640 memcpy(p, o->data, o->len);
641 memset((u8 *)p + o->len, 0, round_up_to_quad(o->len) - o->len);
645 /* Verify the checksum on the header and return SVC_OK on success.
646 * Otherwise, return SVC_DROP (in the case of a bad sequence number)
647 * or return SVC_DENIED and indicate error in authp.
650 gss_verify_header(struct svc_rqst *rqstp, struct rsc *rsci,
651 u32 *rpcstart, struct rpc_gss_wire_cred *gc, u32 *authp)
653 struct gss_ctx *ctx_id = rsci->mechctx;
654 struct xdr_buf rpchdr;
655 struct xdr_netobj checksum;
657 struct kvec *argv = &rqstp->rq_arg.head[0];
660 /* data to compute the checksum over: */
661 iov.iov_base = rpcstart;
662 iov.iov_len = (u8 *)argv->iov_base - (u8 *)rpcstart;
663 xdr_buf_from_iov(&iov, &rpchdr);
665 *authp = rpc_autherr_badverf;
666 if (argv->iov_len < 4)
668 flavor = ntohl(svc_getu32(argv));
669 if (flavor != RPC_AUTH_GSS)
671 if (svc_safe_getnetobj(argv, &checksum))
674 if (rqstp->rq_deferred) /* skip verification of revisited request */
676 if (gss_verify_mic(ctx_id, &rpchdr, &checksum) != GSS_S_COMPLETE) {
677 *authp = rpcsec_gsserr_credproblem;
681 if (gc->gc_seq > MAXSEQ) {
682 dprintk("RPC: svcauth_gss: discarding request with large sequence number %d\n",
684 *authp = rpcsec_gsserr_ctxproblem;
687 if (!gss_check_seq_num(rsci, gc->gc_seq)) {
688 dprintk("RPC: svcauth_gss: discarding request with old sequence number %d\n",
696 gss_write_null_verf(struct svc_rqst *rqstp)
700 svc_putu32(rqstp->rq_res.head, htonl(RPC_AUTH_NULL));
701 p = rqstp->rq_res.head->iov_base + rqstp->rq_res.head->iov_len;
702 /* don't really need to check if head->iov_len > PAGE_SIZE ... */
704 if (!xdr_ressize_check(rqstp, p))
710 gss_write_verf(struct svc_rqst *rqstp, struct gss_ctx *ctx_id, u32 seq)
714 struct xdr_buf verf_data;
715 struct xdr_netobj mic;
719 svc_putu32(rqstp->rq_res.head, htonl(RPC_AUTH_GSS));
720 xdr_seq = htonl(seq);
722 iov.iov_base = &xdr_seq;
723 iov.iov_len = sizeof(xdr_seq);
724 xdr_buf_from_iov(&iov, &verf_data);
725 p = rqstp->rq_res.head->iov_base + rqstp->rq_res.head->iov_len;
726 mic.data = (u8 *)(p + 1);
727 maj_stat = gss_get_mic(ctx_id, &verf_data, &mic);
728 if (maj_stat != GSS_S_COMPLETE)
730 *p++ = htonl(mic.len);
731 memset((u8 *)p + mic.len, 0, round_up_to_quad(mic.len) - mic.len);
732 p += XDR_QUADLEN(mic.len);
733 if (!xdr_ressize_check(rqstp, p))
739 struct auth_domain h;
743 static struct auth_domain *
744 find_gss_auth_domain(struct gss_ctx *ctx, u32 svc)
748 name = gss_service_to_auth_domain_name(ctx->mech_type, svc);
751 return auth_domain_find(name);
754 static struct auth_ops svcauthops_gss;
757 svcauth_gss_register_pseudoflavor(u32 pseudoflavor, char * name)
759 struct gss_domain *new;
760 struct auth_domain *test;
763 new = kmalloc(sizeof(*new), GFP_KERNEL);
766 kref_init(&new->h.ref);
767 new->h.name = kmalloc(strlen(name) + 1, GFP_KERNEL);
770 strcpy(new->h.name, name);
771 new->h.flavour = &svcauthops_gss;
772 new->pseudoflavor = pseudoflavor;
774 test = auth_domain_lookup(name, &new->h);
775 if (test != &new->h) { /* XXX Duplicate registration? */
776 auth_domain_put(&new->h);
777 /* dangling ref-count... */
788 EXPORT_SYMBOL(svcauth_gss_register_pseudoflavor);
791 read_u32_from_xdr_buf(struct xdr_buf *buf, int base, u32 *obj)
796 status = read_bytes_from_xdr_buf(buf, base, &raw, sizeof(*obj));
803 /* It would be nice if this bit of code could be shared with the client.
805 * The client shouldn't malloc(), would have to pass in own memory.
806 * The server uses base of head iovec as read pointer, while the
807 * client uses separate pointer. */
809 unwrap_integ_data(struct xdr_buf *buf, u32 seq, struct gss_ctx *ctx)
812 u32 integ_len, maj_stat;
813 struct xdr_netobj mic;
814 struct xdr_buf integ_buf;
816 integ_len = ntohl(svc_getu32(&buf->head[0]));
819 if (integ_len > buf->len)
821 if (xdr_buf_subsegment(buf, &integ_buf, 0, integ_len))
823 /* copy out mic... */
824 if (read_u32_from_xdr_buf(buf, integ_len, &mic.len))
826 if (mic.len > RPC_MAX_AUTH_SIZE)
828 mic.data = kmalloc(mic.len, GFP_KERNEL);
831 if (read_bytes_from_xdr_buf(buf, integ_len + 4, mic.data, mic.len))
833 maj_stat = gss_verify_mic(ctx, &integ_buf, &mic);
834 if (maj_stat != GSS_S_COMPLETE)
836 if (ntohl(svc_getu32(&buf->head[0])) != seq)
843 struct gss_svc_data {
844 /* decoded gss client cred: */
845 struct rpc_gss_wire_cred clcred;
846 /* pointer to the beginning of the procedure-specific results,
847 * which may be encrypted/checksummed in svcauth_gss_release: */
853 svcauth_gss_set_client(struct svc_rqst *rqstp)
855 struct gss_svc_data *svcdata = rqstp->rq_auth_data;
856 struct rsc *rsci = svcdata->rsci;
857 struct rpc_gss_wire_cred *gc = &svcdata->clcred;
859 rqstp->rq_client = find_gss_auth_domain(rsci->mechctx, gc->gc_svc);
860 if (rqstp->rq_client == NULL)
866 gss_write_init_verf(struct svc_rqst *rqstp, struct rsi *rsip)
870 if (rsip->major_status != GSS_S_COMPLETE)
871 return gss_write_null_verf(rqstp);
872 rsci = gss_svc_searchbyctx(&rsip->out_handle);
874 rsip->major_status = GSS_S_NO_CONTEXT;
875 return gss_write_null_verf(rqstp);
877 return gss_write_verf(rqstp, rsci->mechctx, GSS_SEQ_WIN);
881 * Accept an rpcsec packet.
882 * If context establishment, punt to user space
883 * If data exchange, verify/decrypt
884 * If context destruction, handle here
885 * In the context establishment and destruction case we encode
886 * response here and return SVC_COMPLETE.
889 svcauth_gss_accept(struct svc_rqst *rqstp, u32 *authp)
891 struct kvec *argv = &rqstp->rq_arg.head[0];
892 struct kvec *resv = &rqstp->rq_res.head[0];
894 struct xdr_netobj tmpobj;
895 struct gss_svc_data *svcdata = rqstp->rq_auth_data;
896 struct rpc_gss_wire_cred *gc;
897 struct rsc *rsci = NULL;
898 struct rsi *rsip, rsikey;
900 u32 *reject_stat = resv->iov_base + resv->iov_len;
903 dprintk("RPC: svcauth_gss: argv->iov_len = %zd\n",argv->iov_len);
905 *authp = rpc_autherr_badcred;
907 svcdata = kmalloc(sizeof(*svcdata), GFP_KERNEL);
910 rqstp->rq_auth_data = svcdata;
911 svcdata->body_start = NULL;
912 svcdata->rsci = NULL;
913 gc = &svcdata->clcred;
915 /* start of rpc packet is 7 u32's back from here:
916 * xid direction rpcversion prog vers proc flavour
918 rpcstart = argv->iov_base;
922 * version(==1), proc(0,1,2,3), seq, service (1,2,3), handle
923 * at least 5 u32s, and is preceeded by length, so that makes 6.
926 if (argv->iov_len < 5 * 4)
928 crlen = ntohl(svc_getu32(argv));
929 if (ntohl(svc_getu32(argv)) != RPC_GSS_VERSION)
931 gc->gc_proc = ntohl(svc_getu32(argv));
932 gc->gc_seq = ntohl(svc_getu32(argv));
933 gc->gc_svc = ntohl(svc_getu32(argv));
934 if (svc_safe_getnetobj(argv, &gc->gc_ctx))
936 if (crlen != round_up_to_quad(gc->gc_ctx.len) + 5 * 4)
939 if ((gc->gc_proc != RPC_GSS_PROC_DATA) && (rqstp->rq_proc != 0))
943 * We've successfully parsed the credential. Let's check out the
944 * verifier. An AUTH_NULL verifier is allowed (and required) for
945 * INIT and CONTINUE_INIT requests. AUTH_RPCSEC_GSS is required for
946 * PROC_DATA and PROC_DESTROY.
948 * AUTH_NULL verifier is 0 (AUTH_NULL), 0 (length).
949 * AUTH_RPCSEC_GSS verifier is:
950 * 6 (AUTH_RPCSEC_GSS), length, checksum.
951 * checksum is calculated over rpcheader from xid up to here.
953 *authp = rpc_autherr_badverf;
954 switch (gc->gc_proc) {
955 case RPC_GSS_PROC_INIT:
956 case RPC_GSS_PROC_CONTINUE_INIT:
957 if (argv->iov_len < 2 * 4)
959 if (ntohl(svc_getu32(argv)) != RPC_AUTH_NULL)
961 if (ntohl(svc_getu32(argv)) != 0)
964 case RPC_GSS_PROC_DATA:
965 case RPC_GSS_PROC_DESTROY:
966 *authp = rpcsec_gsserr_credproblem;
967 rsci = gss_svc_searchbyctx(&gc->gc_ctx);
970 switch (gss_verify_header(rqstp, rsci, rpcstart, gc, authp)) {
980 *authp = rpc_autherr_rejectedcred;
984 /* now act upon the command: */
985 switch (gc->gc_proc) {
986 case RPC_GSS_PROC_INIT:
987 case RPC_GSS_PROC_CONTINUE_INIT:
988 *authp = rpc_autherr_badcred;
989 if (gc->gc_proc == RPC_GSS_PROC_INIT && gc->gc_ctx.len != 0)
991 memset(&rsikey, 0, sizeof(rsikey));
992 if (dup_netobj(&rsikey.in_handle, &gc->gc_ctx))
994 *authp = rpc_autherr_badverf;
995 if (svc_safe_getnetobj(argv, &tmpobj)) {
996 kfree(rsikey.in_handle.data);
999 if (dup_netobj(&rsikey.in_token, &tmpobj)) {
1000 kfree(rsikey.in_handle.data);
1004 rsip = rsi_lookup(&rsikey);
1009 switch(cache_check(&rsi_cache, &rsip->h, &rqstp->rq_chandle)) {
1015 if (gss_write_init_verf(rqstp, rsip))
1017 if (resv->iov_len + 4 > PAGE_SIZE)
1019 svc_putu32(resv, rpc_success);
1020 if (svc_safe_putnetobj(resv, &rsip->out_handle))
1022 if (resv->iov_len + 3 * 4 > PAGE_SIZE)
1024 svc_putu32(resv, htonl(rsip->major_status));
1025 svc_putu32(resv, htonl(rsip->minor_status));
1026 svc_putu32(resv, htonl(GSS_SEQ_WIN));
1027 if (svc_safe_putnetobj(resv, &rsip->out_token))
1029 rqstp->rq_client = NULL;
1032 case RPC_GSS_PROC_DESTROY:
1033 set_bit(CACHE_NEGATIVE, &rsci->h.flags);
1034 if (resv->iov_len + 4 > PAGE_SIZE)
1036 svc_putu32(resv, rpc_success);
1038 case RPC_GSS_PROC_DATA:
1039 *authp = rpcsec_gsserr_ctxproblem;
1040 if (gss_write_verf(rqstp, rsci->mechctx, gc->gc_seq))
1042 rqstp->rq_cred = rsci->cred;
1043 get_group_info(rsci->cred.cr_group_info);
1044 *authp = rpc_autherr_badcred;
1045 switch (gc->gc_svc) {
1046 case RPC_GSS_SVC_NONE:
1048 case RPC_GSS_SVC_INTEGRITY:
1049 if (unwrap_integ_data(&rqstp->rq_arg,
1050 gc->gc_seq, rsci->mechctx))
1052 /* placeholders for length and seq. number: */
1053 svcdata->body_start = resv->iov_base + resv->iov_len;
1054 svc_putu32(resv, 0);
1055 svc_putu32(resv, 0);
1057 case RPC_GSS_SVC_PRIVACY:
1058 /* currently unsupported */
1062 svcdata->rsci = rsci;
1063 cache_get(&rsci->h);
1068 /* Restore write pointer to original value: */
1069 xdr_ressize_check(rqstp, reject_stat);
1079 rsc_put(&rsci->h, &rsc_cache);
1084 svcauth_gss_release(struct svc_rqst *rqstp)
1086 struct gss_svc_data *gsd = (struct gss_svc_data *)rqstp->rq_auth_data;
1087 struct rpc_gss_wire_cred *gc = &gsd->clcred;
1088 struct xdr_buf *resbuf = &rqstp->rq_res;
1089 struct xdr_buf integ_buf;
1090 struct xdr_netobj mic;
1093 int integ_offset, integ_len;
1096 if (gc->gc_proc != RPC_GSS_PROC_DATA)
1098 /* Release can be called twice, but we only wrap once. */
1099 if (gsd->body_start == NULL)
1101 /* normally not set till svc_send, but we need it here: */
1102 resbuf->len = resbuf->head[0].iov_len
1103 + resbuf->page_len + resbuf->tail[0].iov_len;
1104 switch (gc->gc_svc) {
1105 case RPC_GSS_SVC_NONE:
1107 case RPC_GSS_SVC_INTEGRITY:
1108 p = gsd->body_start;
1109 gsd->body_start = NULL;
1110 /* move accept_stat to right place: */
1111 memcpy(p, p + 2, 4);
1112 /* don't wrap in failure case: */
1113 /* Note: counting on not getting here if call was not even
1115 if (*p != rpc_success) {
1116 resbuf->head[0].iov_len -= 2 * 4;
1120 integ_offset = (u8 *)(p + 1) - (u8 *)resbuf->head[0].iov_base;
1121 integ_len = resbuf->len - integ_offset;
1122 BUG_ON(integ_len % 4);
1123 *p++ = htonl(integ_len);
1124 *p++ = htonl(gc->gc_seq);
1125 if (xdr_buf_subsegment(resbuf, &integ_buf, integ_offset,
1128 if (resbuf->page_len == 0
1129 && resbuf->tail[0].iov_len + RPC_MAX_AUTH_SIZE
1131 BUG_ON(resbuf->tail[0].iov_len);
1132 /* Use head for everything */
1133 resv = &resbuf->head[0];
1134 } else if (resbuf->tail[0].iov_base == NULL) {
1135 /* copied from nfsd4_encode_read */
1136 svc_take_page(rqstp);
1137 resbuf->tail[0].iov_base = page_address(rqstp
1138 ->rq_respages[rqstp->rq_resused-1]);
1139 rqstp->rq_restailpage = rqstp->rq_resused-1;
1140 resbuf->tail[0].iov_len = 0;
1141 resv = &resbuf->tail[0];
1143 resv = &resbuf->tail[0];
1145 mic.data = (u8 *)resv->iov_base + resv->iov_len + 4;
1146 if (gss_get_mic(gsd->rsci->mechctx, &integ_buf, &mic))
1148 svc_putu32(resv, htonl(mic.len));
1149 memset(mic.data + mic.len, 0,
1150 round_up_to_quad(mic.len) - mic.len);
1151 resv->iov_len += XDR_QUADLEN(mic.len) << 2;
1152 /* not strictly required: */
1153 resbuf->len += XDR_QUADLEN(mic.len) << 2;
1154 BUG_ON(resv->iov_len > PAGE_SIZE);
1156 case RPC_GSS_SVC_PRIVACY:
1164 if (rqstp->rq_client)
1165 auth_domain_put(rqstp->rq_client);
1166 rqstp->rq_client = NULL;
1167 if (rqstp->rq_cred.cr_group_info)
1168 put_group_info(rqstp->rq_cred.cr_group_info);
1169 rqstp->rq_cred.cr_group_info = NULL;
1171 rsc_put(&gsd->rsci->h, &rsc_cache);
1178 svcauth_gss_domain_release(struct auth_domain *dom)
1180 struct gss_domain *gd = container_of(dom, struct gss_domain, h);
1186 static struct auth_ops svcauthops_gss = {
1187 .name = "rpcsec_gss",
1188 .owner = THIS_MODULE,
1189 .flavour = RPC_AUTH_GSS,
1190 .accept = svcauth_gss_accept,
1191 .release = svcauth_gss_release,
1192 .domain_release = svcauth_gss_domain_release,
1193 .set_client = svcauth_gss_set_client,
1199 int rv = svc_auth_register(RPC_AUTH_GSS, &svcauthops_gss);
1201 cache_register(&rsc_cache);
1202 cache_register(&rsi_cache);
1208 gss_svc_shutdown(void)
1210 if (cache_unregister(&rsc_cache))
1211 printk(KERN_ERR "auth_rpcgss: failed to unregister rsc cache\n");
1212 if (cache_unregister(&rsi_cache))
1213 printk(KERN_ERR "auth_rpcgss: failed to unregister rsi cache\n");
1214 svc_auth_unregister(RPC_AUTH_GSS);