Drop changing the password, really need kadmin/ldap support to do it
[Samba.git] / lib / krb5 / test_keytab.c
blob33f2f15955211db10487742a4ae18fc99bb522d9
1 /*
2 * Copyright (c) 2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of KTH nor the names of its contributors may be
18 * used to endorse or promote products derived from this software without
19 * specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
33 #include "krb5_locl.h"
34 #include <err.h>
35 #include <getarg.h>
38 * Test that removal entry from of empty keytab doesn't corrupts
39 * memory.
42 static void
43 test_empty_keytab(krb5_context context, const char *keytab)
45 krb5_error_code ret;
46 krb5_keytab id;
47 krb5_keytab_entry entry;
49 ret = krb5_kt_resolve(context, keytab, &id);
50 if (ret)
51 krb5_err(context, 1, ret, "krb5_kt_resolve");
53 memset(&entry, 0, sizeof(entry));
55 krb5_kt_remove_entry(context, id, &entry);
57 ret = krb5_kt_close(context, id);
58 if (ret)
59 krb5_err(context, 1, ret, "krb5_kt_close");
63 * Test that memory keytab are refcounted.
66 static void
67 test_memory_keytab(krb5_context context, const char *keytab, const char *keytab2)
69 krb5_error_code ret;
70 krb5_keytab id, id2, id3;
71 krb5_keytab_entry entry, entry2, entry3;
73 ret = krb5_kt_resolve(context, keytab, &id);
74 if (ret)
75 krb5_err(context, 1, ret, "krb5_kt_resolve");
77 memset(&entry, 0, sizeof(entry));
78 ret = krb5_parse_name(context, "lha@SU.SE", &entry.principal);
79 if (ret)
80 krb5_err(context, 1, ret, "krb5_parse_name");
81 entry.vno = 1;
82 ret = krb5_generate_random_keyblock(context,
83 ETYPE_AES256_CTS_HMAC_SHA1_96,
84 &entry.keyblock);
85 if (ret)
86 krb5_err(context, 1, ret, "krb5_generate_random_keyblock");
88 krb5_kt_add_entry(context, id, &entry);
90 ret = krb5_kt_resolve(context, keytab, &id2);
91 if (ret)
92 krb5_err(context, 1, ret, "krb5_kt_resolve");
94 ret = krb5_kt_get_entry(context, id,
95 entry.principal,
97 ETYPE_AES256_CTS_HMAC_SHA1_96,
98 &entry2);
99 if (ret)
100 krb5_err(context, 1, ret, "krb5_kt_get_entry");
101 krb5_kt_free_entry(context, &entry2);
103 ret = krb5_kt_close(context, id);
104 if (ret)
105 krb5_err(context, 1, ret, "krb5_kt_close");
107 ret = krb5_kt_get_entry(context, id2,
108 entry.principal,
110 ETYPE_AES256_CTS_HMAC_SHA1_96,
111 &entry2);
112 if (ret)
113 krb5_err(context, 1, ret, "krb5_kt_get_entry");
114 krb5_kt_free_entry(context, &entry2);
116 ret = krb5_kt_close(context, id2);
117 if (ret)
118 krb5_err(context, 1, ret, "krb5_kt_close");
121 ret = krb5_kt_resolve(context, keytab2, &id3);
122 if (ret)
123 krb5_err(context, 1, ret, "krb5_kt_resolve");
125 memset(&entry3, 0, sizeof(entry3));
126 ret = krb5_parse_name(context, "lha3@SU.SE", &entry3.principal);
127 if (ret)
128 krb5_err(context, 1, ret, "krb5_parse_name");
129 entry3.vno = 1;
130 ret = krb5_generate_random_keyblock(context,
131 ETYPE_AES256_CTS_HMAC_SHA1_96,
132 &entry3.keyblock);
133 if (ret)
134 krb5_err(context, 1, ret, "krb5_generate_random_keyblock");
136 krb5_kt_add_entry(context, id3, &entry3);
139 ret = krb5_kt_resolve(context, keytab, &id);
140 if (ret)
141 krb5_err(context, 1, ret, "krb5_kt_resolve");
143 ret = krb5_kt_get_entry(context, id,
144 entry.principal,
146 ETYPE_AES256_CTS_HMAC_SHA1_96,
147 &entry2);
148 if (ret == 0)
149 krb5_errx(context, 1, "krb5_kt_get_entry when if should fail");
151 krb5_kt_remove_entry(context, id, &entry);
153 ret = krb5_kt_close(context, id);
154 if (ret)
155 krb5_err(context, 1, ret, "krb5_kt_close");
157 krb5_kt_free_entry(context, &entry);
159 krb5_kt_remove_entry(context, id3, &entry3);
161 ret = krb5_kt_close(context, id3);
162 if (ret)
163 krb5_err(context, 1, ret, "krb5_kt_close");
165 krb5_free_principal(context, entry3.principal);
166 krb5_free_keyblock_contents(context, &entry3.keyblock);
169 static void
170 perf_add(krb5_context context, krb5_keytab id, int times)
174 static void
175 perf_find(krb5_context context, krb5_keytab id, int times)
179 static void
180 perf_delete(krb5_context context, krb5_keytab id, int forward, int times)
185 static int version_flag = 0;
186 static int help_flag = 0;
187 static char *perf_str = NULL;
188 static int times = 1000;
190 static struct getargs args[] = {
191 {"performance", 0, arg_string, &perf_str,
192 "test performance for named keytab", "keytab" },
193 {"times", 0, arg_integer, &times,
194 "number of times to run the perforamce test", "number" },
195 {"version", 0, arg_flag, &version_flag,
196 "print version", NULL },
197 {"help", 0, arg_flag, &help_flag,
198 NULL, NULL }
201 static void
202 usage (int ret)
204 arg_printusage (args,
205 sizeof(args)/sizeof(*args),
206 NULL,
207 "");
208 exit (ret);
212 main(int argc, char **argv)
214 krb5_context context;
215 krb5_error_code ret;
216 int optidx = 0;
218 setprogname(argv[0]);
220 if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
221 usage(1);
223 if (help_flag)
224 usage (0);
226 if(version_flag){
227 print_version(NULL);
228 exit(0);
231 argc -= optidx;
232 argv += optidx;
234 if (argc != 0)
235 errx(1, "argc != 0");
237 ret = krb5_init_context(&context);
238 if (ret)
239 errx (1, "krb5_init_context failed: %d", ret);
241 if (perf_str) {
242 krb5_keytab id;
244 ret = krb5_kt_resolve(context, perf_str, &id);
245 if (ret)
246 krb5_err(context, 1, ret, "krb5_kt_resolve: %s", perf_str);
248 /* add, find, delete on keytab */
249 perf_add(context, id, times);
250 perf_find(context, id, times);
251 perf_delete(context, id, 0, times);
253 /* add and find again on used keytab */
254 perf_add(context, id, times);
255 perf_find(context, id, times);
257 ret = krb5_kt_destroy(context, id);
258 if (ret)
259 krb5_err(context, 1, ret, "krb5_kt_destroy: %s", perf_str);
261 ret = krb5_kt_resolve(context, perf_str, &id);
262 if (ret)
263 krb5_err(context, 1, ret, "krb5_kt_resolve: %s", perf_str);
265 /* try delete backwards */
266 #if 0
267 perf_add(context, id, times);
268 perf_delete(context, id, 1, times);
269 #endif
271 ret = krb5_kt_destroy(context, id);
272 if (ret)
273 krb5_err(context, 1, ret, "krb5_kt_destroy");
275 } else {
277 test_empty_keytab(context, "MEMORY:foo");
278 test_empty_keytab(context, "FILE:foo");
280 test_memory_keytab(context, "MEMORY:foo", "MEMORY:foo2");
284 krb5_free_context(context);
286 return 0;