glue in send_to_kdc
[heimdal.git] / kdc / kdc-tester.c
blob0b0a1bc057d811dc7c60fcf78544c84fe4727102
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 static struct sockaddr_storage sa;
42 static const char *astr = "0.0.0.0";
44 static krb5_error_code
45 send_to_kdc(krb5_context c, void *ptr, krb5_krbhst_info *hi, time_t timeout,
46 const krb5_data *in, krb5_data *out)
48 krb5_error_code ret;
50 ret = krb5_kdc_process_request(kdc_context, kdc_config,
51 in->data, in->length,
52 out, NULL, astr,
53 (struct sockaddr *)&sa, 0);
54 if (ret)
55 krb5_err(c, 1, ret, "krb5_kdc_process_request");
57 return 0;
64 static void
65 eval_kinit(heim_dict_t o)
67 heim_string_t user, password;
68 krb5_init_creds_context ctx;
69 krb5_principal client;
70 krb5_error_code ret;
72 user = heim_dict_get_value(o, HSTR("client"));
73 if (user == NULL)
74 krb5_errx(kdc_context, 1, "no client");
75 password = heim_dict_get_value(o, HSTR("password"));
76 if (password == NULL)
77 krb5_errx(kdc_context, 1, "no password");
79 ret = krb5_parse_name(kdc_context, heim_string_get_utf8(user), &client);
80 if (ret)
81 krb5_err(kdc_context, 1, ret, "krb5_unparse_name");
83 ret = krb5_init_creds_init(kdc_context, client, NULL, NULL, 0, NULL, &ctx);
84 if (ret)
85 krb5_err(kdc_context, 1, ret, "krb5_init_creds_init");
87 ret = krb5_init_creds_set_password(kdc_context, ctx,
88 heim_string_get_utf8(password));
89 if (ret)
90 krb5_err(kdc_context, 1, ret, "krb5_init_creds_set_password");
93 ret = krb5_init_creds_get(kdc_context, ctx);
94 if (ret)
95 krb5_err(kdc_context, 1, ret, "krb5_init_creds_get");
97 krb5_init_creds_free(kdc_context, ctx);
104 static void eval_object(heim_object_t);
106 static void
107 eval_array_element(heim_object_t o, void *ptr)
109 eval_object(o);
112 static void
113 eval_object(heim_object_t o)
115 heim_tid_t t = heim_get_tid(o);
117 if (t == heim_array_get_type_id()) {
118 heim_array_iterate_f(o, NULL, eval_array_element);
119 } else if (t == heim_dict_get_type_id()) {
120 const char *op = heim_dict_get_value(o, HSTR("op"));
122 heim_assert(op != NULL, "op missing");
124 printf("op: %s\n", op);
126 if (strcmp(op, "repeat") == 0) {
127 heim_object_t or = heim_dict_get_value(o, HSTR("value"));
128 heim_number_t n = heim_dict_get_value(o, HSTR("num"));
129 int i, num;
131 heim_assert(or != NULL, "value missing");
132 heim_assert(n != NULL, "num missing");
134 num = heim_number_get_int(n);
135 heim_assert(num >= 0, "num >= 0");
137 printf("num %d\n", num);
139 for (i = 0; i < num; i++)
140 eval_object(or);
142 } else if (strcmp(op, "kinit") == 0) {
143 eval_kinit(o);
144 } else {
145 errx(1, "unsupported ops %s", op);
148 } else
149 errx(1, "unsupported");
154 main(int argc, char **argv)
156 krb5_error_code ret;
157 int optidx = 0;
159 setprogname(argv[0]);
161 ret = krb5_init_context(&kdc_context);
162 if (ret == KRB5_CONFIG_BADFORMAT)
163 errx (1, "krb5_init_context failed to parse configuration file");
164 else if (ret)
165 errx (1, "krb5_init_context failed: %d", ret);
167 ret = krb5_kt_register(kdc_context, &hdb_kt_ops);
168 if (ret)
169 errx (1, "krb5_kt_register(HDB) failed: %d", ret);
171 kdc_config = configure(kdc_context, argc, argv, &optidx);
173 argc -= optidx;
174 argv += optidx;
176 if (argc == 0)
177 errx(1, "missing operations");
179 krb5_set_send_to_kdc_func(kdc_context, send_to_kdc, NULL);
182 void *buf;
183 size_t size;
184 heim_object_t o;
186 if (rk_undumpdata(argv[0], &buf, &size))
187 errx(1, "undumpdata: %s", argv[0]);
189 o = heim_json_create_with_bytes(buf, size, NULL);
190 free(buf);
191 if (o == NULL)
192 errx(1, "heim_json");
195 * do the work here
198 eval_object(o);
200 heim_release(o);
203 krb5_free_context(kdc_context);
204 return 0;