lib/roken: is_special_auxv_p test for AT_HWCAP2
[heimdal.git] / kdc / kdc-tester.c
blobcbe4b1c00326fe4d23daecafc5c8da9c4eb4e196
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"
37 #include "send_to_kdc_plugin.h"
39 struct perf {
40 unsigned long as_req;
41 unsigned long tgs_req;
42 struct timeval start;
43 struct timeval stop;
44 struct perf *next;
45 } *ptop;
47 int detach_from_console = -1;
48 int daemon_child = -1;
49 int do_bonjour = -1;
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 plugin_init(krb5_context context, void **pctx)
67 *pctx = NULL;
68 return 0;
71 static void
72 plugin_fini(void *ctx)
76 static krb5_error_code
77 plugin_send_to_kdc(krb5_context context,
78 void *ctx,
79 krb5_krbhst_info *ho,
80 time_t timeout,
81 const krb5_data *in,
82 krb5_data *out)
84 return KRB5_PLUGIN_NO_HANDLE;
87 static krb5_error_code
88 plugin_send_to_realm(krb5_context context,
89 void *ctx,
90 krb5_const_realm realm,
91 time_t timeout,
92 const krb5_data *in,
93 krb5_data *out)
95 int ret;
97 krb5_kdc_update_time(NULL);
99 ret = krb5_kdc_process_request(kdc_context, kdc_config,
100 in->data, in->length,
101 out, NULL, astr,
102 (struct sockaddr *)&sa, 0);
103 if (ret)
104 krb5_err(kdc_context, 1, ret, "krb5_kdc_process_request");
106 return 0;
109 static krb5plugin_send_to_kdc_ftable send_to_kdc = {
110 KRB5_PLUGIN_SEND_TO_KDC_VERSION_2,
111 plugin_init,
112 plugin_fini,
113 plugin_send_to_kdc,
114 plugin_send_to_realm
117 static void
118 perf_start(struct perf *perf)
120 memset(perf, 0, sizeof(*perf));
122 gettimeofday(&perf->start, NULL);
123 perf->next = ptop;
124 ptop = perf;
127 static void
128 perf_stop(struct perf *perf)
130 gettimeofday(&perf->stop, NULL);
131 ptop = perf->next;
133 if (ptop) {
134 ptop->as_req += perf->as_req;
135 ptop->tgs_req += perf->tgs_req;
138 timevalsub(&perf->stop, &perf->start);
139 printf("time: %lu.%06lu\n",
140 (unsigned long)perf->stop.tv_sec,
141 (unsigned long)perf->stop.tv_usec);
143 #define USEC_PER_SEC 1000000
145 if (perf->as_req) {
146 double as_ps = 0.0;
147 as_ps = (perf->as_req * USEC_PER_SEC) / (double)((perf->stop.tv_sec * USEC_PER_SEC) + perf->stop.tv_usec);
148 printf("as-req/s %.2lf (total %lu requests)\n", as_ps, perf->as_req);
151 if (perf->tgs_req) {
152 double tgs_ps = 0.0;
153 tgs_ps = (perf->tgs_req * USEC_PER_SEC) / (double)((perf->stop.tv_sec * USEC_PER_SEC) + perf->stop.tv_usec);
154 printf("tgs-req/s %.2lf (total %lu requests)\n", tgs_ps, perf->tgs_req);
162 static void
163 eval_repeat(heim_dict_t o)
165 heim_object_t or = heim_dict_get_value(o, HSTR("value"));
166 heim_number_t n = heim_dict_get_value(o, HSTR("num"));
167 int i, num;
168 struct perf perf;
170 perf_start(&perf);
172 heim_assert(or != NULL, "value missing");
173 heim_assert(n != NULL, "num missing");
175 num = heim_number_get_int(n);
176 heim_assert(num >= 0, "num >= 0");
178 for (i = 0; i < num; i++)
179 eval_object(or);
181 perf_stop(&perf);
188 static krb5_error_code
189 copy_keytab(krb5_context context, krb5_keytab from, krb5_keytab to)
191 krb5_keytab_entry entry;
192 krb5_kt_cursor cursor;
193 krb5_error_code ret;
195 ret = krb5_kt_start_seq_get(context, from, &cursor);
196 if (ret)
197 return ret;
198 while((ret = krb5_kt_next_entry(context, from, &entry, &cursor)) == 0){
199 krb5_kt_add_entry(context, to, &entry);
200 krb5_kt_free_entry(context, &entry);
202 return krb5_kt_end_seq_get(context, from, &cursor);
209 static void
210 eval_kinit(heim_dict_t o)
212 heim_string_t user, password, keytab, fast_armor_cc, pk_user_id, ccache;
213 krb5_get_init_creds_opt *opt;
214 krb5_init_creds_context ctx;
215 krb5_principal client;
216 krb5_keytab ktmem = NULL;
217 krb5_ccache fast_cc = NULL;
218 krb5_error_code ret;
220 if (ptop)
221 ptop->as_req++;
223 user = heim_dict_get_value(o, HSTR("client"));
224 if (user == NULL)
225 krb5_errx(kdc_context, 1, "no client");
227 password = heim_dict_get_value(o, HSTR("password"));
228 keytab = heim_dict_get_value(o, HSTR("keytab"));
229 pk_user_id = heim_dict_get_value(o, HSTR("pkinit-user-cert-id"));
230 if (password == NULL && keytab == NULL && pk_user_id == NULL)
231 krb5_errx(kdc_context, 1, "password, keytab, nor PKINIT user cert ID");
233 ccache = heim_dict_get_value(o, HSTR("ccache"));
235 ret = krb5_parse_name(kdc_context, heim_string_get_utf8(user), &client);
236 if (ret)
237 krb5_err(kdc_context, 1, ret, "krb5_unparse_name");
239 /* PKINIT parts */
240 ret = krb5_get_init_creds_opt_alloc (kdc_context, &opt);
241 if (ret)
242 krb5_err(kdc_context, 1, ret, "krb5_get_init_creds_opt_alloc");
244 if (pk_user_id) {
245 heim_bool_t rsaobj = heim_dict_get_value(o, HSTR("pkinit-use-rsa"));
246 int use_rsa = rsaobj ? heim_bool_val(rsaobj) : 0;
248 ret = krb5_get_init_creds_opt_set_pkinit(kdc_context, opt,
249 client,
250 heim_string_get_utf8(pk_user_id),
251 NULL, NULL, NULL,
252 use_rsa ? 2 : 0,
253 NULL, NULL, NULL);
254 if (ret)
255 krb5_err(kdc_context, 1, ret, "krb5_get_init_creds_opt_set_pkinit");
258 ret = krb5_init_creds_init(kdc_context, client, NULL, NULL, 0, opt, &ctx);
259 if (ret)
260 krb5_err(kdc_context, 1, ret, "krb5_init_creds_init");
262 fast_armor_cc = heim_dict_get_value(o, HSTR("fast-armor-cc"));
263 if (fast_armor_cc) {
265 ret = krb5_cc_resolve(kdc_context, heim_string_get_utf8(fast_armor_cc), &fast_cc);
266 if (ret)
267 krb5_err(kdc_context, 1, ret, "krb5_cc_resolve");
269 ret = krb5_init_creds_set_fast_ccache(kdc_context, ctx, fast_cc);
270 if (ret)
271 krb5_err(kdc_context, 1, ret, "krb5_init_creds_set_fast_ccache");
274 if (password) {
275 ret = krb5_init_creds_set_password(kdc_context, ctx,
276 heim_string_get_utf8(password));
277 if (ret)
278 krb5_err(kdc_context, 1, ret, "krb5_init_creds_set_password");
280 if (keytab) {
281 krb5_keytab kt = NULL;
283 ret = krb5_kt_resolve(kdc_context, heim_string_get_utf8(keytab), &kt);
284 if (ret)
285 krb5_err(kdc_context, 1, ret, "krb5_kt_resolve");
287 ret = krb5_kt_resolve(kdc_context, "MEMORY:keytab", &ktmem);
288 if (ret)
289 krb5_err(kdc_context, 1, ret, "krb5_kt_resolve(MEMORY)");
291 ret = copy_keytab(kdc_context, kt, ktmem);
292 if (ret)
293 krb5_err(kdc_context, 1, ret, "copy_keytab");
295 krb5_kt_close(kdc_context, kt);
297 ret = krb5_init_creds_set_keytab(kdc_context, ctx, ktmem);
298 if (ret)
299 krb5_err(kdc_context, 1, ret, "krb5_init_creds_set_keytab");
302 ret = krb5_init_creds_get(kdc_context, ctx);
303 if (ret)
304 krb5_err(kdc_context, 1, ret, "krb5_init_creds_get");
306 if (ccache) {
307 const char *name = heim_string_get_utf8(ccache);
308 krb5_creds cred;
309 krb5_ccache cc;
311 ret = krb5_init_creds_get_creds(kdc_context, ctx, &cred);
312 if (ret)
313 krb5_err(kdc_context, 1, ret, "krb5_init_creds_get_creds");
315 ret = krb5_cc_resolve(kdc_context, name, &cc);
316 if (ret)
317 krb5_err(kdc_context, 1, ret, "krb5_cc_resolve");
319 krb5_init_creds_store(kdc_context, ctx, cc);
321 ret = krb5_cc_close(kdc_context, cc);
322 if (ret)
323 krb5_err(kdc_context, 1, ret, "krb5_cc_close");
325 krb5_free_cred_contents(kdc_context, &cred);
328 krb5_init_creds_free(kdc_context, ctx);
330 if (ktmem)
331 krb5_kt_close(kdc_context, ktmem);
332 if (fast_cc)
333 krb5_cc_close(kdc_context, fast_cc);
340 static void
341 eval_kgetcred(heim_dict_t o)
343 heim_string_t server, ccache;
344 krb5_get_creds_opt opt;
345 heim_bool_t nostore;
346 krb5_error_code ret;
347 krb5_ccache cc = NULL;
348 krb5_principal s;
349 krb5_creds *out = NULL;
351 if (ptop)
352 ptop->tgs_req++;
354 server = heim_dict_get_value(o, HSTR("server"));
355 if (server == NULL)
356 krb5_errx(kdc_context, 1, "no server");
358 ccache = heim_dict_get_value(o, HSTR("ccache"));
359 if (ccache == NULL)
360 krb5_errx(kdc_context, 1, "no ccache");
362 nostore = heim_dict_get_value(o, HSTR("nostore"));
363 if (nostore == NULL)
364 nostore = heim_bool_create(1);
366 ret = krb5_cc_resolve(kdc_context, heim_string_get_utf8(ccache), &cc);
367 if (ret)
368 krb5_err(kdc_context, 1, ret, "krb5_cc_resolve");
370 ret = krb5_parse_name(kdc_context, heim_string_get_utf8(server), &s);
371 if (ret)
372 krb5_err(kdc_context, 1, ret, "krb5_parse_name");
374 ret = krb5_get_creds_opt_alloc(kdc_context, &opt);
375 if (ret)
376 krb5_err(kdc_context, 1, ret, "krb5_get_creds_opt_alloc");
378 if (heim_bool_val(nostore))
379 krb5_get_creds_opt_add_options(kdc_context, opt, KRB5_GC_NO_STORE);
381 ret = krb5_get_creds(kdc_context, opt, cc, s, &out);
382 if (ret)
383 krb5_err(kdc_context, 1, ret, "krb5_get_creds");
385 krb5_free_creds(kdc_context, out);
386 krb5_free_principal(kdc_context, s);
387 krb5_get_creds_opt_free(kdc_context, opt);
388 krb5_cc_close(kdc_context, cc);
396 static void
397 eval_kdestroy(heim_dict_t o)
399 heim_string_t ccache = heim_dict_get_value(o, HSTR("ccache"));;
400 krb5_error_code ret;
401 const char *name;
402 krb5_ccache cc;
404 heim_assert(ccache != NULL, "ccache_missing");
406 name = heim_string_get_utf8(ccache);
408 ret = krb5_cc_resolve(kdc_context, name, &cc);
409 if (ret)
410 krb5_err(kdc_context, 1, ret, "krb5_cc_resolve");
412 krb5_cc_destroy(kdc_context, cc);
420 static void
421 eval_array_element(heim_object_t o, void *ptr, int *stop)
423 eval_object(o);
426 static void
427 eval_object(heim_object_t o)
429 heim_tid_t t = heim_get_tid(o);
431 if (t == heim_array_get_type_id()) {
432 heim_array_iterate_f(o, NULL, eval_array_element);
433 } else if (t == heim_dict_get_type_id()) {
434 const char *op = heim_dict_get_value(o, HSTR("op"));
436 heim_assert(op != NULL, "op missing");
438 if (strcmp(op, "repeat") == 0) {
439 eval_repeat(o);
440 } else if (strcmp(op, "kinit") == 0) {
441 eval_kinit(o);
442 } else if (strcmp(op, "kgetcred") == 0) {
443 eval_kgetcred(o);
444 } else if (strcmp(op, "kdestroy") == 0) {
445 eval_kdestroy(o);
446 } else {
447 errx(1, "unsupported ops %s", op);
450 } else
451 errx(1, "unsupported");
456 main(int argc, char **argv)
458 krb5_error_code ret;
459 int optidx = 0;
461 setprogname(argv[0]);
463 ret = krb5_init_context(&kdc_context);
464 if (ret == KRB5_CONFIG_BADFORMAT)
465 errx (1, "krb5_init_context failed to parse configuration file");
466 else if (ret)
467 errx (1, "krb5_init_context failed: %d", ret);
469 ret = krb5_kt_register(kdc_context, &hdb_get_kt_ops);
470 if (ret)
471 errx (1, "krb5_kt_register(HDB) failed: %d", ret);
473 kdc_config = configure(kdc_context, argc, argv, &optidx);
475 argc -= optidx;
476 argv += optidx;
478 if (argc == 0)
479 errx(1, "missing operations");
481 krb5_plugin_register(kdc_context, PLUGIN_TYPE_DATA,
482 KRB5_PLUGIN_SEND_TO_KDC, &send_to_kdc);
485 void *buf;
486 size_t size;
487 heim_object_t o;
489 if (rk_undumpdata(argv[0], &buf, &size))
490 errx(1, "undumpdata: %s", argv[0]);
492 o = heim_json_create_with_bytes(buf, size, 10, 0, NULL);
493 free(buf);
494 if (o == NULL)
495 errx(1, "heim_json");
498 * do the work here
501 eval_object(o);
503 heim_release(o);
506 krb5_free_context(kdc_context);
507 return 0;