add basic support for pkinit
[heimdal.git] / kdc / kdc-tester.c
blob178c9e9e5eb44620f5a470ef117ac9543a62fa22
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 static krb5_kdc_configuration *kdc_config;
39 static krb5_context kdc_context;
41 #if 0
43 static struct sockaddr_storage sa;
44 static krb5_socklen_t salen = sizeof(sa);
45 static const char *astr = "0.0.0.0";
47 static void
48 send_to_kdc(krb5_context context)
50 krb5_error_code ret;
52 ret = krb5_kdc_process_request(kdc_context, kdc_config,
53 d.data, d.length,
54 &r, NULL, astr,
55 (struct sockaddr *)&sa, 0);
56 if (ret)
57 krb5_err(context, 1, ret, "krb5_kdc_process_request");
59 #endif
65 static void
66 eval_kinit(heim_dict_t o)
68 heim_string_t user, password;
69 krb5_init_creds_context ctx;
70 krb5_principal client;
71 krb5_error_code ret;
73 user = heim_dict_get_value(o, HSTR("client"));
74 if (user == NULL)
75 krb5_errx(kdc_context, 1, "no client");
76 password = heim_dict_get_value(o, HSTR("password"));
77 if (password == NULL)
78 krb5_errx(kdc_context, 1, "no password");
80 ret = krb5_parse_name(kdc_context, heim_string_get_utf8(user), &client);
81 if (ret)
82 krb5_err(kdc_context, 1, ret, "krb5_unparse_name");
84 ret = krb5_init_creds_init(kdc_context, client, NULL, NULL, 0, NULL, &ctx);
85 if (ret)
86 krb5_err(kdc_context, 1, ret, "krb5_init_creds_init");
88 ret = krb5_init_creds_set_password(kdc_context, ctx,
89 heim_string_get_utf8(password));
90 if (ret)
91 krb5_err(kdc_context, 1, ret, "krb5_init_creds_set_password");
94 ret = krb5_init_creds_get(kdc_context, ctx);
95 if (ret)
96 krb5_err(kdc_context, 1, ret, "krb5_init_creds_get");
98 krb5_init_creds_free(kdc_context, ctx);
105 static void eval_object(heim_object_t);
107 static void
108 eval_array_element(heim_object_t o, void *ptr)
110 eval_object(o);
113 static void
114 eval_object(heim_object_t o)
116 heim_tid_t t = heim_get_tid(o);
118 if (t == heim_array_get_type_id()) {
119 heim_array_iterate_f(o, NULL, eval_array_element);
120 } else if (t == heim_dict_get_type_id()) {
121 const char *op = heim_dict_get_value(o, HSTR("op"));
123 heim_assert(op != NULL, "op missing");
125 printf("op: %s\n", op);
127 if (strcmp(op, "repeat") == 0) {
128 heim_object_t or = heim_dict_get_value(o, HSTR("value"));
129 heim_number_t n = heim_dict_get_value(o, HSTR("num"));
130 int i, num;
132 heim_assert(or != NULL, "value missing");
133 heim_assert(n != NULL, "num missing");
135 num = heim_number_get_int(n);
136 heim_assert(num >= 0, "num >= 0");
138 printf("num %d\n", num);
140 for (i = 0; i < num; i++)
141 eval_object(or);
143 } else if (strcmp(op, "kinit") == 0) {
144 eval_kinit(o);
145 } else {
146 errx(1, "unsupported ops %s", op);
149 } else
150 errx(1, "unsupported");
155 main(int argc, char **argv)
157 krb5_error_code ret;
158 int optidx = 0;
160 setprogname(argv[0]);
162 ret = krb5_init_context(&kdc_context);
163 if (ret == KRB5_CONFIG_BADFORMAT)
164 errx (1, "krb5_init_context failed to parse configuration file");
165 else if (ret)
166 errx (1, "krb5_init_context failed: %d", ret);
168 ret = krb5_kt_register(kdc_context, &hdb_kt_ops);
169 if (ret)
170 errx (1, "krb5_kt_register(HDB) failed: %d", ret);
172 kdc_config = configure(kdc_context, argc, argv, &optidx);
174 argc -= optidx;
175 argv += optidx;
177 if (argc == 0)
178 errx(1, "missing operations");
181 void *buf;
182 size_t size;
183 heim_object_t o;
185 if (rk_undumpdata(argv[0], &buf, &size))
186 errx(1, "undumpdata: %s", argv[0]);
188 o = heim_json_create_with_bytes(buf, size, NULL);
189 free(buf);
190 if (o == NULL)
191 errx(1, "heim_json");
194 * do the work here
197 eval_object(o);
199 heim_release(o);
201 krb5_free_context(kdc_context);
202 return 0;