Formatting: unnecessary lines and trailing whitespace.
[heimdal.git] / kdc / kdc-tester.c
blob6f4a1702cdb9eea47de62f8f20ec19b8d6d5978b
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;
82 static void
83 perf_start(struct perf *perf)
85 memset(perf, 0, sizeof(*perf));
87 gettimeofday(&perf->start, NULL);
88 perf->next = ptop;
89 ptop = perf;
92 static void
93 perf_stop(struct perf *perf)
95 gettimeofday(&perf->stop, NULL);
96 ptop = perf->next;
98 if (ptop) {
99 ptop->as_req += perf->as_req;
100 ptop->tgs_req += perf->tgs_req;
103 timevalsub(&perf->stop, &perf->start);
104 printf("time: %lu.%06lu\n",
105 (unsigned long)perf->stop.tv_sec,
106 (unsigned long)perf->stop.tv_usec);
108 #define USEC_PER_SEC 1000000
110 if (perf->as_req) {
111 double as_ps = 0.0;
112 as_ps = (perf->as_req * USEC_PER_SEC) / (double)((perf->stop.tv_sec * USEC_PER_SEC) + perf->stop.tv_usec);
113 printf("as-req/s %.2lf (total %lu requests)\n", as_ps, perf->as_req);
116 if (perf->tgs_req) {
117 double tgs_ps = 0.0;
118 tgs_ps = (perf->tgs_req * USEC_PER_SEC) / (double)((perf->stop.tv_sec * USEC_PER_SEC) + perf->stop.tv_usec);
119 printf("tgs-req/s %.2lf (total %lu requests)\n", tgs_ps, perf->tgs_req);
127 static void
128 eval_repeat(heim_dict_t o)
130 heim_object_t or = heim_dict_get_value(o, HSTR("value"));
131 heim_number_t n = heim_dict_get_value(o, HSTR("num"));
132 int i, num;
133 struct perf perf;
135 perf_start(&perf);
137 heim_assert(or != NULL, "value missing");
138 heim_assert(n != NULL, "num missing");
140 num = heim_number_get_int(n);
141 heim_assert(num >= 0, "num >= 0");
143 for (i = 0; i < num; i++)
144 eval_object(or);
146 perf_stop(&perf);
153 static void
154 eval_kinit(heim_dict_t o)
156 heim_string_t user, password, keytab, fast_armor_cc, pk_user_id, ccache;
157 krb5_get_init_creds_opt *opt;
158 krb5_init_creds_context ctx;
159 krb5_principal client;
160 krb5_keytab kt = NULL;
161 krb5_ccache fast_cc = NULL;
162 krb5_error_code ret;
164 if (ptop)
165 ptop->as_req++;
167 user = heim_dict_get_value(o, HSTR("client"));
168 if (user == NULL)
169 krb5_errx(kdc_context, 1, "no client");
171 password = heim_dict_get_value(o, HSTR("password"));
172 keytab = heim_dict_get_value(o, HSTR("keytab"));
173 pk_user_id = heim_dict_get_value(o, HSTR("pkinit-user-cert-id"));
174 if (password == NULL && keytab == NULL && pk_user_id == NULL)
175 krb5_errx(kdc_context, 1, "password, keytab, nor PKINIT user cert ID");
177 ccache = heim_dict_get_value(o, HSTR("ccache"));
179 ret = krb5_parse_name(kdc_context, heim_string_get_utf8(user), &client);
180 if (ret)
181 krb5_err(kdc_context, 1, ret, "krb5_unparse_name");
183 /* PKINIT parts */
184 ret = krb5_get_init_creds_opt_alloc (kdc_context, &opt);
185 if (ret)
186 krb5_err(kdc_context, 1, ret, "krb5_get_init_creds_opt_alloc");
188 if (pk_user_id) {
189 heim_bool_t rsaobj = heim_dict_get_value(o, HSTR("pkinit-use-rsa"));
190 int use_rsa = rsaobj ? heim_bool_val(rsaobj) : 0;
192 ret = krb5_get_init_creds_opt_set_pkinit(kdc_context, opt,
193 client,
194 heim_string_get_utf8(pk_user_id),
195 NULL, NULL, NULL,
196 use_rsa ? 2 : 0,
197 NULL, NULL, NULL);
198 if (ret)
199 krb5_err(kdc_context, 1, ret, "krb5_get_init_creds_opt_set_pkinit");
202 ret = krb5_init_creds_init(kdc_context, client, NULL, NULL, 0, opt, &ctx);
203 if (ret)
204 krb5_err(kdc_context, 1, ret, "krb5_init_creds_init");
206 fast_armor_cc = heim_dict_get_value(o, HSTR("fast-armor-cc"));
207 if (fast_armor_cc) {
209 ret = krb5_cc_resolve(kdc_context, heim_string_get_utf8(fast_armor_cc), &fast_cc);
210 if (ret)
211 krb5_err(kdc_context, 1, ret, "krb5_cc_resolve");
213 ret = krb5_init_creds_set_fast_ccache(kdc_context, ctx, fast_cc);
214 if (ret)
215 krb5_err(kdc_context, 1, ret, "krb5_init_creds_set_fast_ccache");
218 if (password) {
219 ret = krb5_init_creds_set_password(kdc_context, ctx,
220 heim_string_get_utf8(password));
221 if (ret)
222 krb5_err(kdc_context, 1, ret, "krb5_init_creds_set_password");
224 if (keytab) {
225 ret = krb5_kt_resolve(kdc_context, heim_string_get_utf8(keytab), &kt);
226 if (ret)
227 krb5_err(kdc_context, 1, ret, "krb5_kt_resolve");
229 ret = krb5_init_creds_set_keytab(kdc_context, ctx, kt);
230 if (ret)
231 krb5_err(kdc_context, 1, ret, "krb5_init_creds_set_keytab");
234 ret = krb5_init_creds_get(kdc_context, ctx);
235 if (ret)
236 krb5_err(kdc_context, 1, ret, "krb5_init_creds_get");
238 if (ccache) {
239 const char *name = heim_string_get_utf8(ccache);
240 krb5_creds cred;
241 krb5_ccache cc;
243 ret = krb5_init_creds_get_creds(kdc_context, ctx, &cred);
244 if (ret)
245 krb5_err(kdc_context, 1, ret, "krb5_init_creds_get_creds");
247 ret = krb5_cc_resolve(kdc_context, name, &cc);
248 if (ret)
249 krb5_err(kdc_context, 1, ret, "krb5_cc_resolve");
251 krb5_init_creds_store(kdc_context, ctx, cc);
253 ret = krb5_cc_close(kdc_context, cc);
254 if (ret)
255 krb5_err(kdc_context, 1, ret, "krb5_cc_close");
257 krb5_free_cred_contents(kdc_context, &cred);
260 krb5_init_creds_free(kdc_context, ctx);
262 if (kt)
263 krb5_kt_close(kdc_context, kt);
264 if (fast_cc)
265 krb5_cc_close(kdc_context, fast_cc);
272 static void
273 eval_kgetcred(heim_dict_t o)
275 heim_string_t server, ccache;
276 krb5_get_creds_opt opt;
277 heim_bool_t nostore;
278 krb5_error_code ret;
279 krb5_ccache cc = NULL;
280 krb5_principal s;
281 krb5_creds *out = NULL;
283 if (ptop)
284 ptop->tgs_req++;
286 server = heim_dict_get_value(o, HSTR("server"));
287 if (server == NULL)
288 krb5_errx(kdc_context, 1, "no server");
290 ccache = heim_dict_get_value(o, HSTR("ccache"));
291 if (ccache == NULL)
292 krb5_errx(kdc_context, 1, "no ccache");
294 nostore = heim_dict_get_value(o, HSTR("nostore"));
295 if (nostore == NULL)
296 nostore = heim_bool_create(1);
298 ret = krb5_cc_resolve(kdc_context, heim_string_get_utf8(ccache), &cc);
299 if (ret)
300 krb5_err(kdc_context, 1, ret, "krb5_cc_resolve");
302 ret = krb5_parse_name(kdc_context, heim_string_get_utf8(server), &s);
303 if (ret)
304 krb5_err(kdc_context, 1, ret, "krb5_parse_name");
306 ret = krb5_get_creds_opt_alloc(kdc_context, &opt);
307 if (ret)
308 krb5_err(kdc_context, 1, ret, "krb5_get_creds_opt_alloc");
310 if (heim_bool_val(nostore))
311 krb5_get_creds_opt_add_options(kdc_context, opt, KRB5_GC_NO_STORE);
313 ret = krb5_get_creds(kdc_context, opt, cc, s, &out);
314 if (ret)
315 krb5_err(kdc_context, 1, ret, "krb5_get_creds");
317 krb5_free_creds(kdc_context, out);
318 krb5_free_principal(kdc_context, s);
319 krb5_get_creds_opt_free(kdc_context, opt);
320 krb5_cc_close(kdc_context, cc);
328 static void
329 eval_kdestroy(heim_dict_t o)
331 heim_string_t ccache = heim_dict_get_value(o, HSTR("ccache"));;
332 krb5_error_code ret;
333 const char *name;
334 krb5_ccache cc;
336 heim_assert(ccache != NULL, "ccache_missing");
338 name = heim_string_get_utf8(ccache);
340 ret = krb5_cc_resolve(kdc_context, name, &cc);
341 if (ret)
342 krb5_err(kdc_context, 1, ret, "krb5_cc_resolve");
344 krb5_cc_destroy(kdc_context, cc);
352 static void
353 eval_array_element(heim_object_t o, void *ptr)
355 eval_object(o);
358 static void
359 eval_object(heim_object_t o)
361 heim_tid_t t = heim_get_tid(o);
363 if (t == heim_array_get_type_id()) {
364 heim_array_iterate_f(o, NULL, eval_array_element);
365 } else if (t == heim_dict_get_type_id()) {
366 const char *op = heim_dict_get_value(o, HSTR("op"));
368 heim_assert(op != NULL, "op missing");
370 if (strcmp(op, "repeat") == 0) {
371 eval_repeat(o);
372 } else if (strcmp(op, "kinit") == 0) {
373 eval_kinit(o);
374 } else if (strcmp(op, "kgetcred") == 0) {
375 eval_kgetcred(o);
376 } else if (strcmp(op, "kdestroy") == 0) {
377 eval_kdestroy(o);
378 } else {
379 errx(1, "unsupported ops %s", op);
382 } else
383 errx(1, "unsupported");
388 main(int argc, char **argv)
390 krb5_error_code ret;
391 int optidx = 0;
393 setprogname(argv[0]);
395 ret = krb5_init_context(&kdc_context);
396 if (ret == KRB5_CONFIG_BADFORMAT)
397 errx (1, "krb5_init_context failed to parse configuration file");
398 else if (ret)
399 errx (1, "krb5_init_context failed: %d", ret);
401 ret = krb5_kt_register(kdc_context, &hdb_kt_ops);
402 if (ret)
403 errx (1, "krb5_kt_register(HDB) failed: %d", ret);
405 kdc_config = configure(kdc_context, argc, argv, &optidx);
407 argc -= optidx;
408 argv += optidx;
410 if (argc == 0)
411 errx(1, "missing operations");
413 krb5_set_send_to_kdc_func(kdc_context, send_to_kdc, NULL);
416 void *buf;
417 size_t size;
418 heim_object_t o;
420 if (rk_undumpdata(argv[0], &buf, &size))
421 errx(1, "undumpdata: %s", argv[0]);
423 o = heim_json_create_with_bytes(buf, size, 10, 0, NULL);
424 free(buf);
425 if (o == NULL)
426 errx(1, "heim_json");
429 * do the work here
432 eval_object(o);
434 heim_release(o);
437 krb5_free_context(kdc_context);
438 return 0;