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 bool gssx_check_pointer(struct xdr_stream *xdr)
28 p = xdr_reserve_space(xdr, 4);
29 if (unlikely(p == NULL))
34 static int gssx_enc_bool(struct xdr_stream *xdr, int v)
38 p = xdr_reserve_space(xdr, 4);
39 if (unlikely(p == NULL))
41 *p = v ? xdr_one : xdr_zero;
45 static int gssx_dec_bool(struct xdr_stream *xdr, u32 *v)
49 p = xdr_inline_decode(xdr, 4);
50 if (unlikely(p == NULL))
56 static int gssx_enc_buffer(struct xdr_stream *xdr,
61 p = xdr_reserve_space(xdr, sizeof(u32) + buf->len);
64 xdr_encode_opaque(p, buf->data, buf->len);
68 static int gssx_enc_in_token(struct xdr_stream *xdr,
69 struct gssp_in_token *in)
73 p = xdr_reserve_space(xdr, 4);
76 *p = cpu_to_be32(in->page_len);
78 /* all we need to do is to write pages */
79 xdr_write_pages(xdr, in->pages, in->page_base, in->page_len);
85 static int gssx_dec_buffer(struct xdr_stream *xdr,
91 p = xdr_inline_decode(xdr, 4);
92 if (unlikely(p == NULL))
95 length = be32_to_cpup(p);
96 p = xdr_inline_decode(xdr, length);
97 if (unlikely(p == NULL))
101 /* we intentionally are not interested in this buffer */
104 if (length > buf->len)
108 buf->data = kmemdup(p, length, GFP_KERNEL);
112 memcpy(buf->data, p, length);
118 static int gssx_enc_option(struct xdr_stream *xdr,
119 struct gssx_option *opt)
123 err = gssx_enc_buffer(xdr, &opt->option);
126 err = gssx_enc_buffer(xdr, &opt->value);
130 static int gssx_dec_option(struct xdr_stream *xdr,
131 struct gssx_option *opt)
135 err = gssx_dec_buffer(xdr, &opt->option);
138 err = gssx_dec_buffer(xdr, &opt->value);
142 static int dummy_enc_opt_array(struct xdr_stream *xdr,
143 struct gssx_option_array *oa)
150 p = xdr_reserve_space(xdr, 4);
158 static int dummy_dec_opt_array(struct xdr_stream *xdr,
159 struct gssx_option_array *oa)
161 struct gssx_option dummy;
165 p = xdr_inline_decode(xdr, 4);
166 if (unlikely(p == NULL))
168 count = be32_to_cpup(p++);
169 memset(&dummy, 0, sizeof(dummy));
170 for (i = 0; i < count; i++) {
171 gssx_dec_option(xdr, &dummy);
179 static int get_s32(void **p, void *max, s32 *res)
182 void *next = (void *)((char *)base + sizeof(s32));
183 if (unlikely(next > max || next < base))
185 memcpy(res, base, sizeof(s32));
190 static int gssx_dec_linux_creds(struct xdr_stream *xdr,
191 struct svc_cred *creds)
199 p = xdr_inline_decode(xdr, 4);
200 if (unlikely(p == NULL))
203 length = be32_to_cpup(p);
205 /* FIXME: we do not want to use the scratch buffer for this one
206 * may need to use functions that allows us to access an io vector
208 p = xdr_inline_decode(xdr, length);
209 if (unlikely(p == NULL))
216 err = get_s32(&q, end, &tmp);
222 err = get_s32(&q, end, &tmp);
227 /* number of additional gid's */
228 err = get_s32(&q, end, &tmp);
232 creds->cr_group_info = groups_alloc(N);
233 if (creds->cr_group_info == NULL)
237 for (i = 0; i < N; i++) {
238 err = get_s32(&q, end, &tmp);
240 groups_free(creds->cr_group_info);
243 GROUP_AT(creds->cr_group_info, i) = tmp;
249 static int gssx_dec_option_array(struct xdr_stream *xdr,
250 struct gssx_option_array *oa)
252 struct svc_cred *creds;
257 p = xdr_inline_decode(xdr, 4);
258 if (unlikely(p == NULL))
260 count = be32_to_cpup(p++);
262 /* we recognize only 1 currently: CREDS_VALUE */
265 oa->data = kmalloc(sizeof(struct gssx_option), GFP_KERNEL);
269 creds = kmalloc(sizeof(struct svc_cred), GFP_KERNEL);
275 oa->data[0].option.data = CREDS_VALUE;
276 oa->data[0].option.len = sizeof(CREDS_VALUE);
277 oa->data[0].value.data = (void *)creds;
278 oa->data[0].value.len = 0;
280 for (i = 0; i < count; i++) {
281 gssx_buffer dummy = { 0, NULL };
285 p = xdr_inline_decode(xdr, 4);
286 if (unlikely(p == NULL))
289 length = be32_to_cpup(p);
290 p = xdr_inline_decode(xdr, length);
291 if (unlikely(p == NULL))
294 if (length == sizeof(CREDS_VALUE) &&
295 memcmp(p, CREDS_VALUE, sizeof(CREDS_VALUE)) == 0) {
296 /* We have creds here. parse them */
297 err = gssx_dec_linux_creds(xdr, creds);
300 oa->data[0].value.len = 1; /* presence */
302 /* consume uninteresting buffer */
303 err = gssx_dec_buffer(xdr, &dummy);
311 static int gssx_dec_status(struct xdr_stream *xdr,
312 struct gssx_status *status)
317 /* status->major_status */
318 p = xdr_inline_decode(xdr, 8);
319 if (unlikely(p == NULL))
321 p = xdr_decode_hyper(p, &status->major_status);
324 err = gssx_dec_buffer(xdr, &status->mech);
328 /* status->minor_status */
329 p = xdr_inline_decode(xdr, 8);
330 if (unlikely(p == NULL))
332 p = xdr_decode_hyper(p, &status->minor_status);
334 /* status->major_status_string */
335 err = gssx_dec_buffer(xdr, &status->major_status_string);
339 /* status->minor_status_string */
340 err = gssx_dec_buffer(xdr, &status->minor_status_string);
344 /* status->server_ctx */
345 err = gssx_dec_buffer(xdr, &status->server_ctx);
349 /* we assume we have no options for now, so simply consume them */
350 /* status->options */
351 err = dummy_dec_opt_array(xdr, &status->options);
356 static int gssx_enc_call_ctx(struct xdr_stream *xdr,
357 struct gssx_call_ctx *ctx)
359 struct gssx_option opt;
364 err = gssx_enc_buffer(xdr, &ctx->locale);
368 /* ctx->server_ctx */
369 err = gssx_enc_buffer(xdr, &ctx->server_ctx);
373 /* we always want to ask for lucid contexts */
375 p = xdr_reserve_space(xdr, 4);
378 /* we want a lucid_v1 context */
379 opt.option.data = LUCID_OPTION;
380 opt.option.len = sizeof(LUCID_OPTION);
381 opt.value.data = LUCID_VALUE;
382 opt.value.len = sizeof(LUCID_VALUE);
383 err = gssx_enc_option(xdr, &opt);
385 /* ..and user creds */
386 opt.option.data = CREDS_OPTION;
387 opt.option.len = sizeof(CREDS_OPTION);
388 opt.value.data = CREDS_VALUE;
389 opt.value.len = sizeof(CREDS_VALUE);
390 err = gssx_enc_option(xdr, &opt);
395 static int gssx_dec_name_attr(struct xdr_stream *xdr,
396 struct gssx_name_attr *attr)
401 err = gssx_dec_buffer(xdr, &attr->attr);
406 err = gssx_dec_buffer(xdr, &attr->value);
410 /* attr->extensions */
411 err = dummy_dec_opt_array(xdr, &attr->extensions);
416 static int dummy_enc_nameattr_array(struct xdr_stream *xdr,
417 struct gssx_name_attr_array *naa)
424 p = xdr_reserve_space(xdr, 4);
432 static int dummy_dec_nameattr_array(struct xdr_stream *xdr,
433 struct gssx_name_attr_array *naa)
435 struct gssx_name_attr dummy;
439 p = xdr_inline_decode(xdr, 4);
440 if (unlikely(p == NULL))
442 count = be32_to_cpup(p++);
443 for (i = 0; i < count; i++) {
444 gssx_dec_name_attr(xdr, &dummy);
452 static struct xdr_netobj zero_netobj = {};
454 static struct gssx_name_attr_array zero_name_attr_array = {};
456 static struct gssx_option_array zero_option_array = {};
458 static int gssx_enc_name(struct xdr_stream *xdr,
459 struct gssx_name *name)
463 /* name->display_name */
464 err = gssx_enc_buffer(xdr, &name->display_name);
468 /* name->name_type */
469 err = gssx_enc_buffer(xdr, &zero_netobj);
473 /* name->exported_name */
474 err = gssx_enc_buffer(xdr, &zero_netobj);
478 /* name->exported_composite_name */
479 err = gssx_enc_buffer(xdr, &zero_netobj);
483 /* leave name_attributes empty for now, will add once we have any
484 * to pass up at all */
485 /* name->name_attributes */
486 err = dummy_enc_nameattr_array(xdr, &zero_name_attr_array);
490 /* leave options empty for now, will add once we have any options
491 * to pass up at all */
492 /* name->extensions */
493 err = dummy_enc_opt_array(xdr, &zero_option_array);
498 static int gssx_dec_name(struct xdr_stream *xdr,
499 struct gssx_name *name)
501 struct xdr_netobj dummy_netobj;
502 struct gssx_name_attr_array dummy_name_attr_array;
503 struct gssx_option_array dummy_option_array;
506 /* name->display_name */
507 err = gssx_dec_buffer(xdr, &name->display_name);
511 /* name->name_type */
512 err = gssx_dec_buffer(xdr, &dummy_netobj);
516 /* name->exported_name */
517 err = gssx_dec_buffer(xdr, &dummy_netobj);
521 /* name->exported_composite_name */
522 err = gssx_dec_buffer(xdr, &dummy_netobj);
526 /* we assume we have no attributes for now, so simply consume them */
527 /* name->name_attributes */
528 err = dummy_dec_nameattr_array(xdr, &dummy_name_attr_array);
532 /* we assume we have no options for now, so simply consume them */
533 /* name->extensions */
534 err = dummy_dec_opt_array(xdr, &dummy_option_array);
539 static int dummy_enc_credel_array(struct xdr_stream *xdr,
540 struct gssx_cred_element_array *cea)
547 p = xdr_reserve_space(xdr, 4);
555 static int gssx_enc_cred(struct xdr_stream *xdr,
556 struct gssx_cred *cred)
560 /* cred->desired_name */
561 err = gssx_enc_name(xdr, &cred->desired_name);
566 err = dummy_enc_credel_array(xdr, &cred->elements);
568 /* cred->cred_handle_reference */
569 err = gssx_enc_buffer(xdr, &cred->cred_handle_reference);
573 /* cred->needs_release */
574 err = gssx_enc_bool(xdr, cred->needs_release);
579 static int gssx_enc_ctx(struct xdr_stream *xdr,
580 struct gssx_ctx *ctx)
585 /* ctx->exported_context_token */
586 err = gssx_enc_buffer(xdr, &ctx->exported_context_token);
591 err = gssx_enc_buffer(xdr, &ctx->state);
595 /* ctx->need_release */
596 err = gssx_enc_bool(xdr, ctx->need_release);
601 err = gssx_enc_buffer(xdr, &ctx->mech);
606 err = gssx_enc_name(xdr, &ctx->src_name);
611 err = gssx_enc_name(xdr, &ctx->targ_name);
616 p = xdr_reserve_space(xdr, 8+8);
619 p = xdr_encode_hyper(p, ctx->lifetime);
622 p = xdr_encode_hyper(p, ctx->ctx_flags);
624 /* ctx->locally_initiated */
625 err = gssx_enc_bool(xdr, ctx->locally_initiated);
630 err = gssx_enc_bool(xdr, ctx->open);
634 /* leave options empty for now, will add once we have any options
635 * to pass up at all */
637 err = dummy_enc_opt_array(xdr, &ctx->options);
642 static int gssx_dec_ctx(struct xdr_stream *xdr,
643 struct gssx_ctx *ctx)
648 /* ctx->exported_context_token */
649 err = gssx_dec_buffer(xdr, &ctx->exported_context_token);
654 err = gssx_dec_buffer(xdr, &ctx->state);
658 /* ctx->need_release */
659 err = gssx_dec_bool(xdr, &ctx->need_release);
664 err = gssx_dec_buffer(xdr, &ctx->mech);
669 err = gssx_dec_name(xdr, &ctx->src_name);
674 err = gssx_dec_name(xdr, &ctx->targ_name);
679 p = xdr_inline_decode(xdr, 8+8);
680 if (unlikely(p == NULL))
682 p = xdr_decode_hyper(p, &ctx->lifetime);
685 p = xdr_decode_hyper(p, &ctx->ctx_flags);
687 /* ctx->locally_initiated */
688 err = gssx_dec_bool(xdr, &ctx->locally_initiated);
693 err = gssx_dec_bool(xdr, &ctx->open);
697 /* we assume we have no options for now, so simply consume them */
699 err = dummy_dec_opt_array(xdr, &ctx->options);
704 static int gssx_enc_cb(struct xdr_stream *xdr, struct gssx_cb *cb)
709 /* cb->initiator_addrtype */
710 p = xdr_reserve_space(xdr, 8);
713 p = xdr_encode_hyper(p, cb->initiator_addrtype);
715 /* cb->initiator_address */
716 err = gssx_enc_buffer(xdr, &cb->initiator_address);
720 /* cb->acceptor_addrtype */
721 p = xdr_reserve_space(xdr, 8);
724 p = xdr_encode_hyper(p, cb->acceptor_addrtype);
726 /* cb->acceptor_address */
727 err = gssx_enc_buffer(xdr, &cb->acceptor_address);
731 /* cb->application_data */
732 err = gssx_enc_buffer(xdr, &cb->application_data);
737 void gssx_enc_accept_sec_context(struct rpc_rqst *req,
738 struct xdr_stream *xdr,
739 struct gssx_arg_accept_sec_context *arg)
743 err = gssx_enc_call_ctx(xdr, &arg->call_ctx);
747 /* arg->context_handle */
748 if (arg->context_handle) {
749 err = gssx_enc_ctx(xdr, arg->context_handle);
753 err = gssx_enc_bool(xdr, 0);
756 /* arg->cred_handle */
757 if (arg->cred_handle) {
758 err = gssx_enc_cred(xdr, arg->cred_handle);
762 err = gssx_enc_bool(xdr, 0);
765 /* arg->input_token */
766 err = gssx_enc_in_token(xdr, &arg->input_token);
772 err = gssx_enc_cb(xdr, arg->input_cb);
776 err = gssx_enc_bool(xdr, 0);
779 err = gssx_enc_bool(xdr, arg->ret_deleg_cred);
783 /* leave options empty for now, will add once we have any options
784 * to pass up at all */
786 err = dummy_enc_opt_array(xdr, &arg->options);
790 dprintk("RPC: gssx_enc_accept_sec_context: %d\n", err);
793 int gssx_dec_accept_sec_context(struct rpc_rqst *rqstp,
794 struct xdr_stream *xdr,
795 struct gssx_res_accept_sec_context *res)
800 err = gssx_dec_status(xdr, &res->status);
804 /* res->context_handle */
805 if (gssx_check_pointer(xdr)) {
806 err = gssx_dec_ctx(xdr, res->context_handle);
810 res->context_handle = NULL;
813 /* res->output_token */
814 if (gssx_check_pointer(xdr)) {
815 err = gssx_dec_buffer(xdr, res->output_token);
819 res->output_token = NULL;
822 /* res->delegated_cred_handle */
823 if (gssx_check_pointer(xdr)) {
824 /* we do not support upcall servers sending this data. */
829 err = gssx_dec_option_array(xdr, &res->options);