From f0134201eeda3b933bc4c73a02dd3c667d6d7965 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Love=20H=C3=B6rnquist=20=C3=85strand?= Date: Thu, 12 Oct 2006 11:15:26 +0000 Subject: [PATCH] a tests for gss lucid interface git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@18423 ec53bebd-3082-4978-b11e-865c3cabbd6b --- lib/gssapi/test_context.c | 189 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 189 insertions(+) create mode 100644 lib/gssapi/test_context.c diff --git a/lib/gssapi/test_context.c b/lib/gssapi/test_context.c new file mode 100644 index 000000000..1cc542250 --- /dev/null +++ b/lib/gssapi/test_context.c @@ -0,0 +1,189 @@ +/* + * Copyright (c) 2004 Kungliga Tekniska Högskolan + * (Royal Institute of Technology, Stockholm, Sweden). + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of KTH nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "krb5/gsskrb5_locl.h" +#include +#include + +RCSID("$Id$"); + +static void +loop(const char *target, gss_ctx_id_t *sctx, gss_ctx_id_t *cctx) +{ + int server_done = 0, client_done = 0; + OM_uint32 maj_stat, min_stat; + gss_name_t gss_target_name; + gss_buffer_desc input_token, output_token; + OM_uint32 flags = 0, ret_cflags, ret_sflags; + gss_cred_id_t deleg_cred = GSS_C_NO_CREDENTIAL; + + input_token.value = rk_UNCONST(target); + input_token.length = strlen(target); + + + maj_stat = gss_import_name(&min_stat, + &input_token, + GSS_KRB5_NT_PRINCIPAL_NAME, + &gss_target_name); + if (GSS_ERROR(maj_stat)) + err(1, "import name creds failed with: %d", maj_stat); + + input_token.length = 0; + input_token.value = NULL; + + while (!server_done && !client_done) { + + maj_stat = gss_init_sec_context(&min_stat, + GSS_C_NO_CREDENTIAL, + cctx, + gss_target_name, + GSS_KRB5_MECHANISM, + flags, + 0, + NULL, + &input_token, + NULL, + &output_token, + &ret_cflags, + NULL); + if (GSS_ERROR(maj_stat)) + errx(1, "init_sec_context: %d", (int)maj_stat); + if (maj_stat & GSS_S_CONTINUE_NEEDED) + ; + else + client_done = 1; + + if (input_token.length != 0) + gss_release_buffer(&min_stat, &input_token); + + maj_stat = gss_accept_sec_context(&min_stat, + sctx, + GSS_C_NO_CREDENTIAL, + &output_token, + GSS_C_NO_CHANNEL_BINDINGS, + NULL, + NULL, + &input_token, + &ret_sflags, + NULL, + &deleg_cred); + if (GSS_ERROR(maj_stat)) + ; + + if (input_token.length != 0) + gss_release_buffer(&min_stat, &input_token); + + if (maj_stat & GSS_S_CONTINUE_NEEDED) + ; + else + server_done = 1; + } + if (output_token.length != 0) + gss_release_buffer(&min_stat, &output_token); + if (input_token.length != 0) + gss_release_buffer(&min_stat, &input_token); + +} + + +static int version_flag = 0; +static int help_flag = 0; + +static struct getargs args[] = { + {"version", 0, arg_flag, &version_flag, "print version", NULL }, + {"help", 0, arg_flag, &help_flag, NULL, NULL } +}; + +static void +usage (int ret) +{ + arg_printusage (args, sizeof(args)/sizeof(*args), + NULL, "service@host"); + exit (ret); +} + +int +main(int argc, char **argv) +{ + int optind = 0; + OM_uint32 min_stat, maj_stat; + gss_ctx_id_t cctx, sctx; + void *ctx; + + cctx = sctx = GSS_C_NO_CONTEXT; + + if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optind)) + usage(1); + + if (help_flag) + usage (0); + + if(version_flag){ + print_version(NULL); + exit(0); + } + + argc -= optind; + argv += optind; + + if (argc != 1) + usage(1); + + loop(argv[0], &sctx, &cctx); + + /* client */ + maj_stat = gss_krb5_export_lucid_sec_context(&min_stat, + &cctx, + 1, /* version */ + &ctx); + if (maj_stat != GSS_S_COMPLETE) + errx(1, "gss_krb5_export_lucid_sec_context failed"); + + + maj_stat = gss_krb5_free_lucid_sec_context(&maj_stat, ctx); + if (maj_stat != GSS_S_COMPLETE) + errx(1, "gss_krb5_free_lucid_sec_context failed"); + + /* server */ + maj_stat = gss_krb5_export_lucid_sec_context(&min_stat, + &sctx, + 1, /* version */ + &ctx); + if (maj_stat != GSS_S_COMPLETE) + errx(1, "gss_krb5_export_lucid_sec_context failed"); + maj_stat = gss_krb5_free_lucid_sec_context(&maj_stat, ctx); + if (maj_stat != GSS_S_COMPLETE) + errx(1, "gss_krb5_free_lucid_sec_context failed"); + + return 0; +}