keytab.c revision 1.1 1 1.1 elric /* $NetBSD: keytab.c,v 1.1 2011/04/13 18:15:34 elric Exp $ */
2 1.1 elric
3 1.1 elric /*
4 1.1 elric * Copyright (c) 1997 - 2005 Kungliga Tekniska Hgskolan
5 1.1 elric * (Royal Institute of Technology, Stockholm, Sweden).
6 1.1 elric * All rights reserved.
7 1.1 elric *
8 1.1 elric * Redistribution and use in source and binary forms, with or without
9 1.1 elric * modification, are permitted provided that the following conditions
10 1.1 elric * are met:
11 1.1 elric *
12 1.1 elric * 1. Redistributions of source code must retain the above copyright
13 1.1 elric * notice, this list of conditions and the following disclaimer.
14 1.1 elric *
15 1.1 elric * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 elric * notice, this list of conditions and the following disclaimer in the
17 1.1 elric * documentation and/or other materials provided with the distribution.
18 1.1 elric *
19 1.1 elric * 3. Neither the name of the Institute nor the names of its contributors
20 1.1 elric * may be used to endorse or promote products derived from this software
21 1.1 elric * without specific prior written permission.
22 1.1 elric *
23 1.1 elric * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 1.1 elric * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 elric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 elric * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 1.1 elric * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 elric * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 elric * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 elric * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 elric * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 elric * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 elric * SUCH DAMAGE.
34 1.1 elric */
35 1.1 elric
36 1.1 elric #include "krb5_locl.h"
37 1.1 elric
38 1.1 elric /**
39 1.1 elric * @page krb5_keytab_intro The keytab handing functions
40 1.1 elric * @section section_krb5_keytab Kerberos Keytabs
41 1.1 elric *
42 1.1 elric * See the library functions here: @ref krb5_keytab
43 1.1 elric *
44 1.1 elric * Keytabs are long term key storage for servers, their equvalment of
45 1.1 elric * password files.
46 1.1 elric *
47 1.1 elric * Normally the only function that useful for server are to specify
48 1.1 elric * what keytab to use to other core functions like krb5_rd_req()
49 1.1 elric * krb5_kt_resolve(), and krb5_kt_close().
50 1.1 elric *
51 1.1 elric * @subsection krb5_keytab_names Keytab names
52 1.1 elric *
53 1.1 elric * A keytab name is on the form type:residual. The residual part is
54 1.1 elric * specific to each keytab-type.
55 1.1 elric *
56 1.1 elric * When a keytab-name is resolved, the type is matched with an internal
57 1.1 elric * list of keytab types. If there is no matching keytab type,
58 1.1 elric * the default keytab is used. The current default type is FILE.
59 1.1 elric *
60 1.1 elric * The default value can be changed in the configuration file
61 1.1 elric * /etc/krb5.conf by setting the variable
62 1.1 elric * [defaults]default_keytab_name.
63 1.1 elric *
64 1.1 elric * The keytab types that are implemented in Heimdal are:
65 1.1 elric * - file
66 1.1 elric * store the keytab in a file, the type's name is FILE . The
67 1.1 elric * residual part is a filename. For compatibility with other
68 1.1 elric * Kerberos implemtation WRFILE and JAVA14 is also accepted. WRFILE
69 1.1 elric * has the same format as FILE. JAVA14 have a format that is
70 1.1 elric * compatible with older versions of MIT kerberos and SUN's Java
71 1.1 elric * based installation. They store a truncted kvno, so when the knvo
72 1.1 elric * excess 255, they are truncted in this format.
73 1.1 elric *
74 1.1 elric * - keytab
75 1.1 elric * store the keytab in a AFS keyfile (usually /usr/afs/etc/KeyFile ),
76 1.1 elric * the type's name is AFSKEYFILE. The residual part is a filename.
77 1.1 elric *
78 1.1 elric * - memory
79 1.1 elric * The keytab is stored in a memory segment. This allows sensitive
80 1.1 elric * and/or temporary data not to be stored on disk. The type's name
81 1.1 elric * is MEMORY. Each MEMORY keytab is referenced counted by and
82 1.1 elric * opened by the residual name, so two handles can point to the
83 1.1 elric * same memory area. When the last user closes using krb5_kt_close()
84 1.1 elric * the keytab, the keys in they keytab is memset() to zero and freed
85 1.1 elric * and can no longer be looked up by name.
86 1.1 elric *
87 1.1 elric *
88 1.1 elric * @subsection krb5_keytab_example Keytab example
89 1.1 elric *
90 1.1 elric * This is a minimalistic version of ktutil.
91 1.1 elric *
92 1.1 elric * @code
93 1.1 elric int
94 1.1 elric main (int argc, char **argv)
95 1.1 elric {
96 1.1 elric krb5_context context;
97 1.1 elric krb5_keytab keytab;
98 1.1 elric krb5_kt_cursor cursor;
99 1.1 elric krb5_keytab_entry entry;
100 1.1 elric krb5_error_code ret;
101 1.1 elric char *principal;
102 1.1 elric
103 1.1 elric if (krb5_init_context (&context) != 0)
104 1.1 elric errx(1, "krb5_context");
105 1.1 elric
106 1.1 elric ret = krb5_kt_default (context, &keytab);
107 1.1 elric if (ret)
108 1.1 elric krb5_err(context, 1, ret, "krb5_kt_default");
109 1.1 elric
110 1.1 elric ret = krb5_kt_start_seq_get(context, keytab, &cursor);
111 1.1 elric if (ret)
112 1.1 elric krb5_err(context, 1, ret, "krb5_kt_start_seq_get");
113 1.1 elric while((ret = krb5_kt_next_entry(context, keytab, &entry, &cursor)) == 0){
114 1.1 elric krb5_unparse_name(context, entry.principal, &principal);
115 1.1 elric printf("principal: %s\n", principal);
116 1.1 elric free(principal);
117 1.1 elric krb5_kt_free_entry(context, &entry);
118 1.1 elric }
119 1.1 elric ret = krb5_kt_end_seq_get(context, keytab, &cursor);
120 1.1 elric if (ret)
121 1.1 elric krb5_err(context, 1, ret, "krb5_kt_end_seq_get");
122 1.1 elric ret = krb5_kt_close(context, keytab);
123 1.1 elric if (ret)
124 1.1 elric krb5_err(context, 1, ret, "krb5_kt_close");
125 1.1 elric krb5_free_context(context);
126 1.1 elric return 0;
127 1.1 elric }
128 1.1 elric * @endcode
129 1.1 elric *
130 1.1 elric */
131 1.1 elric
132 1.1 elric
133 1.1 elric /**
134 1.1 elric * Register a new keytab backend.
135 1.1 elric *
136 1.1 elric * @param context a Keberos context.
137 1.1 elric * @param ops a backend to register.
138 1.1 elric *
139 1.1 elric * @return Return an error code or 0, see krb5_get_error_message().
140 1.1 elric *
141 1.1 elric * @ingroup krb5_keytab
142 1.1 elric */
143 1.1 elric
144 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
145 1.1 elric krb5_kt_register(krb5_context context,
146 1.1 elric const krb5_kt_ops *ops)
147 1.1 elric {
148 1.1 elric struct krb5_keytab_data *tmp;
149 1.1 elric
150 1.1 elric if (strlen(ops->prefix) > KRB5_KT_PREFIX_MAX_LEN - 1) {
151 1.1 elric krb5_set_error_message(context, KRB5_KT_BADNAME,
152 1.1 elric N_("can't register cache type, prefix too long", ""));
153 1.1 elric return KRB5_KT_BADNAME;
154 1.1 elric }
155 1.1 elric
156 1.1 elric tmp = realloc(context->kt_types,
157 1.1 elric (context->num_kt_types + 1) * sizeof(*context->kt_types));
158 1.1 elric if(tmp == NULL) {
159 1.1 elric krb5_set_error_message(context, ENOMEM,
160 1.1 elric N_("malloc: out of memory", ""));
161 1.1 elric return ENOMEM;
162 1.1 elric }
163 1.1 elric memcpy(&tmp[context->num_kt_types], ops,
164 1.1 elric sizeof(tmp[context->num_kt_types]));
165 1.1 elric context->kt_types = tmp;
166 1.1 elric context->num_kt_types++;
167 1.1 elric return 0;
168 1.1 elric }
169 1.1 elric
170 1.1 elric static const char *
171 1.1 elric keytab_name(const char *name, const char **type, size_t *type_len)
172 1.1 elric {
173 1.1 elric const char *residual;
174 1.1 elric
175 1.1 elric residual = strchr(name, ':');
176 1.1 elric
177 1.1 elric if (residual == NULL ||
178 1.1 elric name[0] == '/'
179 1.1 elric #ifdef _WIN32
180 1.1 elric /* Avoid treating <drive>:<path> as a keytab type
181 1.1 elric * specification */
182 1.1 elric || name + 1 == residual
183 1.1 elric #endif
184 1.1 elric ) {
185 1.1 elric
186 1.1 elric *type = "FILE";
187 1.1 elric *type_len = strlen(*type);
188 1.1 elric residual = name;
189 1.1 elric } else {
190 1.1 elric *type = name;
191 1.1 elric *type_len = residual - name;
192 1.1 elric residual++;
193 1.1 elric }
194 1.1 elric
195 1.1 elric return residual;
196 1.1 elric }
197 1.1 elric
198 1.1 elric /**
199 1.1 elric * Resolve the keytab name (of the form `type:residual') in `name'
200 1.1 elric * into a keytab in `id'.
201 1.1 elric *
202 1.1 elric * @param context a Keberos context.
203 1.1 elric * @param name name to resolve
204 1.1 elric * @param id resulting keytab, free with krb5_kt_close().
205 1.1 elric *
206 1.1 elric * @return Return an error code or 0, see krb5_get_error_message().
207 1.1 elric *
208 1.1 elric * @ingroup krb5_keytab
209 1.1 elric */
210 1.1 elric
211 1.1 elric
212 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
213 1.1 elric krb5_kt_resolve(krb5_context context,
214 1.1 elric const char *name,
215 1.1 elric krb5_keytab *id)
216 1.1 elric {
217 1.1 elric krb5_keytab k;
218 1.1 elric int i;
219 1.1 elric const char *type, *residual;
220 1.1 elric size_t type_len;
221 1.1 elric krb5_error_code ret;
222 1.1 elric
223 1.1 elric residual = keytab_name(name, &type, &type_len);
224 1.1 elric
225 1.1 elric for(i = 0; i < context->num_kt_types; i++) {
226 1.1 elric if(strncasecmp(type, context->kt_types[i].prefix, type_len) == 0)
227 1.1 elric break;
228 1.1 elric }
229 1.1 elric if(i == context->num_kt_types) {
230 1.1 elric krb5_set_error_message(context, KRB5_KT_UNKNOWN_TYPE,
231 1.1 elric N_("unknown keytab type %.*s", "type"),
232 1.1 elric (int)type_len, type);
233 1.1 elric return KRB5_KT_UNKNOWN_TYPE;
234 1.1 elric }
235 1.1 elric
236 1.1 elric k = malloc (sizeof(*k));
237 1.1 elric if (k == NULL) {
238 1.1 elric krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
239 1.1 elric return ENOMEM;
240 1.1 elric }
241 1.1 elric memcpy(k, &context->kt_types[i], sizeof(*k));
242 1.1 elric k->data = NULL;
243 1.1 elric ret = (*k->resolve)(context, residual, k);
244 1.1 elric if(ret) {
245 1.1 elric free(k);
246 1.1 elric k = NULL;
247 1.1 elric }
248 1.1 elric *id = k;
249 1.1 elric return ret;
250 1.1 elric }
251 1.1 elric
252 1.1 elric /**
253 1.1 elric * copy the name of the default keytab into `name'.
254 1.1 elric *
255 1.1 elric * @param context a Keberos context.
256 1.1 elric * @param name buffer where the name will be written
257 1.1 elric * @param namesize length of name
258 1.1 elric *
259 1.1 elric * @return Return an error code or 0, see krb5_get_error_message().
260 1.1 elric *
261 1.1 elric * @ingroup krb5_keytab
262 1.1 elric */
263 1.1 elric
264 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
265 1.1 elric krb5_kt_default_name(krb5_context context, char *name, size_t namesize)
266 1.1 elric {
267 1.1 elric if (strlcpy (name, context->default_keytab, namesize) >= namesize) {
268 1.1 elric krb5_clear_error_message (context);
269 1.1 elric return KRB5_CONFIG_NOTENUFSPACE;
270 1.1 elric }
271 1.1 elric return 0;
272 1.1 elric }
273 1.1 elric
274 1.1 elric /**
275 1.1 elric * Copy the name of the default modify keytab into `name'.
276 1.1 elric *
277 1.1 elric * @param context a Keberos context.
278 1.1 elric * @param name buffer where the name will be written
279 1.1 elric * @param namesize length of name
280 1.1 elric *
281 1.1 elric * @return Return an error code or 0, see krb5_get_error_message().
282 1.1 elric *
283 1.1 elric * @ingroup krb5_keytab
284 1.1 elric */
285 1.1 elric
286 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
287 1.1 elric krb5_kt_default_modify_name(krb5_context context, char *name, size_t namesize)
288 1.1 elric {
289 1.1 elric const char *kt = NULL;
290 1.1 elric if(context->default_keytab_modify == NULL) {
291 1.1 elric if(strncasecmp(context->default_keytab, "ANY:", 4) != 0)
292 1.1 elric kt = context->default_keytab;
293 1.1 elric else {
294 1.1 elric size_t len = strcspn(context->default_keytab + 4, ",");
295 1.1 elric if(len >= namesize) {
296 1.1 elric krb5_clear_error_message(context);
297 1.1 elric return KRB5_CONFIG_NOTENUFSPACE;
298 1.1 elric }
299 1.1 elric strlcpy(name, context->default_keytab + 4, namesize);
300 1.1 elric name[len] = '\0';
301 1.1 elric return 0;
302 1.1 elric }
303 1.1 elric } else
304 1.1 elric kt = context->default_keytab_modify;
305 1.1 elric if (strlcpy (name, kt, namesize) >= namesize) {
306 1.1 elric krb5_clear_error_message (context);
307 1.1 elric return KRB5_CONFIG_NOTENUFSPACE;
308 1.1 elric }
309 1.1 elric return 0;
310 1.1 elric }
311 1.1 elric
312 1.1 elric /**
313 1.1 elric * Set `id' to the default keytab.
314 1.1 elric *
315 1.1 elric * @param context a Keberos context.
316 1.1 elric * @param id the new default keytab.
317 1.1 elric *
318 1.1 elric * @return Return an error code or 0, see krb5_get_error_message().
319 1.1 elric *
320 1.1 elric * @ingroup krb5_keytab
321 1.1 elric */
322 1.1 elric
323 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
324 1.1 elric krb5_kt_default(krb5_context context, krb5_keytab *id)
325 1.1 elric {
326 1.1 elric return krb5_kt_resolve (context, context->default_keytab, id);
327 1.1 elric }
328 1.1 elric
329 1.1 elric /**
330 1.1 elric * Read the key identified by `(principal, vno, enctype)' from the
331 1.1 elric * keytab in `keyprocarg' (the default if == NULL) into `*key'.
332 1.1 elric *
333 1.1 elric * @param context a Keberos context.
334 1.1 elric * @param keyprocarg
335 1.1 elric * @param principal
336 1.1 elric * @param vno
337 1.1 elric * @param enctype
338 1.1 elric * @param key
339 1.1 elric *
340 1.1 elric * @return Return an error code or 0, see krb5_get_error_message().
341 1.1 elric *
342 1.1 elric * @ingroup krb5_keytab
343 1.1 elric */
344 1.1 elric
345 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
346 1.1 elric krb5_kt_read_service_key(krb5_context context,
347 1.1 elric krb5_pointer keyprocarg,
348 1.1 elric krb5_principal principal,
349 1.1 elric krb5_kvno vno,
350 1.1 elric krb5_enctype enctype,
351 1.1 elric krb5_keyblock **key)
352 1.1 elric {
353 1.1 elric krb5_keytab keytab;
354 1.1 elric krb5_keytab_entry entry;
355 1.1 elric krb5_error_code ret;
356 1.1 elric
357 1.1 elric if (keyprocarg)
358 1.1 elric ret = krb5_kt_resolve (context, keyprocarg, &keytab);
359 1.1 elric else
360 1.1 elric ret = krb5_kt_default (context, &keytab);
361 1.1 elric
362 1.1 elric if (ret)
363 1.1 elric return ret;
364 1.1 elric
365 1.1 elric ret = krb5_kt_get_entry (context, keytab, principal, vno, enctype, &entry);
366 1.1 elric krb5_kt_close (context, keytab);
367 1.1 elric if (ret)
368 1.1 elric return ret;
369 1.1 elric ret = krb5_copy_keyblock (context, &entry.keyblock, key);
370 1.1 elric krb5_kt_free_entry(context, &entry);
371 1.1 elric return ret;
372 1.1 elric }
373 1.1 elric
374 1.1 elric /**
375 1.1 elric * Return the type of the `keytab' in the string `prefix of length
376 1.1 elric * `prefixsize'.
377 1.1 elric *
378 1.1 elric * @param context a Keberos context.
379 1.1 elric * @param keytab the keytab to get the prefix for
380 1.1 elric * @param prefix prefix buffer
381 1.1 elric * @param prefixsize length of prefix buffer
382 1.1 elric *
383 1.1 elric * @return Return an error code or 0, see krb5_get_error_message().
384 1.1 elric *
385 1.1 elric * @ingroup krb5_keytab
386 1.1 elric */
387 1.1 elric
388 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
389 1.1 elric krb5_kt_get_type(krb5_context context,
390 1.1 elric krb5_keytab keytab,
391 1.1 elric char *prefix,
392 1.1 elric size_t prefixsize)
393 1.1 elric {
394 1.1 elric strlcpy(prefix, keytab->prefix, prefixsize);
395 1.1 elric return 0;
396 1.1 elric }
397 1.1 elric
398 1.1 elric /**
399 1.1 elric * Retrieve the name of the keytab `keytab' into `name', `namesize'
400 1.1 elric *
401 1.1 elric * @param context a Keberos context.
402 1.1 elric * @param keytab the keytab to get the name for.
403 1.1 elric * @param name name buffer.
404 1.1 elric * @param namesize size of name buffer.
405 1.1 elric *
406 1.1 elric * @return Return an error code or 0, see krb5_get_error_message().
407 1.1 elric *
408 1.1 elric * @ingroup krb5_keytab
409 1.1 elric */
410 1.1 elric
411 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
412 1.1 elric krb5_kt_get_name(krb5_context context,
413 1.1 elric krb5_keytab keytab,
414 1.1 elric char *name,
415 1.1 elric size_t namesize)
416 1.1 elric {
417 1.1 elric return (*keytab->get_name)(context, keytab, name, namesize);
418 1.1 elric }
419 1.1 elric
420 1.1 elric /**
421 1.1 elric * Retrieve the full name of the keytab `keytab' and store the name in
422 1.1 elric * `str'.
423 1.1 elric *
424 1.1 elric * @param context a Keberos context.
425 1.1 elric * @param keytab keytab to get name for.
426 1.1 elric * @param str the name of the keytab name, usee krb5_xfree() to free
427 1.1 elric * the string. On error, *str is set to NULL.
428 1.1 elric *
429 1.1 elric * @return Return an error code or 0, see krb5_get_error_message().
430 1.1 elric *
431 1.1 elric * @ingroup krb5_keytab
432 1.1 elric */
433 1.1 elric
434 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
435 1.1 elric krb5_kt_get_full_name(krb5_context context,
436 1.1 elric krb5_keytab keytab,
437 1.1 elric char **str)
438 1.1 elric {
439 1.1 elric char type[KRB5_KT_PREFIX_MAX_LEN];
440 1.1 elric char name[MAXPATHLEN];
441 1.1 elric krb5_error_code ret;
442 1.1 elric
443 1.1 elric *str = NULL;
444 1.1 elric
445 1.1 elric ret = krb5_kt_get_type(context, keytab, type, sizeof(type));
446 1.1 elric if (ret)
447 1.1 elric return ret;
448 1.1 elric
449 1.1 elric ret = krb5_kt_get_name(context, keytab, name, sizeof(name));
450 1.1 elric if (ret)
451 1.1 elric return ret;
452 1.1 elric
453 1.1 elric if (asprintf(str, "%s:%s", type, name) == -1) {
454 1.1 elric krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
455 1.1 elric *str = NULL;
456 1.1 elric return ENOMEM;
457 1.1 elric }
458 1.1 elric
459 1.1 elric return 0;
460 1.1 elric }
461 1.1 elric
462 1.1 elric /**
463 1.1 elric * Finish using the keytab in `id'. All resources will be released,
464 1.1 elric * even on errors.
465 1.1 elric *
466 1.1 elric * @param context a Keberos context.
467 1.1 elric * @param id keytab to close.
468 1.1 elric *
469 1.1 elric * @return Return an error code or 0, see krb5_get_error_message().
470 1.1 elric *
471 1.1 elric * @ingroup krb5_keytab
472 1.1 elric */
473 1.1 elric
474 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
475 1.1 elric krb5_kt_close(krb5_context context,
476 1.1 elric krb5_keytab id)
477 1.1 elric {
478 1.1 elric krb5_error_code ret;
479 1.1 elric
480 1.1 elric ret = (*id->close)(context, id);
481 1.1 elric memset(id, 0, sizeof(*id));
482 1.1 elric free(id);
483 1.1 elric return ret;
484 1.1 elric }
485 1.1 elric
486 1.1 elric /**
487 1.1 elric * Destroy (remove) the keytab in `id'. All resources will be released,
488 1.1 elric * even on errors, does the equvalment of krb5_kt_close() on the resources.
489 1.1 elric *
490 1.1 elric * @param context a Keberos context.
491 1.1 elric * @param id keytab to destroy.
492 1.1 elric *
493 1.1 elric * @return Return an error code or 0, see krb5_get_error_message().
494 1.1 elric *
495 1.1 elric * @ingroup krb5_keytab
496 1.1 elric */
497 1.1 elric
498 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
499 1.1 elric krb5_kt_destroy(krb5_context context,
500 1.1 elric krb5_keytab id)
501 1.1 elric {
502 1.1 elric krb5_error_code ret;
503 1.1 elric
504 1.1 elric ret = (*id->destroy)(context, id);
505 1.1 elric krb5_kt_close(context, id);
506 1.1 elric return ret;
507 1.1 elric }
508 1.1 elric
509 1.1 elric /*
510 1.1 elric * Match any aliases in keytab `entry' with `principal'.
511 1.1 elric */
512 1.1 elric
513 1.1 elric static krb5_boolean
514 1.1 elric compare_aliseses(krb5_context context,
515 1.1 elric krb5_keytab_entry *entry,
516 1.1 elric krb5_const_principal principal)
517 1.1 elric {
518 1.1 elric unsigned int i;
519 1.1 elric if (entry->aliases == NULL)
520 1.1 elric return FALSE;
521 1.1 elric for (i = 0; i < entry->aliases->len; i++)
522 1.1 elric if (krb5_principal_compare(context, &entry->aliases->val[i], principal))
523 1.1 elric return TRUE;
524 1.1 elric return FALSE;
525 1.1 elric }
526 1.1 elric
527 1.1 elric /**
528 1.1 elric * Compare `entry' against `principal, vno, enctype'.
529 1.1 elric * Any of `principal, vno, enctype' might be 0 which acts as a wildcard.
530 1.1 elric * Return TRUE if they compare the same, FALSE otherwise.
531 1.1 elric *
532 1.1 elric * @param context a Keberos context.
533 1.1 elric * @param entry an entry to match with.
534 1.1 elric * @param principal principal to match, NULL matches all principals.
535 1.1 elric * @param vno key version to match, 0 matches all key version numbers.
536 1.1 elric * @param enctype encryption type to match, 0 matches all encryption types.
537 1.1 elric *
538 1.1 elric * @return Return TRUE or match, FALSE if not matched.
539 1.1 elric *
540 1.1 elric * @ingroup krb5_keytab
541 1.1 elric */
542 1.1 elric
543 1.1 elric KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
544 1.1 elric krb5_kt_compare(krb5_context context,
545 1.1 elric krb5_keytab_entry *entry,
546 1.1 elric krb5_const_principal principal,
547 1.1 elric krb5_kvno vno,
548 1.1 elric krb5_enctype enctype)
549 1.1 elric {
550 1.1 elric if(principal != NULL &&
551 1.1 elric !(krb5_principal_compare(context, entry->principal, principal) ||
552 1.1 elric compare_aliseses(context, entry, principal)))
553 1.1 elric return FALSE;
554 1.1 elric if(vno && vno != entry->vno)
555 1.1 elric return FALSE;
556 1.1 elric if(enctype && enctype != entry->keyblock.keytype)
557 1.1 elric return FALSE;
558 1.1 elric return TRUE;
559 1.1 elric }
560 1.1 elric
561 1.1 elric krb5_error_code
562 1.1 elric _krb5_kt_principal_not_found(krb5_context context,
563 1.1 elric krb5_error_code ret,
564 1.1 elric krb5_keytab id,
565 1.1 elric krb5_const_principal principal,
566 1.1 elric krb5_enctype enctype,
567 1.1 elric int kvno)
568 1.1 elric {
569 1.1 elric char princ[256], kvno_str[25], *kt_name;
570 1.1 elric char *enctype_str = NULL;
571 1.1 elric
572 1.1 elric krb5_unparse_name_fixed (context, principal, princ, sizeof(princ));
573 1.1 elric krb5_kt_get_full_name (context, id, &kt_name);
574 1.1 elric krb5_enctype_to_string(context, enctype, &enctype_str);
575 1.1 elric
576 1.1 elric if (kvno)
577 1.1 elric snprintf(kvno_str, sizeof(kvno_str), "(kvno %d)", kvno);
578 1.1 elric else
579 1.1 elric kvno_str[0] = '\0';
580 1.1 elric
581 1.1 elric krb5_set_error_message (context, ret,
582 1.1 elric N_("Failed to find %s%s in keytab %s (%s)",
583 1.1 elric "principal, kvno, keytab file, enctype"),
584 1.1 elric princ,
585 1.1 elric kvno_str,
586 1.1 elric kt_name ? kt_name : "unknown keytab",
587 1.1 elric enctype_str ? enctype_str : "unknown enctype");
588 1.1 elric free(kt_name);
589 1.1 elric free(enctype_str);
590 1.1 elric return ret;
591 1.1 elric }
592 1.1 elric
593 1.1 elric
594 1.1 elric /**
595 1.1 elric * Retrieve the keytab entry for `principal, kvno, enctype' into `entry'
596 1.1 elric * from the keytab `id'. Matching is done like krb5_kt_compare().
597 1.1 elric *
598 1.1 elric * @param context a Keberos context.
599 1.1 elric * @param id a keytab.
600 1.1 elric * @param principal principal to match, NULL matches all principals.
601 1.1 elric * @param kvno key version to match, 0 matches all key version numbers.
602 1.1 elric * @param enctype encryption type to match, 0 matches all encryption types.
603 1.1 elric * @param entry the returned entry, free with krb5_kt_free_entry().
604 1.1 elric *
605 1.1 elric * @return Return an error code or 0, see krb5_get_error_message().
606 1.1 elric *
607 1.1 elric * @ingroup krb5_keytab
608 1.1 elric */
609 1.1 elric
610 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
611 1.1 elric krb5_kt_get_entry(krb5_context context,
612 1.1 elric krb5_keytab id,
613 1.1 elric krb5_const_principal principal,
614 1.1 elric krb5_kvno kvno,
615 1.1 elric krb5_enctype enctype,
616 1.1 elric krb5_keytab_entry *entry)
617 1.1 elric {
618 1.1 elric krb5_keytab_entry tmp;
619 1.1 elric krb5_error_code ret;
620 1.1 elric krb5_kt_cursor cursor;
621 1.1 elric
622 1.1 elric if(id->get)
623 1.1 elric return (*id->get)(context, id, principal, kvno, enctype, entry);
624 1.1 elric
625 1.1 elric ret = krb5_kt_start_seq_get (context, id, &cursor);
626 1.1 elric if (ret) {
627 1.1 elric /* This is needed for krb5_verify_init_creds, but keep error
628 1.1 elric * string from previous error for the human. */
629 1.1 elric context->error_code = KRB5_KT_NOTFOUND;
630 1.1 elric return KRB5_KT_NOTFOUND;
631 1.1 elric }
632 1.1 elric
633 1.1 elric entry->vno = 0;
634 1.1 elric while (krb5_kt_next_entry(context, id, &tmp, &cursor) == 0) {
635 1.1 elric if (krb5_kt_compare(context, &tmp, principal, 0, enctype)) {
636 1.1 elric /* the file keytab might only store the lower 8 bits of
637 1.1 elric the kvno, so only compare those bits */
638 1.1 elric if (kvno == tmp.vno
639 1.1 elric || (tmp.vno < 256 && kvno % 256 == tmp.vno)) {
640 1.1 elric krb5_kt_copy_entry_contents (context, &tmp, entry);
641 1.1 elric krb5_kt_free_entry (context, &tmp);
642 1.1 elric krb5_kt_end_seq_get(context, id, &cursor);
643 1.1 elric return 0;
644 1.1 elric } else if (kvno == 0 && tmp.vno > entry->vno) {
645 1.1 elric if (entry->vno)
646 1.1 elric krb5_kt_free_entry (context, entry);
647 1.1 elric krb5_kt_copy_entry_contents (context, &tmp, entry);
648 1.1 elric }
649 1.1 elric }
650 1.1 elric krb5_kt_free_entry(context, &tmp);
651 1.1 elric }
652 1.1 elric krb5_kt_end_seq_get (context, id, &cursor);
653 1.1 elric if (entry->vno == 0)
654 1.1 elric return _krb5_kt_principal_not_found(context, KRB5_KT_NOTFOUND,
655 1.1 elric id, principal, enctype, kvno);
656 1.1 elric return 0;
657 1.1 elric }
658 1.1 elric
659 1.1 elric /**
660 1.1 elric * Copy the contents of `in' into `out'.
661 1.1 elric *
662 1.1 elric * @param context a Keberos context.
663 1.1 elric * @param in the keytab entry to copy.
664 1.1 elric * @param out the copy of the keytab entry, free with krb5_kt_free_entry().
665 1.1 elric *
666 1.1 elric * @return Return an error code or 0, see krb5_get_error_message().
667 1.1 elric *
668 1.1 elric * @ingroup krb5_keytab
669 1.1 elric */
670 1.1 elric
671 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
672 1.1 elric krb5_kt_copy_entry_contents(krb5_context context,
673 1.1 elric const krb5_keytab_entry *in,
674 1.1 elric krb5_keytab_entry *out)
675 1.1 elric {
676 1.1 elric krb5_error_code ret;
677 1.1 elric
678 1.1 elric memset(out, 0, sizeof(*out));
679 1.1 elric out->vno = in->vno;
680 1.1 elric
681 1.1 elric ret = krb5_copy_principal (context, in->principal, &out->principal);
682 1.1 elric if (ret)
683 1.1 elric goto fail;
684 1.1 elric ret = krb5_copy_keyblock_contents (context,
685 1.1 elric &in->keyblock,
686 1.1 elric &out->keyblock);
687 1.1 elric if (ret)
688 1.1 elric goto fail;
689 1.1 elric out->timestamp = in->timestamp;
690 1.1 elric return 0;
691 1.1 elric fail:
692 1.1 elric krb5_kt_free_entry (context, out);
693 1.1 elric return ret;
694 1.1 elric }
695 1.1 elric
696 1.1 elric /**
697 1.1 elric * Free the contents of `entry'.
698 1.1 elric *
699 1.1 elric * @param context a Keberos context.
700 1.1 elric * @param entry the entry to free
701 1.1 elric *
702 1.1 elric * @return Return an error code or 0, see krb5_get_error_message().
703 1.1 elric *
704 1.1 elric * @ingroup krb5_keytab
705 1.1 elric */
706 1.1 elric
707 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
708 1.1 elric krb5_kt_free_entry(krb5_context context,
709 1.1 elric krb5_keytab_entry *entry)
710 1.1 elric {
711 1.1 elric krb5_free_principal (context, entry->principal);
712 1.1 elric krb5_free_keyblock_contents (context, &entry->keyblock);
713 1.1 elric memset(entry, 0, sizeof(*entry));
714 1.1 elric return 0;
715 1.1 elric }
716 1.1 elric
717 1.1 elric /**
718 1.1 elric * Set `cursor' to point at the beginning of `id'.
719 1.1 elric *
720 1.1 elric * @param context a Keberos context.
721 1.1 elric * @param id a keytab.
722 1.1 elric * @param cursor a newly allocated cursor, free with krb5_kt_end_seq_get().
723 1.1 elric *
724 1.1 elric * @return Return an error code or 0, see krb5_get_error_message().
725 1.1 elric *
726 1.1 elric * @ingroup krb5_keytab
727 1.1 elric */
728 1.1 elric
729 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
730 1.1 elric krb5_kt_start_seq_get(krb5_context context,
731 1.1 elric krb5_keytab id,
732 1.1 elric krb5_kt_cursor *cursor)
733 1.1 elric {
734 1.1 elric if(id->start_seq_get == NULL) {
735 1.1 elric krb5_set_error_message(context, HEIM_ERR_OPNOTSUPP,
736 1.1 elric N_("start_seq_get is not supported "
737 1.1 elric "in the %s keytab type", ""),
738 1.1 elric id->prefix);
739 1.1 elric return HEIM_ERR_OPNOTSUPP;
740 1.1 elric }
741 1.1 elric return (*id->start_seq_get)(context, id, cursor);
742 1.1 elric }
743 1.1 elric
744 1.1 elric /**
745 1.1 elric * Get the next entry from keytab, advance the cursor. On last entry
746 1.1 elric * the function will return KRB5_KT_END.
747 1.1 elric *
748 1.1 elric * @param context a Keberos context.
749 1.1 elric * @param id a keytab.
750 1.1 elric * @param entry the returned entry, free with krb5_kt_free_entry().
751 1.1 elric * @param cursor the cursor of the iteration.
752 1.1 elric *
753 1.1 elric * @return Return an error code or 0, see krb5_get_error_message().
754 1.1 elric *
755 1.1 elric * @ingroup krb5_keytab
756 1.1 elric */
757 1.1 elric
758 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
759 1.1 elric krb5_kt_next_entry(krb5_context context,
760 1.1 elric krb5_keytab id,
761 1.1 elric krb5_keytab_entry *entry,
762 1.1 elric krb5_kt_cursor *cursor)
763 1.1 elric {
764 1.1 elric if(id->next_entry == NULL) {
765 1.1 elric krb5_set_error_message(context, HEIM_ERR_OPNOTSUPP,
766 1.1 elric N_("next_entry is not supported in the %s "
767 1.1 elric " keytab", ""),
768 1.1 elric id->prefix);
769 1.1 elric return HEIM_ERR_OPNOTSUPP;
770 1.1 elric }
771 1.1 elric return (*id->next_entry)(context, id, entry, cursor);
772 1.1 elric }
773 1.1 elric
774 1.1 elric /**
775 1.1 elric * Release all resources associated with `cursor'.
776 1.1 elric *
777 1.1 elric * @param context a Keberos context.
778 1.1 elric * @param id a keytab.
779 1.1 elric * @param cursor the cursor to free.
780 1.1 elric *
781 1.1 elric * @return Return an error code or 0, see krb5_get_error_message().
782 1.1 elric *
783 1.1 elric * @ingroup krb5_keytab
784 1.1 elric */
785 1.1 elric
786 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
787 1.1 elric krb5_kt_end_seq_get(krb5_context context,
788 1.1 elric krb5_keytab id,
789 1.1 elric krb5_kt_cursor *cursor)
790 1.1 elric {
791 1.1 elric if(id->end_seq_get == NULL) {
792 1.1 elric krb5_set_error_message(context, HEIM_ERR_OPNOTSUPP,
793 1.1 elric "end_seq_get is not supported in the %s "
794 1.1 elric " keytab", id->prefix);
795 1.1 elric return HEIM_ERR_OPNOTSUPP;
796 1.1 elric }
797 1.1 elric return (*id->end_seq_get)(context, id, cursor);
798 1.1 elric }
799 1.1 elric
800 1.1 elric /**
801 1.1 elric * Add the entry in `entry' to the keytab `id'.
802 1.1 elric *
803 1.1 elric * @param context a Keberos context.
804 1.1 elric * @param id a keytab.
805 1.1 elric * @param entry the entry to add
806 1.1 elric *
807 1.1 elric * @return Return an error code or 0, see krb5_get_error_message().
808 1.1 elric *
809 1.1 elric * @ingroup krb5_keytab
810 1.1 elric */
811 1.1 elric
812 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
813 1.1 elric krb5_kt_add_entry(krb5_context context,
814 1.1 elric krb5_keytab id,
815 1.1 elric krb5_keytab_entry *entry)
816 1.1 elric {
817 1.1 elric if(id->add == NULL) {
818 1.1 elric krb5_set_error_message(context, KRB5_KT_NOWRITE,
819 1.1 elric N_("Add is not supported in the %s keytab", ""),
820 1.1 elric id->prefix);
821 1.1 elric return KRB5_KT_NOWRITE;
822 1.1 elric }
823 1.1 elric entry->timestamp = time(NULL);
824 1.1 elric return (*id->add)(context, id,entry);
825 1.1 elric }
826 1.1 elric
827 1.1 elric /**
828 1.1 elric * Remove an entry from the keytab, matching is done using
829 1.1 elric * krb5_kt_compare().
830 1.1 elric
831 1.1 elric * @param context a Keberos context.
832 1.1 elric * @param id a keytab.
833 1.1 elric * @param entry the entry to remove
834 1.1 elric *
835 1.1 elric * @return Return an error code or 0, see krb5_get_error_message().
836 1.1 elric *
837 1.1 elric * @ingroup krb5_keytab
838 1.1 elric */
839 1.1 elric
840 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
841 1.1 elric krb5_kt_remove_entry(krb5_context context,
842 1.1 elric krb5_keytab id,
843 1.1 elric krb5_keytab_entry *entry)
844 1.1 elric {
845 1.1 elric if(id->remove == NULL) {
846 1.1 elric krb5_set_error_message(context, KRB5_KT_NOWRITE,
847 1.1 elric N_("Remove is not supported in the %s keytab", ""),
848 1.1 elric id->prefix);
849 1.1 elric return KRB5_KT_NOWRITE;
850 1.1 elric }
851 1.1 elric return (*id->remove)(context, id, entry);
852 1.1 elric }
853 1.1 elric
854 1.1 elric /**
855 1.1 elric * Return true if the keytab exists and have entries
856 1.1 elric *
857 1.1 elric * @param context a Keberos context.
858 1.1 elric * @param id a keytab.
859 1.1 elric *
860 1.1 elric * @return Return an error code or 0, see krb5_get_error_message().
861 1.1 elric *
862 1.1 elric * @ingroup krb5_keytab
863 1.1 elric */
864 1.1 elric
865 1.1 elric KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
866 1.1 elric krb5_kt_have_content(krb5_context context,
867 1.1 elric krb5_keytab id)
868 1.1 elric {
869 1.1 elric krb5_keytab_entry entry;
870 1.1 elric krb5_kt_cursor cursor;
871 1.1 elric krb5_error_code ret;
872 1.1 elric char *name;
873 1.1 elric
874 1.1 elric ret = krb5_kt_start_seq_get(context, id, &cursor);
875 1.1 elric if (ret)
876 1.1 elric goto notfound;
877 1.1 elric
878 1.1 elric ret = krb5_kt_next_entry(context, id, &entry, &cursor);
879 1.1 elric krb5_kt_end_seq_get(context, id, &cursor);
880 1.1 elric if (ret)
881 1.1 elric goto notfound;
882 1.1 elric
883 1.1 elric krb5_kt_free_entry(context, &entry);
884 1.1 elric
885 1.1 elric return 0;
886 1.1 elric
887 1.1 elric notfound:
888 1.1 elric ret = krb5_kt_get_full_name(context, id, &name);
889 1.1 elric if (ret == 0) {
890 1.1 elric krb5_set_error_message(context, KRB5_KT_NOTFOUND,
891 1.1 elric N_("No entry in keytab: %s", ""), name);
892 1.1 elric free(name);
893 1.1 elric }
894 1.1 elric return KRB5_KT_NOTFOUND;
895 1.1 elric }
896