handle new syntax for ccache's
[heimdal.git] / kdc / kdc-tester.c
blobb449b4df74b502337d4c8326a174fdd1be3bd1c9
1 /*
2 * Copyright (c) 1997-2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
36 #include "kdc_locl.h"
38 struct perf {
39 unsigned long as_req;
40 unsigned long tgs_req;
41 struct timeval start;
42 struct timeval stop;
43 struct perf *next;
44 } *ptop;
46 #ifdef SUPPORT_DETACH
47 int detach_from_console = -1;
48 #define DETACH_IS_DEFAULT FALSE
49 #endif
51 static krb5_kdc_configuration *kdc_config;
52 static krb5_context kdc_context;
54 static struct sockaddr_storage sa;
55 static const char *astr = "0.0.0.0";
57 static void eval_object(heim_object_t);
64 static krb5_error_code
65 send_to_kdc(krb5_context c, void *ptr, krb5_krbhst_info *hi, time_t timeout,
66 const krb5_data *in, krb5_data *out)
68 krb5_error_code ret;
70 krb5_kdc_update_time(NULL);
72 ret = krb5_kdc_process_request(kdc_context, kdc_config,
73 in->data, in->length,
74 out, NULL, astr,
75 (struct sockaddr *)&sa, 0);
76 if (ret)
77 krb5_err(c, 1, ret, "krb5_kdc_process_request");
79 return 0;
86 static void
87 eval_repeat(heim_dict_t o)
89 heim_object_t or = heim_dict_get_value(o, HSTR("value"));
90 heim_number_t n = heim_dict_get_value(o, HSTR("num"));
91 int i, num;
92 struct perf perf;
94 memset(&perf, 0, sizeof(perf));
96 gettimeofday(&perf.start, NULL);
97 perf.next = ptop;
98 ptop = &perf;
100 heim_assert(or != NULL, "value missing");
101 heim_assert(n != NULL, "num missing");
103 num = heim_number_get_int(n);
104 heim_assert(num >= 0, "num >= 0");
106 for (i = 0; i < num; i++)
107 eval_object(or);
109 gettimeofday(&perf.stop, NULL);
110 ptop = perf.next;
112 if (ptop) {
113 ptop->as_req += perf.as_req;
114 ptop->tgs_req += perf.tgs_req;
117 timevalsub(&perf.stop, &perf.start);
118 printf("time: %lu.%06lu\n",
119 (unsigned long)perf.stop.tv_sec,
120 (unsigned long)perf.stop.tv_usec);
122 #define USEC_PER_SEC 1000000
124 if (perf.as_req) {
125 double as_ps = 0.0;
126 as_ps = (perf.as_req * USEC_PER_SEC) / (double)((perf.stop.tv_sec * USEC_PER_SEC) + perf.stop.tv_usec);
127 printf("as-req/s %.2lf\n", as_ps);
130 if (perf.tgs_req) {
131 double tgs_ps = 0.0;
132 tgs_ps = (perf.tgs_req * USEC_PER_SEC) / (double)((perf.stop.tv_sec * USEC_PER_SEC) + perf.stop.tv_usec);
133 printf("tgs-req/s %.2lf\n", tgs_ps);
141 static void
142 eval_kinit(heim_dict_t o)
144 heim_string_t user, password, keytab, fast_armor_cc, pk_user_id, ccache;
145 krb5_get_init_creds_opt *opt;
146 krb5_init_creds_context ctx;
147 krb5_principal client;
148 krb5_keytab kt = NULL;
149 krb5_ccache fast_cc = NULL;
150 krb5_error_code ret;
152 if (ptop)
153 ptop->as_req++;
155 user = heim_dict_get_value(o, HSTR("client"));
156 if (user == NULL)
157 krb5_errx(kdc_context, 1, "no client");
159 password = heim_dict_get_value(o, HSTR("password"));
160 keytab = heim_dict_get_value(o, HSTR("keytab"));
161 pk_user_id = heim_dict_get_value(o, HSTR("pkinit-user-cert-id"));
162 if (password == NULL && keytab == NULL && pk_user_id == NULL)
163 krb5_errx(kdc_context, 1, "password, keytab, nor PKINIT user cert ID");
165 ccache = heim_dict_get_value(o, HSTR("ccache"));
167 ret = krb5_parse_name(kdc_context, heim_string_get_utf8(user), &client);
168 if (ret)
169 krb5_err(kdc_context, 1, ret, "krb5_unparse_name");
171 /* PKINIT parts */
172 ret = krb5_get_init_creds_opt_alloc (kdc_context, &opt);
173 if (ret)
174 krb5_err(kdc_context, 1, ret, "krb5_get_init_creds_opt_alloc");
176 if (pk_user_id) {
177 ret = krb5_get_init_creds_opt_set_pkinit(kdc_context, opt,
178 client,
179 heim_string_get_utf8(pk_user_id),
180 NULL, NULL, NULL, 0,
181 NULL, NULL, NULL);
182 if (ret)
183 krb5_err(kdc_context, 1, ret, "krb5_get_init_creds_opt_set_pkinit");
186 ret = krb5_init_creds_init(kdc_context, client, NULL, NULL, 0, opt, &ctx);
187 if (ret)
188 krb5_err(kdc_context, 1, ret, "krb5_init_creds_init");
190 fast_armor_cc = heim_dict_get_value(o, HSTR("fast-armor-cc"));
191 if (fast_armor_cc) {
193 ret = krb5_cc_resolve(kdc_context, heim_string_get_utf8(fast_armor_cc), &fast_cc);
194 if (ret)
195 krb5_err(kdc_context, 1, ret, "krb5_cc_resolve");
197 ret = krb5_init_creds_set_fast_ccache(kdc_context, ctx, fast_cc);
198 if (ret)
199 krb5_err(kdc_context, 1, ret, "krb5_init_creds_set_fast_ccache");
202 if (password) {
203 ret = krb5_init_creds_set_password(kdc_context, ctx,
204 heim_string_get_utf8(password));
205 if (ret)
206 krb5_err(kdc_context, 1, ret, "krb5_init_creds_set_password");
208 if (keytab) {
209 ret = krb5_kt_resolve(kdc_context, heim_string_get_utf8(keytab), &kt);
210 if (ret)
211 krb5_err(kdc_context, 1, ret, "krb5_kt_resolve");
213 ret = krb5_init_creds_set_keytab(kdc_context, ctx, kt);
214 if (ret)
215 krb5_err(kdc_context, 1, ret, "krb5_init_creds_set_keytab");
218 ret = krb5_init_creds_get(kdc_context, ctx);
219 if (ret)
220 krb5_err(kdc_context, 1, ret, "krb5_init_creds_get");
222 if (ccache) {
223 const char *name = heim_string_get_utf8(ccache);
224 krb5_creds cred;
225 krb5_ccache cc;
227 ret = krb5_init_creds_get_creds(kdc_context, ctx, &cred);
228 if (ret)
229 krb5_err(kdc_context, 1, ret, "krb5_init_creds_get_creds");
231 ret = krb5_cc_resolve(kdc_context, name, &cc);
232 if (ret)
233 krb5_err(kdc_context, 1, ret, "krb5_cc_resolve");
235 krb5_init_creds_store(kdc_context, ctx, cc);
237 ret = krb5_cc_close(kdc_context, cc);
238 if (ret)
239 krb5_err(kdc_context, 1, ret, "krb5_cc_close");
241 krb5_free_cred_contents(kdc_context, &cred);
244 krb5_init_creds_free(kdc_context, ctx);
246 if (kt)
247 krb5_kt_close(kdc_context, kt);
248 if (fast_cc)
249 krb5_cc_close(kdc_context, fast_cc);
251 #if 0
252 printf("kinit success %s\n", heim_string_get_utf8(user));
253 #endif
261 static void
262 eval_kdestroy(heim_dict_t o)
264 heim_string_t ccache = heim_dict_get_value(o, HSTR("ccache"));;
265 krb5_error_code ret;
266 const char *name;
267 krb5_ccache cc;
269 heim_assert(ccache != NULL, "ccache_missing");
271 name = heim_string_get_utf8(ccache);
273 ret = krb5_cc_resolve(kdc_context, name, &cc);
274 if (ret)
275 krb5_err(kdc_context, 1, ret, "krb5_cc_resolve");
277 krb5_cc_destroy(kdc_context, cc);
285 static void
286 eval_array_element(heim_object_t o, void *ptr)
288 eval_object(o);
291 static void
292 eval_object(heim_object_t o)
294 heim_tid_t t = heim_get_tid(o);
296 if (t == heim_array_get_type_id()) {
297 heim_array_iterate_f(o, NULL, eval_array_element);
298 } else if (t == heim_dict_get_type_id()) {
299 const char *op = heim_dict_get_value(o, HSTR("op"));
301 heim_assert(op != NULL, "op missing");
303 if (strcmp(op, "repeat") == 0) {
304 eval_repeat(o);
305 } else if (strcmp(op, "kinit") == 0) {
306 eval_kinit(o);
307 } else if (strcmp(op, "kdestroy") == 0) {
308 eval_kdestroy(o);
309 } else {
310 errx(1, "unsupported ops %s", op);
313 } else
314 errx(1, "unsupported");
319 main(int argc, char **argv)
321 krb5_error_code ret;
322 int optidx = 0;
324 setprogname(argv[0]);
326 ret = krb5_init_context(&kdc_context);
327 if (ret == KRB5_CONFIG_BADFORMAT)
328 errx (1, "krb5_init_context failed to parse configuration file");
329 else if (ret)
330 errx (1, "krb5_init_context failed: %d", ret);
332 ret = krb5_kt_register(kdc_context, &hdb_kt_ops);
333 if (ret)
334 errx (1, "krb5_kt_register(HDB) failed: %d", ret);
336 kdc_config = configure(kdc_context, argc, argv, &optidx);
338 argc -= optidx;
339 argv += optidx;
341 if (argc == 0)
342 errx(1, "missing operations");
344 krb5_set_send_to_kdc_func(kdc_context, send_to_kdc, NULL);
347 void *buf;
348 size_t size;
349 heim_object_t o;
351 if (rk_undumpdata(argv[0], &buf, &size))
352 errx(1, "undumpdata: %s", argv[0]);
354 o = heim_json_create_with_bytes(buf, size, NULL);
355 free(buf);
356 if (o == NULL)
357 errx(1, "heim_json");
360 * do the work here
363 eval_object(o);
365 heim_release(o);
368 krb5_free_context(kdc_context);
369 return 0;