less verbose
[heimdal.git] / kdc / kdc-tester.c
blobd0c29cfccc79c741a72a32348076d3093e07ecd5
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 krb5_kdc_update_time(NULL);
52 ret = krb5_kdc_process_request(kdc_context, kdc_config,
53 in->data, in->length,
54 out, NULL, astr,
55 (struct sockaddr *)&sa, 0);
56 if (ret)
57 krb5_err(c, 1, ret, "krb5_kdc_process_request");
59 return 0;
66 static void
67 eval_kinit(heim_dict_t o)
69 heim_string_t user, password;
70 krb5_init_creds_context ctx;
71 krb5_principal client;
72 krb5_error_code ret;
74 user = heim_dict_get_value(o, HSTR("client"));
75 if (user == NULL)
76 krb5_errx(kdc_context, 1, "no client");
77 password = heim_dict_get_value(o, HSTR("password"));
78 if (password == NULL)
79 krb5_errx(kdc_context, 1, "no password");
81 ret = krb5_parse_name(kdc_context, heim_string_get_utf8(user), &client);
82 if (ret)
83 krb5_err(kdc_context, 1, ret, "krb5_unparse_name");
85 ret = krb5_init_creds_init(kdc_context, client, NULL, NULL, 0, NULL, &ctx);
86 if (ret)
87 krb5_err(kdc_context, 1, ret, "krb5_init_creds_init");
89 ret = krb5_init_creds_set_password(kdc_context, ctx,
90 heim_string_get_utf8(password));
91 if (ret)
92 krb5_err(kdc_context, 1, ret, "krb5_init_creds_set_password");
95 ret = krb5_init_creds_get(kdc_context, ctx);
96 if (ret)
97 krb5_err(kdc_context, 1, ret, "krb5_init_creds_get");
99 krb5_init_creds_free(kdc_context, ctx);
101 printf("kinit success %s\n", heim_string_get_utf8(user));
108 static void eval_object(heim_object_t);
110 static void
111 eval_array_element(heim_object_t o, void *ptr)
113 eval_object(o);
116 static void
117 eval_object(heim_object_t o)
119 heim_tid_t t = heim_get_tid(o);
121 if (t == heim_array_get_type_id()) {
122 heim_array_iterate_f(o, NULL, eval_array_element);
123 } else if (t == heim_dict_get_type_id()) {
124 const char *op = heim_dict_get_value(o, HSTR("op"));
126 heim_assert(op != NULL, "op missing");
128 if (strcmp(op, "repeat") == 0) {
129 heim_object_t or = heim_dict_get_value(o, HSTR("value"));
130 heim_number_t n = heim_dict_get_value(o, HSTR("num"));
131 int i, num;
133 heim_assert(or != NULL, "value missing");
134 heim_assert(n != NULL, "num missing");
136 num = heim_number_get_int(n);
137 heim_assert(num >= 0, "num >= 0");
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;