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,
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 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 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_s32(struct xdr_stream *xdr, s32 *res)
173 p = xdr_inline_decode(xdr, 4);
176 memcpy(res, p, sizeof(s32));
180 static int gssx_dec_linux_creds(struct xdr_stream *xdr,
181 struct svc_cred *creds)
188 p = xdr_inline_decode(xdr, 4);
189 if (unlikely(p == NULL))
192 length = be32_to_cpup(p);
194 if (length > (3 + NGROUPS_MAX) * sizeof(u32))
198 err = get_s32(xdr, &tmp);
201 creds->cr_uid = make_kuid(&init_user_ns, tmp);
204 err = get_s32(xdr, &tmp);
207 creds->cr_gid = make_kgid(&init_user_ns, tmp);
209 /* number of additional gid's */
210 err = get_s32(xdr, &tmp);
214 if ((3 + N) * sizeof(u32) != length)
216 creds->cr_group_info = groups_alloc(N);
217 if (creds->cr_group_info == NULL)
221 for (i = 0; i < N; i++) {
223 err = get_s32(xdr, &tmp);
225 goto out_free_groups;
227 kgid = make_kgid(&init_user_ns, tmp);
228 if (!gid_valid(kgid))
229 goto out_free_groups;
230 GROUP_AT(creds->cr_group_info, i) = kgid;
235 groups_free(creds->cr_group_info);
239 static int gssx_dec_option_array(struct xdr_stream *xdr,
240 struct gssx_option_array *oa)
242 struct svc_cred *creds;
247 p = xdr_inline_decode(xdr, 4);
248 if (unlikely(p == NULL))
250 count = be32_to_cpup(p++);
254 /* we recognize only 1 currently: CREDS_VALUE */
257 oa->data = kmalloc(sizeof(struct gssx_option), GFP_KERNEL);
261 creds = kmalloc(sizeof(struct svc_cred), GFP_KERNEL);
267 oa->data[0].option.data = CREDS_VALUE;
268 oa->data[0].option.len = sizeof(CREDS_VALUE);
269 oa->data[0].value.data = (void *)creds;
270 oa->data[0].value.len = 0;
272 for (i = 0; i < count; i++) {
273 gssx_buffer dummy = { 0, NULL };
277 p = xdr_inline_decode(xdr, 4);
278 if (unlikely(p == NULL))
281 length = be32_to_cpup(p);
282 p = xdr_inline_decode(xdr, length);
283 if (unlikely(p == NULL))
286 if (length == sizeof(CREDS_VALUE) &&
287 memcmp(p, CREDS_VALUE, sizeof(CREDS_VALUE)) == 0) {
288 /* We have creds here. parse them */
289 err = gssx_dec_linux_creds(xdr, creds);
292 oa->data[0].value.len = 1; /* presence */
294 /* consume uninteresting buffer */
295 err = gssx_dec_buffer(xdr, &dummy);
303 static int gssx_dec_status(struct xdr_stream *xdr,
304 struct gssx_status *status)
309 /* status->major_status */
310 p = xdr_inline_decode(xdr, 8);
311 if (unlikely(p == NULL))
313 p = xdr_decode_hyper(p, &status->major_status);
316 err = gssx_dec_buffer(xdr, &status->mech);
320 /* status->minor_status */
321 p = xdr_inline_decode(xdr, 8);
322 if (unlikely(p == NULL))
324 p = xdr_decode_hyper(p, &status->minor_status);
326 /* status->major_status_string */
327 err = gssx_dec_buffer(xdr, &status->major_status_string);
331 /* status->minor_status_string */
332 err = gssx_dec_buffer(xdr, &status->minor_status_string);
336 /* status->server_ctx */
337 err = gssx_dec_buffer(xdr, &status->server_ctx);
341 /* we assume we have no options for now, so simply consume them */
342 /* status->options */
343 err = dummy_dec_opt_array(xdr, &status->options);
348 static int gssx_enc_call_ctx(struct xdr_stream *xdr,
349 struct gssx_call_ctx *ctx)
351 struct gssx_option opt;
356 err = gssx_enc_buffer(xdr, &ctx->locale);
360 /* ctx->server_ctx */
361 err = gssx_enc_buffer(xdr, &ctx->server_ctx);
365 /* we always want to ask for lucid contexts */
367 p = xdr_reserve_space(xdr, 4);
370 /* we want a lucid_v1 context */
371 opt.option.data = LUCID_OPTION;
372 opt.option.len = sizeof(LUCID_OPTION);
373 opt.value.data = LUCID_VALUE;
374 opt.value.len = sizeof(LUCID_VALUE);
375 err = gssx_enc_option(xdr, &opt);
377 /* ..and user creds */
378 opt.option.data = CREDS_OPTION;
379 opt.option.len = sizeof(CREDS_OPTION);
380 opt.value.data = CREDS_VALUE;
381 opt.value.len = sizeof(CREDS_VALUE);
382 err = gssx_enc_option(xdr, &opt);
387 static int gssx_dec_name_attr(struct xdr_stream *xdr,
388 struct gssx_name_attr *attr)
393 err = gssx_dec_buffer(xdr, &attr->attr);
398 err = gssx_dec_buffer(xdr, &attr->value);
402 /* attr->extensions */
403 err = dummy_dec_opt_array(xdr, &attr->extensions);
408 static int dummy_enc_nameattr_array(struct xdr_stream *xdr,
409 struct gssx_name_attr_array *naa)
416 p = xdr_reserve_space(xdr, 4);
424 static int dummy_dec_nameattr_array(struct xdr_stream *xdr,
425 struct gssx_name_attr_array *naa)
427 struct gssx_name_attr dummy = { .attr = {.len = 0} };
431 p = xdr_inline_decode(xdr, 4);
432 if (unlikely(p == NULL))
434 count = be32_to_cpup(p++);
435 for (i = 0; i < count; i++) {
436 gssx_dec_name_attr(xdr, &dummy);
444 static struct xdr_netobj zero_netobj = {};
446 static struct gssx_name_attr_array zero_name_attr_array = {};
448 static struct gssx_option_array zero_option_array = {};
450 static int gssx_enc_name(struct xdr_stream *xdr,
451 struct gssx_name *name)
455 /* name->display_name */
456 err = gssx_enc_buffer(xdr, &name->display_name);
460 /* name->name_type */
461 err = gssx_enc_buffer(xdr, &zero_netobj);
465 /* name->exported_name */
466 err = gssx_enc_buffer(xdr, &zero_netobj);
470 /* name->exported_composite_name */
471 err = gssx_enc_buffer(xdr, &zero_netobj);
475 /* leave name_attributes empty for now, will add once we have any
476 * to pass up at all */
477 /* name->name_attributes */
478 err = dummy_enc_nameattr_array(xdr, &zero_name_attr_array);
482 /* leave options empty for now, will add once we have any options
483 * to pass up at all */
484 /* name->extensions */
485 err = dummy_enc_opt_array(xdr, &zero_option_array);
491 static int gssx_dec_name(struct xdr_stream *xdr,
492 struct gssx_name *name)
494 struct xdr_netobj dummy_netobj = { .len = 0 };
495 struct gssx_name_attr_array dummy_name_attr_array = { .count = 0 };
496 struct gssx_option_array dummy_option_array = { .count = 0 };
499 /* name->display_name */
500 err = gssx_dec_buffer(xdr, &name->display_name);
504 /* name->name_type */
505 err = gssx_dec_buffer(xdr, &dummy_netobj);
509 /* name->exported_name */
510 err = gssx_dec_buffer(xdr, &dummy_netobj);
514 /* name->exported_composite_name */
515 err = gssx_dec_buffer(xdr, &dummy_netobj);
519 /* we assume we have no attributes for now, so simply consume them */
520 /* name->name_attributes */
521 err = dummy_dec_nameattr_array(xdr, &dummy_name_attr_array);
525 /* we assume we have no options for now, so simply consume them */
526 /* name->extensions */
527 err = dummy_dec_opt_array(xdr, &dummy_option_array);
532 static int dummy_enc_credel_array(struct xdr_stream *xdr,
533 struct gssx_cred_element_array *cea)
540 p = xdr_reserve_space(xdr, 4);
548 static int gssx_enc_cred(struct xdr_stream *xdr,
549 struct gssx_cred *cred)
553 /* cred->desired_name */
554 err = gssx_enc_name(xdr, &cred->desired_name);
559 err = dummy_enc_credel_array(xdr, &cred->elements);
561 /* cred->cred_handle_reference */
562 err = gssx_enc_buffer(xdr, &cred->cred_handle_reference);
566 /* cred->needs_release */
567 err = gssx_enc_bool(xdr, cred->needs_release);
572 static int gssx_enc_ctx(struct xdr_stream *xdr,
573 struct gssx_ctx *ctx)
578 /* ctx->exported_context_token */
579 err = gssx_enc_buffer(xdr, &ctx->exported_context_token);
584 err = gssx_enc_buffer(xdr, &ctx->state);
588 /* ctx->need_release */
589 err = gssx_enc_bool(xdr, ctx->need_release);
594 err = gssx_enc_buffer(xdr, &ctx->mech);
599 err = gssx_enc_name(xdr, &ctx->src_name);
604 err = gssx_enc_name(xdr, &ctx->targ_name);
609 p = xdr_reserve_space(xdr, 8+8);
612 p = xdr_encode_hyper(p, ctx->lifetime);
615 p = xdr_encode_hyper(p, ctx->ctx_flags);
617 /* ctx->locally_initiated */
618 err = gssx_enc_bool(xdr, ctx->locally_initiated);
623 err = gssx_enc_bool(xdr, ctx->open);
627 /* leave options empty for now, will add once we have any options
628 * to pass up at all */
630 err = dummy_enc_opt_array(xdr, &ctx->options);
635 static int gssx_dec_ctx(struct xdr_stream *xdr,
636 struct gssx_ctx *ctx)
641 /* ctx->exported_context_token */
642 err = gssx_dec_buffer(xdr, &ctx->exported_context_token);
647 err = gssx_dec_buffer(xdr, &ctx->state);
651 /* ctx->need_release */
652 err = gssx_dec_bool(xdr, &ctx->need_release);
657 err = gssx_dec_buffer(xdr, &ctx->mech);
662 err = gssx_dec_name(xdr, &ctx->src_name);
667 err = gssx_dec_name(xdr, &ctx->targ_name);
672 p = xdr_inline_decode(xdr, 8+8);
673 if (unlikely(p == NULL))
675 p = xdr_decode_hyper(p, &ctx->lifetime);
678 p = xdr_decode_hyper(p, &ctx->ctx_flags);
680 /* ctx->locally_initiated */
681 err = gssx_dec_bool(xdr, &ctx->locally_initiated);
686 err = gssx_dec_bool(xdr, &ctx->open);
690 /* we assume we have no options for now, so simply consume them */
692 err = dummy_dec_opt_array(xdr, &ctx->options);
697 static int gssx_enc_cb(struct xdr_stream *xdr, struct gssx_cb *cb)
702 /* cb->initiator_addrtype */
703 p = xdr_reserve_space(xdr, 8);
706 p = xdr_encode_hyper(p, cb->initiator_addrtype);
708 /* cb->initiator_address */
709 err = gssx_enc_buffer(xdr, &cb->initiator_address);
713 /* cb->acceptor_addrtype */
714 p = xdr_reserve_space(xdr, 8);
717 p = xdr_encode_hyper(p, cb->acceptor_addrtype);
719 /* cb->acceptor_address */
720 err = gssx_enc_buffer(xdr, &cb->acceptor_address);
724 /* cb->application_data */
725 err = gssx_enc_buffer(xdr, &cb->application_data);
730 void gssx_enc_accept_sec_context(struct rpc_rqst *req,
731 struct xdr_stream *xdr,
732 struct gssx_arg_accept_sec_context *arg)
736 err = gssx_enc_call_ctx(xdr, &arg->call_ctx);
740 /* arg->context_handle */
741 if (arg->context_handle) {
742 err = gssx_enc_ctx(xdr, arg->context_handle);
746 err = gssx_enc_bool(xdr, 0);
749 /* arg->cred_handle */
750 if (arg->cred_handle) {
751 err = gssx_enc_cred(xdr, arg->cred_handle);
755 err = gssx_enc_bool(xdr, 0);
758 /* arg->input_token */
759 err = gssx_enc_in_token(xdr, &arg->input_token);
765 err = gssx_enc_cb(xdr, arg->input_cb);
769 err = gssx_enc_bool(xdr, 0);
772 err = gssx_enc_bool(xdr, arg->ret_deleg_cred);
776 /* leave options empty for now, will add once we have any options
777 * to pass up at all */
779 err = dummy_enc_opt_array(xdr, &arg->options);
783 dprintk("RPC: gssx_enc_accept_sec_context: %d\n", err);
786 int gssx_dec_accept_sec_context(struct rpc_rqst *rqstp,
787 struct xdr_stream *xdr,
788 struct gssx_res_accept_sec_context *res)
794 err = gssx_dec_status(xdr, &res->status);
798 /* res->context_handle */
799 err = gssx_dec_bool(xdr, &value_follows);
803 err = gssx_dec_ctx(xdr, res->context_handle);
807 res->context_handle = NULL;
810 /* res->output_token */
811 err = gssx_dec_bool(xdr, &value_follows);
815 err = gssx_dec_buffer(xdr, res->output_token);
819 res->output_token = NULL;
822 /* res->delegated_cred_handle */
823 err = gssx_dec_bool(xdr, &value_follows);
827 /* we do not support upcall servers sending this data. */
832 err = gssx_dec_option_array(xdr, &res->options);