Use anon realm for anonymous PKINIT
[heimdal.git] / lib / krb5 / keytab.c
blob9cebe8d702fb26731e344610b564a4a414059e99
1 /*
2 * Copyright (c) 1997 - 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 the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "krb5_locl.h"
36 /**
37 * @page krb5_keytab_intro The keytab handing functions
38 * @section section_krb5_keytab Kerberos Keytabs
40 * See the library functions here: @ref krb5_keytab
42 * Keytabs are long term key storage for servers, their equvalment of
43 * password files.
45 * Normally the only function that useful for server are to specify
46 * what keytab to use to other core functions like krb5_rd_req()
47 * krb5_kt_resolve(), and krb5_kt_close().
49 * @subsection krb5_keytab_names Keytab names
51 * A keytab name is on the form type:residual. The residual part is
52 * specific to each keytab-type.
54 * When a keytab-name is resolved, the type is matched with an internal
55 * list of keytab types. If there is no matching keytab type,
56 * the default keytab is used. The current default type is FILE.
58 * The default value can be changed in the configuration file
59 * /etc/krb5.conf by setting the variable
60 * [defaults]default_keytab_name.
62 * The keytab types that are implemented in Heimdal are:
63 * - file
64 * store the keytab in a file, the type's name is FILE . The
65 * residual part is a filename. For compatibility with other
66 * Kerberos implemtation WRFILE and JAVA14 is also accepted. WRFILE
67 * has the same format as FILE. JAVA14 have a format that is
68 * compatible with older versions of MIT kerberos and SUN's Java
69 * based installation. They store a truncted kvno, so when the knvo
70 * excess 255, they are truncted in this format.
72 * - keytab
73 * store the keytab in a AFS keyfile (usually /usr/afs/etc/KeyFile ),
74 * the type's name is AFSKEYFILE. The residual part is a filename.
76 * - memory
77 * The keytab is stored in a memory segment. This allows sensitive
78 * and/or temporary data not to be stored on disk. The type's name
79 * is MEMORY. Each MEMORY keytab is referenced counted by and
80 * opened by the residual name, so two handles can point to the
81 * same memory area. When the last user closes using krb5_kt_close()
82 * the keytab, the keys in they keytab is memset() to zero and freed
83 * and can no longer be looked up by name.
86 * @subsection krb5_keytab_example Keytab example
88 * This is a minimalistic version of ktutil.
90 * @code
91 int
92 main (int argc, char **argv)
94 krb5_context context;
95 krb5_keytab keytab;
96 krb5_kt_cursor cursor;
97 krb5_keytab_entry entry;
98 krb5_error_code ret;
99 char *principal;
101 if (krb5_init_context (&context) != 0)
102 errx(1, "krb5_context");
104 ret = krb5_kt_default (context, &keytab);
105 if (ret)
106 krb5_err(context, 1, ret, "krb5_kt_default");
108 ret = krb5_kt_start_seq_get(context, keytab, &cursor);
109 if (ret)
110 krb5_err(context, 1, ret, "krb5_kt_start_seq_get");
111 while((ret = krb5_kt_next_entry(context, keytab, &entry, &cursor)) == 0){
112 krb5_unparse_name(context, entry.principal, &principal);
113 printf("principal: %s\n", principal);
114 free(principal);
115 krb5_kt_free_entry(context, &entry);
117 ret = krb5_kt_end_seq_get(context, keytab, &cursor);
118 if (ret)
119 krb5_err(context, 1, ret, "krb5_kt_end_seq_get");
120 ret = krb5_kt_close(context, keytab);
121 if (ret)
122 krb5_err(context, 1, ret, "krb5_kt_close");
123 krb5_free_context(context);
124 return 0;
126 * @endcode
132 * Register a new keytab backend.
134 * @param context a Keberos context.
135 * @param ops a backend to register.
137 * @return Return an error code or 0, see krb5_get_error_message().
139 * @ingroup krb5_keytab
142 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
143 krb5_kt_register(krb5_context context,
144 const krb5_kt_ops *ops)
146 struct krb5_keytab_data *tmp;
148 if (strlen(ops->prefix) > KRB5_KT_PREFIX_MAX_LEN - 1) {
149 krb5_set_error_message(context, KRB5_KT_BADNAME,
150 N_("can't register cache type, prefix too long", ""));
151 return KRB5_KT_BADNAME;
154 tmp = realloc(context->kt_types,
155 (context->num_kt_types + 1) * sizeof(*context->kt_types));
156 if(tmp == NULL)
157 return krb5_enomem(context);
158 memcpy(&tmp[context->num_kt_types], ops,
159 sizeof(tmp[context->num_kt_types]));
160 context->kt_types = tmp;
161 context->num_kt_types++;
162 return 0;
165 static const char *
166 keytab_name(const char *name, const char **type, size_t *type_len)
168 const char *residual;
170 residual = strchr(name, ':');
172 if (residual == NULL ||
173 ISPATHSEP(name[0])
174 #ifdef _WIN32
175 /* Avoid treating <drive>:<path> as a keytab type
176 * specification */
177 || name + 1 == residual
178 #endif
181 *type = "FILE";
182 *type_len = strlen(*type);
183 residual = name;
184 } else {
185 *type = name;
186 *type_len = residual - name;
187 residual++;
190 return residual;
194 * Resolve the keytab name (of the form `type:residual') in `name'
195 * into a keytab in `id'.
197 * @param context a Keberos context.
198 * @param name name to resolve
199 * @param id resulting keytab, free with krb5_kt_close().
201 * @return Return an error code or 0, see krb5_get_error_message().
203 * @ingroup krb5_keytab
207 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
208 krb5_kt_resolve(krb5_context context,
209 const char *name,
210 krb5_keytab *id)
212 krb5_keytab k;
213 int i;
214 const char *type, *residual;
215 size_t type_len;
216 krb5_error_code ret;
218 residual = keytab_name(name, &type, &type_len);
220 for(i = 0; i < context->num_kt_types; i++) {
221 if(strncasecmp(type, context->kt_types[i].prefix, type_len) == 0)
222 break;
224 if(i == context->num_kt_types) {
225 krb5_set_error_message(context, KRB5_KT_UNKNOWN_TYPE,
226 N_("unknown keytab type %.*s", "type"),
227 (int)type_len, type);
228 return KRB5_KT_UNKNOWN_TYPE;
231 k = malloc (sizeof(*k));
232 if (k == NULL)
233 return krb5_enomem(context);
234 memcpy(k, &context->kt_types[i], sizeof(*k));
235 k->data = NULL;
236 ret = (*k->resolve)(context, residual, k);
237 if(ret) {
238 free(k);
239 k = NULL;
241 *id = k;
242 return ret;
246 * copy the name of the default keytab into `name'.
248 * @param context a Keberos context.
249 * @param name buffer where the name will be written
250 * @param namesize length of name
252 * @return Return an error code or 0, see krb5_get_error_message().
254 * @ingroup krb5_keytab
257 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
258 krb5_kt_default_name(krb5_context context, char *name, size_t namesize)
260 if (strlcpy (name, context->default_keytab, namesize) >= namesize) {
261 krb5_clear_error_message (context);
262 return KRB5_CONFIG_NOTENUFSPACE;
264 return 0;
268 * Copy the name of the default modify keytab into `name'.
270 * @param context a Keberos context.
271 * @param name buffer where the name will be written
272 * @param namesize length of name
274 * @return Return an error code or 0, see krb5_get_error_message().
276 * @ingroup krb5_keytab
279 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
280 krb5_kt_default_modify_name(krb5_context context, char *name, size_t namesize)
282 const char *kt = NULL;
283 if(context->default_keytab_modify == NULL) {
284 if(strncasecmp(context->default_keytab, "ANY:", 4) != 0)
285 kt = context->default_keytab;
286 else {
287 size_t len = strcspn(context->default_keytab + 4, ",");
288 if(len >= namesize) {
289 krb5_clear_error_message(context);
290 return KRB5_CONFIG_NOTENUFSPACE;
292 strlcpy(name, context->default_keytab + 4, namesize);
293 name[len] = '\0';
294 return 0;
296 } else
297 kt = context->default_keytab_modify;
298 if (strlcpy (name, kt, namesize) >= namesize) {
299 krb5_clear_error_message (context);
300 return KRB5_CONFIG_NOTENUFSPACE;
302 return 0;
306 * Set `id' to the default keytab.
308 * @param context a Keberos context.
309 * @param id the new default keytab.
311 * @return Return an error code or 0, see krb5_get_error_message().
313 * @ingroup krb5_keytab
316 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
317 krb5_kt_default(krb5_context context, krb5_keytab *id)
319 return krb5_kt_resolve (context, context->default_keytab, id);
323 * Read the key identified by `(principal, vno, enctype)' from the
324 * keytab in `keyprocarg' (the default if == NULL) into `*key'.
326 * @param context a Keberos context.
327 * @param keyprocarg
328 * @param principal
329 * @param vno
330 * @param enctype
331 * @param key
333 * @return Return an error code or 0, see krb5_get_error_message().
335 * @ingroup krb5_keytab
338 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
339 krb5_kt_read_service_key(krb5_context context,
340 krb5_pointer keyprocarg,
341 krb5_principal principal,
342 krb5_kvno vno,
343 krb5_enctype enctype,
344 krb5_keyblock **key)
346 krb5_keytab keytab;
347 krb5_keytab_entry entry;
348 krb5_error_code ret;
350 if (keyprocarg)
351 ret = krb5_kt_resolve (context, keyprocarg, &keytab);
352 else
353 ret = krb5_kt_default (context, &keytab);
355 if (ret)
356 return ret;
358 ret = krb5_kt_get_entry (context, keytab, principal, vno, enctype, &entry);
359 krb5_kt_close (context, keytab);
360 if (ret)
361 return ret;
362 ret = krb5_copy_keyblock (context, &entry.keyblock, key);
363 krb5_kt_free_entry(context, &entry);
364 return ret;
368 * Return the type of the `keytab' in the string `prefix of length
369 * `prefixsize'.
371 * @param context a Keberos context.
372 * @param keytab the keytab to get the prefix for
373 * @param prefix prefix buffer
374 * @param prefixsize length of prefix buffer
376 * @return Return an error code or 0, see krb5_get_error_message().
378 * @ingroup krb5_keytab
381 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
382 krb5_kt_get_type(krb5_context context,
383 krb5_keytab keytab,
384 char *prefix,
385 size_t prefixsize)
387 strlcpy(prefix, keytab->prefix, prefixsize);
388 return 0;
392 * Retrieve the name of the keytab `keytab' into `name', `namesize'
394 * @param context a Keberos context.
395 * @param keytab the keytab to get the name for.
396 * @param name name buffer.
397 * @param namesize size of name buffer.
399 * @return Return an error code or 0, see krb5_get_error_message().
401 * @ingroup krb5_keytab
404 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
405 krb5_kt_get_name(krb5_context context,
406 krb5_keytab keytab,
407 char *name,
408 size_t namesize)
410 return (*keytab->get_name)(context, keytab, name, namesize);
414 * Retrieve the full name of the keytab `keytab' and store the name in
415 * `str'.
417 * @param context a Keberos context.
418 * @param keytab keytab to get name for.
419 * @param str the name of the keytab name, usee krb5_xfree() to free
420 * the string. On error, *str is set to NULL.
422 * @return Return an error code or 0, see krb5_get_error_message().
424 * @ingroup krb5_keytab
427 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
428 krb5_kt_get_full_name(krb5_context context,
429 krb5_keytab keytab,
430 char **str)
432 char type[KRB5_KT_PREFIX_MAX_LEN];
433 char name[MAXPATHLEN];
434 krb5_error_code ret;
436 *str = NULL;
438 ret = krb5_kt_get_type(context, keytab, type, sizeof(type));
439 if (ret)
440 return ret;
442 ret = krb5_kt_get_name(context, keytab, name, sizeof(name));
443 if (ret)
444 return ret;
446 if (asprintf(str, "%s:%s", type, name) == -1) {
447 *str = NULL;
448 return krb5_enomem(context);
451 return 0;
455 * Finish using the keytab in `id'. All resources will be released,
456 * even on errors.
458 * @param context a Keberos context.
459 * @param id keytab to close.
461 * @return Return an error code or 0, see krb5_get_error_message().
463 * @ingroup krb5_keytab
466 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
467 krb5_kt_close(krb5_context context,
468 krb5_keytab id)
470 krb5_error_code ret;
472 ret = (*id->close)(context, id);
473 memset(id, 0, sizeof(*id));
474 free(id);
475 return ret;
479 * Destroy (remove) the keytab in `id'. All resources will be released,
480 * even on errors, does the equvalment of krb5_kt_close() on the resources.
482 * @param context a Keberos context.
483 * @param id keytab to destroy.
485 * @return Return an error code or 0, see krb5_get_error_message().
487 * @ingroup krb5_keytab
490 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
491 krb5_kt_destroy(krb5_context context,
492 krb5_keytab id)
494 krb5_error_code ret;
496 ret = (*id->destroy)(context, id);
497 krb5_kt_close(context, id);
498 return ret;
502 * Match any aliases in keytab `entry' with `principal'.
505 static krb5_boolean
506 compare_aliseses(krb5_context context,
507 krb5_keytab_entry *entry,
508 krb5_const_principal principal)
510 unsigned int i;
511 if (entry->aliases == NULL)
512 return FALSE;
513 for (i = 0; i < entry->aliases->len; i++)
514 if (krb5_principal_compare(context, &entry->aliases->val[i], principal))
515 return TRUE;
516 return FALSE;
520 * Compare `entry' against `principal, vno, enctype'.
521 * Any of `principal, vno, enctype' might be 0 which acts as a wildcard.
522 * Return TRUE if they compare the same, FALSE otherwise.
524 * @param context a Keberos context.
525 * @param entry an entry to match with.
526 * @param principal principal to match, NULL matches all principals.
527 * @param vno key version to match, 0 matches all key version numbers.
528 * @param enctype encryption type to match, 0 matches all encryption types.
530 * @return Return TRUE or match, FALSE if not matched.
532 * @ingroup krb5_keytab
535 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
536 krb5_kt_compare(krb5_context context,
537 krb5_keytab_entry *entry,
538 krb5_const_principal principal,
539 krb5_kvno vno,
540 krb5_enctype enctype)
542 if(principal != NULL &&
543 !(krb5_principal_compare(context, entry->principal, principal) ||
544 compare_aliseses(context, entry, principal)))
545 return FALSE;
546 if(vno && vno != entry->vno)
547 return FALSE;
548 if(enctype && enctype != entry->keyblock.keytype)
549 return FALSE;
550 return TRUE;
553 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
554 _krb5_kt_principal_not_found(krb5_context context,
555 krb5_error_code ret,
556 krb5_keytab id,
557 krb5_const_principal principal,
558 krb5_enctype enctype,
559 int kvno)
561 char princ[256], kvno_str[25], *kt_name;
562 char *enctype_str = NULL;
564 krb5_unparse_name_fixed (context, principal, princ, sizeof(princ));
565 krb5_kt_get_full_name (context, id, &kt_name);
566 if (enctype)
567 krb5_enctype_to_string(context, enctype, &enctype_str);
569 if (kvno)
570 snprintf(kvno_str, sizeof(kvno_str), "(kvno %d)", kvno);
571 else
572 kvno_str[0] = '\0';
574 krb5_set_error_message (context, ret,
575 N_("Failed to find %s%s in keytab %s (%s)",
576 "principal, kvno, keytab file, enctype"),
577 princ,
578 kvno_str,
579 kt_name ? kt_name : "unknown keytab",
580 enctype_str ? enctype_str : "unknown enctype");
581 free(kt_name);
582 if (enctype_str)
583 free(enctype_str);
584 return ret;
587 static krb5_error_code
588 krb5_kt_get_entry_wrapped(krb5_context context,
589 krb5_keytab id,
590 krb5_const_principal principal,
591 krb5_kvno kvno,
592 krb5_enctype enctype,
593 krb5_keytab_entry *entry)
595 krb5_keytab_entry tmp;
596 krb5_error_code ret;
597 krb5_kt_cursor cursor;
599 if(id->get)
600 return (*id->get)(context, id, principal, kvno, enctype, entry);
602 ret = krb5_kt_start_seq_get (context, id, &cursor);
603 if (ret) {
604 /* This is needed for krb5_verify_init_creds, but keep error
605 * string from previous error for the human. */
606 context->error_code = KRB5_KT_NOTFOUND;
607 return KRB5_KT_NOTFOUND;
610 entry->vno = 0;
611 while (krb5_kt_next_entry(context, id, &tmp, &cursor) == 0) {
612 if (krb5_kt_compare(context, &tmp, principal, 0, enctype)) {
613 /* the file keytab might only store the lower 8 bits of
614 the kvno, so only compare those bits */
615 if (kvno == tmp.vno
616 || (tmp.vno < 256 && kvno % 256 == tmp.vno)) {
617 krb5_kt_copy_entry_contents (context, &tmp, entry);
618 krb5_kt_free_entry (context, &tmp);
619 krb5_kt_end_seq_get(context, id, &cursor);
620 return 0;
621 } else if (kvno == 0 && tmp.vno > entry->vno) {
622 if (entry->vno)
623 krb5_kt_free_entry (context, entry);
624 krb5_kt_copy_entry_contents (context, &tmp, entry);
627 krb5_kt_free_entry(context, &tmp);
629 krb5_kt_end_seq_get (context, id, &cursor);
630 if (entry->vno == 0)
631 return _krb5_kt_principal_not_found(context, KRB5_KT_NOTFOUND,
632 id, principal, enctype, kvno);
633 return 0;
637 * Retrieve the keytab entry for `principal, kvno, enctype' into `entry'
638 * from the keytab `id'. Matching is done like krb5_kt_compare().
640 * @param context a Keberos context.
641 * @param id a keytab.
642 * @param principal principal to match, NULL matches all principals.
643 * @param kvno key version to match, 0 matches all key version numbers.
644 * @param enctype encryption type to match, 0 matches all encryption types.
645 * @param entry the returned entry, free with krb5_kt_free_entry().
647 * @return Return an error code or 0, see krb5_get_error_message().
649 * @ingroup krb5_keytab
652 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
653 krb5_kt_get_entry(krb5_context context,
654 krb5_keytab id,
655 krb5_const_principal principal,
656 krb5_kvno kvno,
657 krb5_enctype enctype,
658 krb5_keytab_entry *entry)
660 krb5_error_code ret;
661 krb5_principal try_princ;
662 krb5_name_canon_iterator name_canon_iter;
664 if (!principal || principal->name.name_type != KRB5_NT_SRV_HST_NEEDS_CANON)
665 return krb5_kt_get_entry_wrapped(context, id, principal, kvno, enctype,
666 entry);
668 ret = krb5_name_canon_iterator_start(context, principal, NULL,
669 &name_canon_iter);
670 if (ret)
671 return ret;
673 do {
674 ret = krb5_name_canon_iterate_princ(context, &name_canon_iter,
675 &try_princ, NULL);
676 if (ret)
677 break;
678 ret = krb5_kt_get_entry_wrapped(context, id, try_princ, kvno,
679 enctype, entry);
680 } while (ret == KRB5_KT_NOTFOUND && name_canon_iter);
682 if (ret != KRB5_KT_NOTFOUND)
683 krb5_set_error_message(context, ret,
684 N_("Name canon failed while searching keytab",
685 ""));
686 krb5_free_name_canon_iterator(context, name_canon_iter);
687 return ret;
691 * Copy the contents of `in' into `out'.
693 * @param context a Keberos context.
694 * @param in the keytab entry to copy.
695 * @param out the copy of the keytab entry, free with krb5_kt_free_entry().
697 * @return Return an error code or 0, see krb5_get_error_message().
699 * @ingroup krb5_keytab
702 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
703 krb5_kt_copy_entry_contents(krb5_context context,
704 const krb5_keytab_entry *in,
705 krb5_keytab_entry *out)
707 krb5_error_code ret;
709 memset(out, 0, sizeof(*out));
710 out->vno = in->vno;
712 ret = krb5_copy_principal (context, in->principal, &out->principal);
713 if (ret)
714 goto fail;
715 ret = krb5_copy_keyblock_contents (context,
716 &in->keyblock,
717 &out->keyblock);
718 if (ret)
719 goto fail;
720 out->timestamp = in->timestamp;
721 return 0;
722 fail:
723 krb5_kt_free_entry (context, out);
724 return ret;
728 * Free the contents of `entry'.
730 * @param context a Keberos context.
731 * @param entry the entry to free
733 * @return Return an error code or 0, see krb5_get_error_message().
735 * @ingroup krb5_keytab
738 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
739 krb5_kt_free_entry(krb5_context context,
740 krb5_keytab_entry *entry)
742 krb5_free_principal (context, entry->principal);
743 krb5_free_keyblock_contents (context, &entry->keyblock);
744 memset(entry, 0, sizeof(*entry));
745 return 0;
749 * Set `cursor' to point at the beginning of `id'.
751 * @param context a Keberos context.
752 * @param id a keytab.
753 * @param cursor a newly allocated cursor, free with krb5_kt_end_seq_get().
755 * @return Return an error code or 0, see krb5_get_error_message().
757 * @ingroup krb5_keytab
760 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
761 krb5_kt_start_seq_get(krb5_context context,
762 krb5_keytab id,
763 krb5_kt_cursor *cursor)
765 if(id->start_seq_get == NULL) {
766 krb5_set_error_message(context, HEIM_ERR_OPNOTSUPP,
767 N_("start_seq_get is not supported "
768 "in the %s keytab type", ""),
769 id->prefix);
770 return HEIM_ERR_OPNOTSUPP;
772 return (*id->start_seq_get)(context, id, cursor);
776 * Get the next entry from keytab, advance the cursor. On last entry
777 * the function will return KRB5_KT_END.
779 * @param context a Keberos context.
780 * @param id a keytab.
781 * @param entry the returned entry, free with krb5_kt_free_entry().
782 * @param cursor the cursor of the iteration.
784 * @return Return an error code or 0, see krb5_get_error_message().
786 * @ingroup krb5_keytab
789 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
790 krb5_kt_next_entry(krb5_context context,
791 krb5_keytab id,
792 krb5_keytab_entry *entry,
793 krb5_kt_cursor *cursor)
795 if(id->next_entry == NULL) {
796 krb5_set_error_message(context, HEIM_ERR_OPNOTSUPP,
797 N_("next_entry is not supported in the %s "
798 " keytab", ""),
799 id->prefix);
800 return HEIM_ERR_OPNOTSUPP;
802 return (*id->next_entry)(context, id, entry, cursor);
806 * Release all resources associated with `cursor'.
808 * @param context a Keberos context.
809 * @param id a keytab.
810 * @param cursor the cursor to free.
812 * @return Return an error code or 0, see krb5_get_error_message().
814 * @ingroup krb5_keytab
817 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
818 krb5_kt_end_seq_get(krb5_context context,
819 krb5_keytab id,
820 krb5_kt_cursor *cursor)
822 if(id->end_seq_get == NULL) {
823 krb5_set_error_message(context, HEIM_ERR_OPNOTSUPP,
824 "end_seq_get is not supported in the %s "
825 " keytab", id->prefix);
826 return HEIM_ERR_OPNOTSUPP;
828 return (*id->end_seq_get)(context, id, cursor);
832 * Add the entry in `entry' to the keytab `id'.
834 * @param context a Keberos context.
835 * @param id a keytab.
836 * @param entry the entry to add
838 * @return Return an error code or 0, see krb5_get_error_message().
840 * @ingroup krb5_keytab
843 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
844 krb5_kt_add_entry(krb5_context context,
845 krb5_keytab id,
846 krb5_keytab_entry *entry)
848 if(id->add == NULL) {
849 krb5_set_error_message(context, KRB5_KT_NOWRITE,
850 N_("Add is not supported in the %s keytab", ""),
851 id->prefix);
852 return KRB5_KT_NOWRITE;
854 entry->timestamp = time(NULL);
855 return (*id->add)(context, id,entry);
859 * Remove an entry from the keytab, matching is done using
860 * krb5_kt_compare().
862 * @param context a Keberos context.
863 * @param id a keytab.
864 * @param entry the entry to remove
866 * @return Return an error code or 0, see krb5_get_error_message().
868 * @ingroup krb5_keytab
871 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
872 krb5_kt_remove_entry(krb5_context context,
873 krb5_keytab id,
874 krb5_keytab_entry *entry)
876 if(id->remove == NULL) {
877 krb5_set_error_message(context, KRB5_KT_NOWRITE,
878 N_("Remove is not supported in the %s keytab", ""),
879 id->prefix);
880 return KRB5_KT_NOWRITE;
882 return (*id->remove)(context, id, entry);
886 * Return true if the keytab exists and have entries
888 * @param context a Keberos context.
889 * @param id a keytab.
891 * @return Return an error code or 0, see krb5_get_error_message().
893 * @ingroup krb5_keytab
896 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
897 krb5_kt_have_content(krb5_context context,
898 krb5_keytab id)
900 krb5_keytab_entry entry;
901 krb5_kt_cursor cursor;
902 krb5_error_code ret;
903 char *name;
905 ret = krb5_kt_start_seq_get(context, id, &cursor);
906 if (ret)
907 goto notfound;
909 ret = krb5_kt_next_entry(context, id, &entry, &cursor);
910 krb5_kt_end_seq_get(context, id, &cursor);
911 if (ret)
912 goto notfound;
914 krb5_kt_free_entry(context, &entry);
916 return 0;
918 notfound:
919 ret = krb5_kt_get_full_name(context, id, &name);
920 if (ret == 0) {
921 krb5_set_error_message(context, KRB5_KT_NOTFOUND,
922 N_("No entry in keytab: %s", ""), name);
923 free(name);
925 return KRB5_KT_NOTFOUND;