Home | History | Annotate | Line # | Download | only in gssapi
      1  1.1     elric /*	$NetBSD: test_kcred.c,v 1.2 2017/01/28 21:31:46 christos Exp $	*/
      2  1.1     elric 
      3  1.1     elric /*
      4  1.1     elric  * Copyright (c) 2003-2004 Kungliga Tekniska Hgskolan
      5  1.1     elric  * (Royal Institute of Technology, Stockholm, Sweden).
      6  1.1     elric  * All rights reserved.
      7  1.1     elric  *
      8  1.1     elric  * Redistribution and use in source and binary forms, with or without
      9  1.1     elric  * modification, are permitted provided that the following conditions
     10  1.1     elric  * are met:
     11  1.1     elric  *
     12  1.1     elric  * 1. Redistributions of source code must retain the above copyright
     13  1.1     elric  *    notice, this list of conditions and the following disclaimer.
     14  1.1     elric  *
     15  1.1     elric  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1     elric  *    notice, this list of conditions and the following disclaimer in the
     17  1.1     elric  *    documentation and/or other materials provided with the distribution.
     18  1.1     elric  *
     19  1.1     elric  * 3. Neither the name of KTH nor the names of its contributors may be
     20  1.1     elric  *    used to endorse or promote products derived from this software without
     21  1.1     elric  *    specific prior written permission.
     22  1.1     elric  *
     23  1.1     elric  * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
     24  1.1     elric  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1     elric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  1.1     elric  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
     27  1.1     elric  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  1.1     elric  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  1.1     elric  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
     30  1.1     elric  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     31  1.1     elric  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     32  1.1     elric  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     33  1.1     elric  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     34  1.1     elric  */
     35  1.1     elric 
     36  1.1     elric #ifdef HAVE_CONFIG_H
     37  1.1     elric #include <config.h>
     38  1.1     elric #endif
     39  1.1     elric 
     40  1.1     elric #include <krb5/roken.h>
     41  1.1     elric #include <stdio.h>
     42  1.1     elric #include <stdlib.h>
     43  1.1     elric #include <string.h>
     44  1.1     elric #include <stdarg.h>
     45  1.1     elric #include <gssapi/gssapi.h>
     46  1.1     elric #include <gssapi/gssapi_krb5.h>
     47  1.1     elric #include <gssapi/gssapi_spnego.h>
     48  1.1     elric #include <krb5/krb5.h>
     49  1.1     elric #include <err.h>
     50  1.1     elric #include <krb5/getarg.h>
     51  1.1     elric 
     52  1.1     elric static int version_flag = 0;
     53  1.1     elric static int help_flag	= 0;
     54  1.1     elric 
     55  1.1     elric static void
     56  1.1     elric copy_import(void)
     57  1.1     elric {
     58  1.1     elric     gss_cred_id_t cred1, cred2;
     59  1.1     elric     OM_uint32 maj_stat, min_stat;
     60  1.1     elric     gss_name_t name1, name2;
     61  1.1     elric     OM_uint32 lifetime1, lifetime2;
     62  1.1     elric     gss_cred_usage_t usage1, usage2;
     63  1.1     elric     gss_OID_set mechs1, mechs2;
     64  1.1     elric     krb5_ccache id;
     65  1.1     elric     krb5_error_code ret;
     66  1.1     elric     krb5_context context;
     67  1.1     elric     int equal;
     68  1.1     elric 
     69  1.1     elric     maj_stat = gss_acquire_cred(&min_stat, GSS_C_NO_NAME, GSS_C_INDEFINITE,
     70  1.1     elric 				GSS_C_NO_OID_SET, GSS_C_INITIATE,
     71  1.1     elric 				&cred1, NULL, NULL);
     72  1.1     elric     if (maj_stat != GSS_S_COMPLETE)
     73  1.1     elric 	errx(1, "gss_acquire_cred");
     74  1.1     elric 
     75  1.1     elric     maj_stat = gss_inquire_cred(&min_stat, cred1, &name1, &lifetime1,
     76  1.1     elric 				&usage1, &mechs1);
     77  1.1     elric     if (maj_stat != GSS_S_COMPLETE)
     78  1.1     elric 	errx(1, "gss_inquire_cred");
     79  1.1     elric 
     80  1.1     elric     ret = krb5_init_context(&context);
     81  1.1     elric     if (ret)
     82  1.1     elric 	errx(1, "krb5_init_context");
     83  1.1     elric 
     84  1.1     elric     ret = krb5_cc_new_unique(context, krb5_cc_type_memory, NULL, &id);
     85  1.1     elric     if (ret)
     86  1.1     elric 	krb5_err(context, 1, ret, "krb5_cc_new_unique");
     87  1.1     elric 
     88  1.1     elric     maj_stat = gss_krb5_copy_ccache(&min_stat, cred1, id);
     89  1.1     elric     if (maj_stat != GSS_S_COMPLETE)
     90  1.1     elric 	errx(1, "gss_krb5_copy_ccache");
     91  1.1     elric 
     92  1.1     elric     maj_stat = gss_krb5_import_cred(&min_stat, id, NULL, NULL, &cred2);
     93  1.1     elric     if (maj_stat != GSS_S_COMPLETE)
     94  1.1     elric 	errx(1, "gss_krb5_import_cred");
     95  1.1     elric 
     96  1.1     elric     maj_stat = gss_inquire_cred(&min_stat, cred2, &name2, &lifetime2,
     97  1.1     elric 				&usage2, &mechs2);
     98  1.1     elric     if (maj_stat != GSS_S_COMPLETE)
     99  1.1     elric 	errx(1, "gss_inquire_cred 2");
    100  1.1     elric 
    101  1.1     elric     maj_stat = gss_compare_name(&min_stat, name1, name2, &equal);
    102  1.1     elric     if (maj_stat != GSS_S_COMPLETE)
    103  1.1     elric 	errx(1, "gss_compare_name");
    104  1.1     elric     if (!equal)
    105  1.1     elric 	errx(1, "names not equal");
    106  1.2  christos 
    107  1.1     elric     if (lifetime1 != lifetime2)
    108  1.1     elric 	errx(1, "lifetime not equal %lu != %lu",
    109  1.1     elric 	     (unsigned long)lifetime1, (unsigned long)lifetime2);
    110  1.1     elric 
    111  1.1     elric     if (usage1 != usage2) {
    112  1.1     elric 	/* as long any of them is both are everything it ok */
    113  1.1     elric 	if (usage1 != GSS_C_BOTH && usage2 != GSS_C_BOTH)
    114  1.1     elric 	    errx(1, "usages disjoined");
    115  1.1     elric     }
    116  1.1     elric 
    117  1.1     elric     gss_release_name(&min_stat, &name2);
    118  1.1     elric     gss_release_oid_set(&min_stat, &mechs2);
    119  1.1     elric 
    120  1.1     elric     maj_stat = gss_inquire_cred(&min_stat, cred2, &name2, &lifetime2,
    121  1.1     elric 				&usage2, &mechs2);
    122  1.1     elric     if (maj_stat != GSS_S_COMPLETE)
    123  1.1     elric 	errx(1, "gss_inquire_cred");
    124  1.1     elric 
    125  1.1     elric     maj_stat = gss_compare_name(&min_stat, name1, name2, &equal);
    126  1.1     elric     if (maj_stat != GSS_S_COMPLETE)
    127  1.1     elric 	errx(1, "gss_compare_name");
    128  1.1     elric     if (!equal)
    129  1.1     elric 	errx(1, "names not equal");
    130  1.2  christos 
    131  1.1     elric     if (lifetime1 != lifetime2)
    132  1.1     elric 	errx(1, "lifetime not equal %lu != %lu",
    133  1.1     elric 	     (unsigned long)lifetime1, (unsigned long)lifetime2);
    134  1.1     elric 
    135  1.1     elric     gss_release_cred(&min_stat, &cred1);
    136  1.1     elric     gss_release_cred(&min_stat, &cred2);
    137  1.1     elric 
    138  1.1     elric     gss_release_name(&min_stat, &name1);
    139  1.1     elric     gss_release_name(&min_stat, &name2);
    140  1.1     elric 
    141  1.1     elric #if 0
    142  1.1     elric     compare(mechs1, mechs2);
    143  1.1     elric #endif
    144  1.1     elric 
    145  1.1     elric     gss_release_oid_set(&min_stat, &mechs1);
    146  1.1     elric     gss_release_oid_set(&min_stat, &mechs2);
    147  1.1     elric 
    148  1.1     elric     krb5_cc_destroy(context, id);
    149  1.1     elric     krb5_free_context(context);
    150  1.1     elric }
    151  1.1     elric 
    152  1.1     elric static struct getargs args[] = {
    153  1.1     elric     {"version",	0,	arg_flag,	&version_flag, "print version", NULL },
    154  1.1     elric     {"help",	0,	arg_flag,	&help_flag,  NULL, NULL }
    155  1.1     elric };
    156  1.1     elric 
    157  1.1     elric static void
    158  1.1     elric usage (int ret)
    159  1.1     elric {
    160  1.1     elric     arg_printusage (args, sizeof(args)/sizeof(*args),
    161  1.1     elric 		    NULL, "");
    162  1.1     elric     exit (ret);
    163  1.1     elric }
    164  1.1     elric 
    165  1.1     elric int
    166  1.1     elric main(int argc, char **argv)
    167  1.1     elric {
    168  1.1     elric     int optidx = 0;
    169  1.1     elric 
    170  1.1     elric     setprogname(argv[0]);
    171  1.1     elric     if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
    172  1.1     elric 	usage(1);
    173  1.1     elric 
    174  1.1     elric     if (help_flag)
    175  1.1     elric 	usage (0);
    176  1.1     elric 
    177  1.1     elric     if(version_flag){
    178  1.1     elric 	print_version(NULL);
    179  1.1     elric 	exit(0);
    180  1.1     elric     }
    181  1.1     elric 
    182  1.1     elric     argc -= optidx;
    183  1.1     elric     argv += optidx;
    184  1.1     elric 
    185  1.1     elric     copy_import();
    186  1.1     elric 
    187  1.1     elric     return 0;
    188  1.1     elric }
    189