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