1.2.2rc2
[heimdal.git] / lib / gssapi / test_acquire_cred.c
blob120c86597a142c6ca1b7a02065c19304f74fa672
1 /*
2 * Copyright (c) 2003-2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of KTH nor the names of its contributors may be
18 * used to endorse or promote products derived from this software without
19 * specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <stdarg.h>
42 #include <gssapi.h>
43 #include <err.h>
44 #include <roken.h>
45 #include <getarg.h>
47 #include "test_common.h"
49 RCSID("$Id$");
51 static void
52 print_time(OM_uint32 time_rec)
54 if (time_rec == GSS_C_INDEFINITE) {
55 printf("cred never expire\n");
56 } else {
57 time_t t = time_rec + time(NULL);
58 printf("expiration time: %s", ctime(&t));
62 #if 0
64 static void
65 test_add(gss_cred_id_t cred_handle)
67 OM_uint32 major_status, minor_status;
68 gss_cred_id_t copy_cred;
69 OM_uint32 time_rec;
71 major_status = gss_add_cred (&minor_status,
72 cred_handle,
73 GSS_C_NO_NAME,
74 GSS_KRB5_MECHANISM,
75 GSS_C_INITIATE,
78 &copy_cred,
79 NULL,
80 &time_rec,
81 NULL);
83 if (GSS_ERROR(major_status))
84 errx(1, "add_cred failed");
86 print_time(time_rec);
88 major_status = gss_release_cred(&minor_status,
89 &copy_cred);
90 if (GSS_ERROR(major_status))
91 errx(1, "release_cred failed");
94 static void
95 copy_cred(void)
97 OM_uint32 major_status, minor_status;
98 gss_cred_id_t cred_handle;
99 OM_uint32 time_rec;
101 major_status = gss_acquire_cred(&minor_status,
102 GSS_C_NO_NAME,
104 NULL,
105 GSS_C_INITIATE,
106 &cred_handle,
107 NULL,
108 &time_rec);
109 if (GSS_ERROR(major_status))
110 errx(1, "acquire_cred failed");
112 print_time(time_rec);
114 test_add(cred_handle);
115 test_add(cred_handle);
116 test_add(cred_handle);
118 major_status = gss_release_cred(&minor_status,
119 &cred_handle);
120 if (GSS_ERROR(major_status))
121 errx(1, "release_cred failed");
123 #endif
125 static void
126 acquire_cred_service(const char *service,
127 gss_OID nametype,
128 int flags)
130 OM_uint32 major_status, minor_status;
131 gss_cred_id_t cred_handle;
132 OM_uint32 time_rec;
133 gss_buffer_desc name_buffer;
134 gss_name_t name = GSS_C_NO_NAME;
136 if (service) {
137 name_buffer.value = rk_UNCONST(service);
138 name_buffer.length = strlen(service);
140 major_status = gss_import_name(&minor_status,
141 &name_buffer,
142 nametype,
143 &name);
144 if (GSS_ERROR(major_status))
145 errx(1, "import_name failed");
148 major_status = gss_acquire_cred(&minor_status,
149 name,
151 NULL,
152 flags,
153 &cred_handle,
154 NULL,
155 &time_rec);
156 if (GSS_ERROR(major_status)) {
157 warnx("acquire_cred failed: %s",
158 gssapi_err(major_status, minor_status, GSS_C_NO_OID));
159 } else {
160 print_time(time_rec);
161 gss_release_cred(&minor_status, &cred_handle);
164 if (name != GSS_C_NO_NAME)
165 gss_release_name(&minor_status, &name);
167 if (GSS_ERROR(major_status))
168 exit(1);
171 static int version_flag = 0;
172 static int help_flag = 0;
173 static char *acquire_name;
174 static char *acquire_type;
175 static char *name_type;
176 static char *ccache;
178 static struct getargs args[] = {
179 {"acquire-name", 0, arg_string, &acquire_name, "name", NULL },
180 {"acquire-type", 0, arg_string, &acquire_type, "type", NULL },
181 {"ccache", 0, arg_string, &ccache, "name", NULL },
182 {"name-type", 0, arg_string, &name_type, "type", NULL },
183 {"version", 0, arg_flag, &version_flag, "print version", NULL },
184 {"help", 0, arg_flag, &help_flag, NULL, NULL }
187 static void
188 usage (int ret)
190 arg_printusage (args, sizeof(args)/sizeof(*args), NULL, "");
191 exit (ret);
195 main(int argc, char **argv)
197 int optidx = 0;
198 OM_uint32 flag;
199 gss_OID type;
201 setprogname(argv[0]);
202 if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
203 usage(1);
205 if (help_flag)
206 usage (0);
208 if(version_flag){
209 print_version(NULL);
210 exit(0);
213 argc -= optidx;
214 argv += optidx;
216 if (argc != 0)
217 usage(1);
219 if (acquire_type) {
220 if (strcasecmp(acquire_type, "both") == 0)
221 flag = GSS_C_BOTH;
222 else if (strcasecmp(acquire_type, "accept") == 0)
223 flag = GSS_C_ACCEPT;
224 else if (strcasecmp(acquire_type, "initiate") == 0)
225 flag = GSS_C_INITIATE;
226 else
227 errx(1, "unknown type %s", acquire_type);
228 } else
229 flag = GSS_C_ACCEPT;
231 if (name_type) {
232 if (strcasecmp("hostbased-service", name_type) == 0)
233 type = GSS_C_NT_HOSTBASED_SERVICE;
234 else if (strcasecmp("user-name", name_type) == 0)
235 type = GSS_C_NT_USER_NAME;
236 else
237 errx(1, "unknown name type %s", name_type);
238 } else
239 type = GSS_C_NT_HOSTBASED_SERVICE;
241 if (ccache) {
242 OM_uint32 major_status, minor_status;
243 major_status = gss_krb5_ccache_name(&minor_status,
244 ccache, NULL);
245 if (GSS_ERROR(major_status))
246 errx(1, "gss_krb5_ccache_name %s",
247 gssapi_err(major_status, minor_status, GSS_C_NO_OID));
250 acquire_cred_service(acquire_name, type, flag);
252 return 0;