2 * GSS Proxy upcall module
4 * Copyright (C) 2012 Simo Sorce <simo@redhat.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <linux/sunrpc/svcauth.h>
22 #include "gss_rpc_xdr.h"
24 static int gssx_enc_bool(struct xdr_stream *xdr, int v)
28 p = xdr_reserve_space(xdr, 4);
29 if (unlikely(p == NULL))
31 *p = v ? xdr_one : xdr_zero;
35 static int gssx_dec_bool(struct xdr_stream *xdr, u32 *v)
39 p = xdr_inline_decode(xdr, 4);
40 if (unlikely(p == NULL))
46 static int gssx_enc_buffer(struct xdr_stream *xdr,
47 const gssx_buffer *buf)
51 p = xdr_reserve_space(xdr, sizeof(u32) + buf->len);
54 xdr_encode_opaque(p, buf->data, buf->len);
58 static int gssx_enc_in_token(struct xdr_stream *xdr,
59 const struct gssp_in_token *in)
63 p = xdr_reserve_space(xdr, 4);
66 *p = cpu_to_be32(in->page_len);
68 /* all we need to do is to write pages */
69 xdr_write_pages(xdr, in->pages, in->page_base, in->page_len);
75 static int gssx_dec_buffer(struct xdr_stream *xdr,
81 p = xdr_inline_decode(xdr, 4);
82 if (unlikely(p == NULL))
85 length = be32_to_cpup(p);
86 p = xdr_inline_decode(xdr, length);
87 if (unlikely(p == NULL))
91 /* we intentionally are not interested in this buffer */
94 if (length > buf->len)
98 buf->data = kmemdup(p, length, GFP_KERNEL);
102 memcpy(buf->data, p, length);
108 static int gssx_enc_option(struct xdr_stream *xdr,
109 struct gssx_option *opt)
113 err = gssx_enc_buffer(xdr, &opt->option);
116 err = gssx_enc_buffer(xdr, &opt->value);
120 static int gssx_dec_option(struct xdr_stream *xdr,
121 struct gssx_option *opt)
125 err = gssx_dec_buffer(xdr, &opt->option);
128 err = gssx_dec_buffer(xdr, &opt->value);
132 static int dummy_enc_opt_array(struct xdr_stream *xdr,
133 const struct gssx_option_array *oa)
140 p = xdr_reserve_space(xdr, 4);
148 static int dummy_dec_opt_array(struct xdr_stream *xdr,
149 struct gssx_option_array *oa)
151 struct gssx_option dummy;
155 p = xdr_inline_decode(xdr, 4);
156 if (unlikely(p == NULL))
158 count = be32_to_cpup(p++);
159 memset(&dummy, 0, sizeof(dummy));
160 for (i = 0; i < count; i++) {
161 gssx_dec_option(xdr, &dummy);
169 static int get_host_u32(struct xdr_stream *xdr, u32 *res)
173 p = xdr_inline_decode(xdr, 4);
176 /* Contents of linux creds are all host-endian: */
177 memcpy(res, p, sizeof(u32));
181 static int gssx_dec_linux_creds(struct xdr_stream *xdr,
182 struct svc_cred *creds)
190 p = xdr_inline_decode(xdr, 4);
191 if (unlikely(p == NULL))
194 length = be32_to_cpup(p);
196 if (length > (3 + NGROUPS_MAX) * sizeof(u32))
200 err = get_host_u32(xdr, &tmp);
203 creds->cr_uid = make_kuid(&init_user_ns, tmp);
206 err = get_host_u32(xdr, &tmp);
209 creds->cr_gid = make_kgid(&init_user_ns, tmp);
211 /* number of additional gid's */
212 err = get_host_u32(xdr, &tmp);
216 if ((3 + N) * sizeof(u32) != length)
218 creds->cr_group_info = groups_alloc(N);
219 if (creds->cr_group_info == NULL)
223 for (i = 0; i < N; i++) {
225 err = get_host_u32(xdr, &tmp);
227 goto out_free_groups;
229 kgid = make_kgid(&init_user_ns, tmp);
230 if (!gid_valid(kgid))
231 goto out_free_groups;
232 creds->cr_group_info->gid[i] = kgid;
234 groups_sort(creds->cr_group_info);
238 groups_free(creds->cr_group_info);
242 static int gssx_dec_option_array(struct xdr_stream *xdr,
243 struct gssx_option_array *oa)
245 struct svc_cred *creds;
250 p = xdr_inline_decode(xdr, 4);
251 if (unlikely(p == NULL))
253 count = be32_to_cpup(p++);
257 /* we recognize only 1 currently: CREDS_VALUE */
260 oa->data = kmalloc(sizeof(struct gssx_option), GFP_KERNEL);
264 creds = kzalloc(sizeof(struct svc_cred), GFP_KERNEL);
270 oa->data[0].option.data = CREDS_VALUE;
271 oa->data[0].option.len = sizeof(CREDS_VALUE);
272 oa->data[0].value.data = (void *)creds;
273 oa->data[0].value.len = 0;
275 for (i = 0; i < count; i++) {
276 gssx_buffer dummy = { 0, NULL };
280 p = xdr_inline_decode(xdr, 4);
281 if (unlikely(p == NULL)) {
286 length = be32_to_cpup(p);
287 p = xdr_inline_decode(xdr, length);
288 if (unlikely(p == NULL)) {
293 if (length == sizeof(CREDS_VALUE) &&
294 memcmp(p, CREDS_VALUE, sizeof(CREDS_VALUE)) == 0) {
295 /* We have creds here. parse them */
296 err = gssx_dec_linux_creds(xdr, creds);
299 oa->data[0].value.len = 1; /* presence */
301 /* consume uninteresting buffer */
302 err = gssx_dec_buffer(xdr, &dummy);
317 static int gssx_dec_status(struct xdr_stream *xdr,
318 struct gssx_status *status)
323 /* status->major_status */
324 p = xdr_inline_decode(xdr, 8);
325 if (unlikely(p == NULL))
327 p = xdr_decode_hyper(p, &status->major_status);
330 err = gssx_dec_buffer(xdr, &status->mech);
334 /* status->minor_status */
335 p = xdr_inline_decode(xdr, 8);
336 if (unlikely(p == NULL))
338 p = xdr_decode_hyper(p, &status->minor_status);
340 /* status->major_status_string */
341 err = gssx_dec_buffer(xdr, &status->major_status_string);
345 /* status->minor_status_string */
346 err = gssx_dec_buffer(xdr, &status->minor_status_string);
350 /* status->server_ctx */
351 err = gssx_dec_buffer(xdr, &status->server_ctx);
355 /* we assume we have no options for now, so simply consume them */
356 /* status->options */
357 err = dummy_dec_opt_array(xdr, &status->options);
362 static int gssx_enc_call_ctx(struct xdr_stream *xdr,
363 const struct gssx_call_ctx *ctx)
365 struct gssx_option opt;
370 err = gssx_enc_buffer(xdr, &ctx->locale);
374 /* ctx->server_ctx */
375 err = gssx_enc_buffer(xdr, &ctx->server_ctx);
379 /* we always want to ask for lucid contexts */
381 p = xdr_reserve_space(xdr, 4);
384 /* we want a lucid_v1 context */
385 opt.option.data = LUCID_OPTION;
386 opt.option.len = sizeof(LUCID_OPTION);
387 opt.value.data = LUCID_VALUE;
388 opt.value.len = sizeof(LUCID_VALUE);
389 err = gssx_enc_option(xdr, &opt);
391 /* ..and user creds */
392 opt.option.data = CREDS_OPTION;
393 opt.option.len = sizeof(CREDS_OPTION);
394 opt.value.data = CREDS_VALUE;
395 opt.value.len = sizeof(CREDS_VALUE);
396 err = gssx_enc_option(xdr, &opt);
401 static int gssx_dec_name_attr(struct xdr_stream *xdr,
402 struct gssx_name_attr *attr)
407 err = gssx_dec_buffer(xdr, &attr->attr);
412 err = gssx_dec_buffer(xdr, &attr->value);
416 /* attr->extensions */
417 err = dummy_dec_opt_array(xdr, &attr->extensions);
422 static int dummy_enc_nameattr_array(struct xdr_stream *xdr,
423 struct gssx_name_attr_array *naa)
430 p = xdr_reserve_space(xdr, 4);
438 static int dummy_dec_nameattr_array(struct xdr_stream *xdr,
439 struct gssx_name_attr_array *naa)
441 struct gssx_name_attr dummy = { .attr = {.len = 0} };
445 p = xdr_inline_decode(xdr, 4);
446 if (unlikely(p == NULL))
448 count = be32_to_cpup(p++);
449 for (i = 0; i < count; i++) {
450 gssx_dec_name_attr(xdr, &dummy);
458 static struct xdr_netobj zero_netobj = {};
460 static struct gssx_name_attr_array zero_name_attr_array = {};
462 static struct gssx_option_array zero_option_array = {};
464 static int gssx_enc_name(struct xdr_stream *xdr,
465 struct gssx_name *name)
469 /* name->display_name */
470 err = gssx_enc_buffer(xdr, &name->display_name);
474 /* name->name_type */
475 err = gssx_enc_buffer(xdr, &zero_netobj);
479 /* name->exported_name */
480 err = gssx_enc_buffer(xdr, &zero_netobj);
484 /* name->exported_composite_name */
485 err = gssx_enc_buffer(xdr, &zero_netobj);
489 /* leave name_attributes empty for now, will add once we have any
490 * to pass up at all */
491 /* name->name_attributes */
492 err = dummy_enc_nameattr_array(xdr, &zero_name_attr_array);
496 /* leave options empty for now, will add once we have any options
497 * to pass up at all */
498 /* name->extensions */
499 err = dummy_enc_opt_array(xdr, &zero_option_array);
505 static int gssx_dec_name(struct xdr_stream *xdr,
506 struct gssx_name *name)
508 struct xdr_netobj dummy_netobj = { .len = 0 };
509 struct gssx_name_attr_array dummy_name_attr_array = { .count = 0 };
510 struct gssx_option_array dummy_option_array = { .count = 0 };
513 /* name->display_name */
514 err = gssx_dec_buffer(xdr, &name->display_name);
518 /* name->name_type */
519 err = gssx_dec_buffer(xdr, &dummy_netobj);
523 /* name->exported_name */
524 err = gssx_dec_buffer(xdr, &dummy_netobj);
528 /* name->exported_composite_name */
529 err = gssx_dec_buffer(xdr, &dummy_netobj);
533 /* we assume we have no attributes for now, so simply consume them */
534 /* name->name_attributes */
535 err = dummy_dec_nameattr_array(xdr, &dummy_name_attr_array);
539 /* we assume we have no options for now, so simply consume them */
540 /* name->extensions */
541 err = dummy_dec_opt_array(xdr, &dummy_option_array);
546 static int dummy_enc_credel_array(struct xdr_stream *xdr,
547 struct gssx_cred_element_array *cea)
554 p = xdr_reserve_space(xdr, 4);
562 static int gssx_enc_cred(struct xdr_stream *xdr,
563 struct gssx_cred *cred)
567 /* cred->desired_name */
568 err = gssx_enc_name(xdr, &cred->desired_name);
573 err = dummy_enc_credel_array(xdr, &cred->elements);
577 /* cred->cred_handle_reference */
578 err = gssx_enc_buffer(xdr, &cred->cred_handle_reference);
582 /* cred->needs_release */
583 err = gssx_enc_bool(xdr, cred->needs_release);
588 static int gssx_enc_ctx(struct xdr_stream *xdr,
589 struct gssx_ctx *ctx)
594 /* ctx->exported_context_token */
595 err = gssx_enc_buffer(xdr, &ctx->exported_context_token);
600 err = gssx_enc_buffer(xdr, &ctx->state);
604 /* ctx->need_release */
605 err = gssx_enc_bool(xdr, ctx->need_release);
610 err = gssx_enc_buffer(xdr, &ctx->mech);
615 err = gssx_enc_name(xdr, &ctx->src_name);
620 err = gssx_enc_name(xdr, &ctx->targ_name);
625 p = xdr_reserve_space(xdr, 8+8);
628 p = xdr_encode_hyper(p, ctx->lifetime);
631 p = xdr_encode_hyper(p, ctx->ctx_flags);
633 /* ctx->locally_initiated */
634 err = gssx_enc_bool(xdr, ctx->locally_initiated);
639 err = gssx_enc_bool(xdr, ctx->open);
643 /* leave options empty for now, will add once we have any options
644 * to pass up at all */
646 err = dummy_enc_opt_array(xdr, &ctx->options);
651 static int gssx_dec_ctx(struct xdr_stream *xdr,
652 struct gssx_ctx *ctx)
657 /* ctx->exported_context_token */
658 err = gssx_dec_buffer(xdr, &ctx->exported_context_token);
663 err = gssx_dec_buffer(xdr, &ctx->state);
667 /* ctx->need_release */
668 err = gssx_dec_bool(xdr, &ctx->need_release);
673 err = gssx_dec_buffer(xdr, &ctx->mech);
678 err = gssx_dec_name(xdr, &ctx->src_name);
683 err = gssx_dec_name(xdr, &ctx->targ_name);
688 p = xdr_inline_decode(xdr, 8+8);
689 if (unlikely(p == NULL))
691 p = xdr_decode_hyper(p, &ctx->lifetime);
694 p = xdr_decode_hyper(p, &ctx->ctx_flags);
696 /* ctx->locally_initiated */
697 err = gssx_dec_bool(xdr, &ctx->locally_initiated);
702 err = gssx_dec_bool(xdr, &ctx->open);
706 /* we assume we have no options for now, so simply consume them */
708 err = dummy_dec_opt_array(xdr, &ctx->options);
713 static int gssx_enc_cb(struct xdr_stream *xdr, struct gssx_cb *cb)
718 /* cb->initiator_addrtype */
719 p = xdr_reserve_space(xdr, 8);
722 p = xdr_encode_hyper(p, cb->initiator_addrtype);
724 /* cb->initiator_address */
725 err = gssx_enc_buffer(xdr, &cb->initiator_address);
729 /* cb->acceptor_addrtype */
730 p = xdr_reserve_space(xdr, 8);
733 p = xdr_encode_hyper(p, cb->acceptor_addrtype);
735 /* cb->acceptor_address */
736 err = gssx_enc_buffer(xdr, &cb->acceptor_address);
740 /* cb->application_data */
741 err = gssx_enc_buffer(xdr, &cb->application_data);
746 void gssx_enc_accept_sec_context(struct rpc_rqst *req,
747 struct xdr_stream *xdr,
750 const struct gssx_arg_accept_sec_context *arg = data;
753 err = gssx_enc_call_ctx(xdr, &arg->call_ctx);
757 /* arg->context_handle */
758 if (arg->context_handle)
759 err = gssx_enc_ctx(xdr, arg->context_handle);
761 err = gssx_enc_bool(xdr, 0);
765 /* arg->cred_handle */
766 if (arg->cred_handle)
767 err = gssx_enc_cred(xdr, arg->cred_handle);
769 err = gssx_enc_bool(xdr, 0);
773 /* arg->input_token */
774 err = gssx_enc_in_token(xdr, &arg->input_token);
780 err = gssx_enc_cb(xdr, arg->input_cb);
782 err = gssx_enc_bool(xdr, 0);
786 err = gssx_enc_bool(xdr, arg->ret_deleg_cred);
790 /* leave options empty for now, will add once we have any options
791 * to pass up at all */
793 err = dummy_enc_opt_array(xdr, &arg->options);
795 xdr_inline_pages(&req->rq_rcv_buf,
796 PAGE_SIZE/2 /* pretty arbitrary */,
797 arg->pages, 0 /* page base */, arg->npages * PAGE_SIZE);
800 dprintk("RPC: gssx_enc_accept_sec_context: %d\n", err);
803 int gssx_dec_accept_sec_context(struct rpc_rqst *rqstp,
804 struct xdr_stream *xdr,
807 struct gssx_res_accept_sec_context *res = data;
810 struct page *scratch;
812 scratch = alloc_page(GFP_KERNEL);
815 xdr_set_scratch_buffer(xdr, page_address(scratch), PAGE_SIZE);
818 err = gssx_dec_status(xdr, &res->status);
822 /* res->context_handle */
823 err = gssx_dec_bool(xdr, &value_follows);
827 err = gssx_dec_ctx(xdr, res->context_handle);
831 res->context_handle = NULL;
834 /* res->output_token */
835 err = gssx_dec_bool(xdr, &value_follows);
839 err = gssx_dec_buffer(xdr, res->output_token);
843 res->output_token = NULL;
846 /* res->delegated_cred_handle */
847 err = gssx_dec_bool(xdr, &value_follows);
851 /* we do not support upcall servers sending this data. */
857 err = gssx_dec_option_array(xdr, &res->options);
860 __free_page(scratch);