Merge pull request #203 from sdigit/patch-1
[heimdal.git] / lib / krb5 / keytab.c
blobca37e292a4b373d370eda7704c1a73026ddc22f7
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 * Default ktname from context with possible environment
247 * override
249 static const char *default_ktname(krb5_context context)
251 const char *tmp = NULL;
253 if(!issuid())
254 tmp = getenv("KRB5_KTNAME");
255 if(tmp != NULL)
256 return tmp;
257 return context->default_keytab;
261 * copy the name of the default keytab into `name'.
263 * @param context a Keberos context.
264 * @param name buffer where the name will be written
265 * @param namesize length of name
267 * @return Return an error code or 0, see krb5_get_error_message().
269 * @ingroup krb5_keytab
272 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
273 krb5_kt_default_name(krb5_context context, char *name, size_t namesize)
275 if (strlcpy (name, default_ktname(context), namesize) >= namesize) {
276 krb5_clear_error_message (context);
277 return KRB5_CONFIG_NOTENUFSPACE;
279 return 0;
283 * Copy the name of the default modify keytab into `name'.
285 * @param context a Keberos context.
286 * @param name buffer where the name will be written
287 * @param namesize length of name
289 * @return Return an error code or 0, see krb5_get_error_message().
291 * @ingroup krb5_keytab
294 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
295 krb5_kt_default_modify_name(krb5_context context, char *name, size_t namesize)
297 const char *kt;
299 if(context->default_keytab_modify == NULL) {
300 kt = default_ktname(context);
302 if (strncasecmp(kt, "ANY:", 4) == 0) {
303 size_t len = strcspn(kt + 4, ",");
304 if (len >= namesize) {
305 krb5_clear_error_message(context);
306 return KRB5_CONFIG_NOTENUFSPACE;
308 strlcpy(name, kt + 4, namesize);
309 name[len] = '\0';
310 return 0;
312 } else
313 kt = context->default_keytab_modify;
314 if (strlcpy (name, kt, namesize) >= namesize) {
315 krb5_clear_error_message (context);
316 return KRB5_CONFIG_NOTENUFSPACE;
318 return 0;
322 * Set `id' to the default keytab.
324 * @param context a Keberos context.
325 * @param id the new default keytab.
327 * @return Return an error code or 0, see krb5_get_error_message().
329 * @ingroup krb5_keytab
332 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
333 krb5_kt_default(krb5_context context, krb5_keytab *id)
335 return krb5_kt_resolve (context, default_ktname(context), id);
339 * Read the key identified by `(principal, vno, enctype)' from the
340 * keytab in `keyprocarg' (the default if == NULL) into `*key'.
342 * @param context a Keberos context.
343 * @param keyprocarg
344 * @param principal
345 * @param vno
346 * @param enctype
347 * @param key
349 * @return Return an error code or 0, see krb5_get_error_message().
351 * @ingroup krb5_keytab
354 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
355 krb5_kt_read_service_key(krb5_context context,
356 krb5_pointer keyprocarg,
357 krb5_principal principal,
358 krb5_kvno vno,
359 krb5_enctype enctype,
360 krb5_keyblock **key)
362 krb5_keytab keytab;
363 krb5_keytab_entry entry;
364 krb5_error_code ret;
366 if (keyprocarg)
367 ret = krb5_kt_resolve (context, keyprocarg, &keytab);
368 else
369 ret = krb5_kt_default (context, &keytab);
371 if (ret)
372 return ret;
374 ret = krb5_kt_get_entry (context, keytab, principal, vno, enctype, &entry);
375 krb5_kt_close (context, keytab);
376 if (ret)
377 return ret;
378 ret = krb5_copy_keyblock (context, &entry.keyblock, key);
379 krb5_kt_free_entry(context, &entry);
380 return ret;
384 * Return the type of the `keytab' in the string `prefix of length
385 * `prefixsize'.
387 * @param context a Keberos context.
388 * @param keytab the keytab to get the prefix for
389 * @param prefix prefix buffer
390 * @param prefixsize length of prefix buffer
392 * @return Return an error code or 0, see krb5_get_error_message().
394 * @ingroup krb5_keytab
397 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
398 krb5_kt_get_type(krb5_context context,
399 krb5_keytab keytab,
400 char *prefix,
401 size_t prefixsize)
403 strlcpy(prefix, keytab->prefix, prefixsize);
404 return 0;
408 * Retrieve the name of the keytab `keytab' into `name', `namesize'
410 * @param context a Keberos context.
411 * @param keytab the keytab to get the name for.
412 * @param name name buffer.
413 * @param namesize size of name buffer.
415 * @return Return an error code or 0, see krb5_get_error_message().
417 * @ingroup krb5_keytab
420 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
421 krb5_kt_get_name(krb5_context context,
422 krb5_keytab keytab,
423 char *name,
424 size_t namesize)
426 return (*keytab->get_name)(context, keytab, name, namesize);
430 * Retrieve the full name of the keytab `keytab' and store the name in
431 * `str'.
433 * @param context a Keberos context.
434 * @param keytab keytab to get name for.
435 * @param str the name of the keytab name, usee krb5_xfree() to free
436 * the string. On error, *str is set to NULL.
438 * @return Return an error code or 0, see krb5_get_error_message().
440 * @ingroup krb5_keytab
443 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
444 krb5_kt_get_full_name(krb5_context context,
445 krb5_keytab keytab,
446 char **str)
448 char type[KRB5_KT_PREFIX_MAX_LEN];
449 char name[MAXPATHLEN];
450 krb5_error_code ret;
452 *str = NULL;
454 ret = krb5_kt_get_type(context, keytab, type, sizeof(type));
455 if (ret)
456 return ret;
458 ret = krb5_kt_get_name(context, keytab, name, sizeof(name));
459 if (ret)
460 return ret;
462 if (asprintf(str, "%s:%s", type, name) == -1) {
463 *str = NULL;
464 return krb5_enomem(context);
467 return 0;
471 * Finish using the keytab in `id'. All resources will be released,
472 * even on errors.
474 * @param context a Keberos context.
475 * @param id keytab to close.
477 * @return Return an error code or 0, see krb5_get_error_message().
479 * @ingroup krb5_keytab
482 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
483 krb5_kt_close(krb5_context context,
484 krb5_keytab id)
486 krb5_error_code ret;
488 ret = (*id->close)(context, id);
489 memset(id, 0, sizeof(*id));
490 free(id);
491 return ret;
495 * Destroy (remove) the keytab in `id'. All resources will be released,
496 * even on errors, does the equvalment of krb5_kt_close() on the resources.
498 * @param context a Keberos context.
499 * @param id keytab to destroy.
501 * @return Return an error code or 0, see krb5_get_error_message().
503 * @ingroup krb5_keytab
506 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
507 krb5_kt_destroy(krb5_context context,
508 krb5_keytab id)
510 krb5_error_code ret;
512 ret = (*id->destroy)(context, id);
513 krb5_kt_close(context, id);
514 return ret;
518 * Match any aliases in keytab `entry' with `principal'.
521 static krb5_boolean
522 compare_aliases(krb5_context context,
523 krb5_keytab_entry *entry,
524 krb5_const_principal principal)
526 unsigned int i;
527 if (entry->aliases == NULL)
528 return FALSE;
529 for (i = 0; i < entry->aliases->len; i++)
530 if (krb5_principal_compare(context, &entry->aliases->val[i], principal))
531 return TRUE;
532 return FALSE;
536 * Compare `entry' against `principal, vno, enctype'.
537 * Any of `principal, vno, enctype' might be 0 which acts as a wildcard.
538 * Return TRUE if they compare the same, FALSE otherwise.
540 * @param context a Keberos context.
541 * @param entry an entry to match with.
542 * @param principal principal to match, NULL matches all principals.
543 * @param vno key version to match, 0 matches all key version numbers.
544 * @param enctype encryption type to match, 0 matches all encryption types.
546 * @return Return TRUE or match, FALSE if not matched.
548 * @ingroup krb5_keytab
551 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
552 krb5_kt_compare(krb5_context context,
553 krb5_keytab_entry *entry,
554 krb5_const_principal principal,
555 krb5_kvno vno,
556 krb5_enctype enctype)
558 /* krb5_principal_compare() does not special-case the referral realm */
559 if (principal != NULL && strcmp(principal->realm, "") == 0 &&
560 !(krb5_principal_compare_any_realm(context, entry->principal, principal) ||
561 compare_aliases(context, entry, principal))) {
562 return FALSE;
563 } else if (principal != NULL && strcmp(principal->realm, "") != 0 &&
564 !(krb5_principal_compare(context, entry->principal, principal) ||
565 compare_aliases(context, entry, principal))) {
566 return FALSE;
568 if (vno && vno != entry->vno)
569 return FALSE;
570 if (enctype && enctype != entry->keyblock.keytype)
571 return FALSE;
572 return TRUE;
575 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
576 _krb5_kt_principal_not_found(krb5_context context,
577 krb5_error_code ret,
578 krb5_keytab id,
579 krb5_const_principal principal,
580 krb5_enctype enctype,
581 int kvno)
583 char princ[256], kvno_str[25], *kt_name;
584 char *enctype_str = NULL;
586 krb5_unparse_name_fixed (context, principal, princ, sizeof(princ));
587 krb5_kt_get_full_name (context, id, &kt_name);
588 if (enctype)
589 krb5_enctype_to_string(context, enctype, &enctype_str);
591 if (kvno)
592 snprintf(kvno_str, sizeof(kvno_str), "(kvno %d)", kvno);
593 else
594 kvno_str[0] = '\0';
596 krb5_set_error_message (context, ret,
597 N_("Failed to find %s%s in keytab %s (%s)",
598 "principal, kvno, keytab file, enctype"),
599 princ,
600 kvno_str,
601 kt_name ? kt_name : "unknown keytab",
602 enctype_str ? enctype_str : "unknown enctype");
603 free(kt_name);
604 if (enctype_str)
605 free(enctype_str);
606 return ret;
609 static krb5_error_code
610 krb5_kt_get_entry_wrapped(krb5_context context,
611 krb5_keytab id,
612 krb5_const_principal principal,
613 krb5_kvno kvno,
614 krb5_enctype enctype,
615 krb5_keytab_entry *entry)
617 krb5_keytab_entry tmp;
618 krb5_error_code ret;
619 krb5_kt_cursor cursor;
621 if(id->get)
622 return (*id->get)(context, id, principal, kvno, enctype, entry);
624 ret = krb5_kt_start_seq_get (context, id, &cursor);
625 if (ret) {
626 /* This is needed for krb5_verify_init_creds, but keep error
627 * string from previous error for the human. */
628 context->error_code = KRB5_KT_NOTFOUND;
629 return KRB5_KT_NOTFOUND;
632 entry->vno = 0;
633 while (krb5_kt_next_entry(context, id, &tmp, &cursor) == 0) {
634 if (krb5_kt_compare(context, &tmp, principal, 0, enctype)) {
635 /* the file keytab might only store the lower 8 bits of
636 the kvno, so only compare those bits */
637 if (kvno == tmp.vno
638 || (tmp.vno < 256 && kvno % 256 == tmp.vno)) {
639 krb5_kt_copy_entry_contents (context, &tmp, entry);
640 krb5_kt_free_entry (context, &tmp);
641 krb5_kt_end_seq_get(context, id, &cursor);
642 return 0;
643 } else if (kvno == 0 && tmp.vno > entry->vno) {
644 if (entry->vno)
645 krb5_kt_free_entry (context, entry);
646 krb5_kt_copy_entry_contents (context, &tmp, entry);
649 krb5_kt_free_entry(context, &tmp);
651 krb5_kt_end_seq_get (context, id, &cursor);
652 if (entry->vno == 0)
653 return _krb5_kt_principal_not_found(context, KRB5_KT_NOTFOUND,
654 id, principal, enctype, kvno);
655 return 0;
659 * Retrieve the keytab entry for `principal, kvno, enctype' into `entry'
660 * from the keytab `id'. Matching is done like krb5_kt_compare().
662 * @param context a Keberos context.
663 * @param id a keytab.
664 * @param principal principal to match, NULL matches all principals.
665 * @param kvno key version to match, 0 matches all key version numbers.
666 * @param enctype encryption type to match, 0 matches all encryption types.
667 * @param entry the returned entry, free with krb5_kt_free_entry().
669 * @return Return an error code or 0, see krb5_get_error_message().
671 * @ingroup krb5_keytab
674 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
675 krb5_kt_get_entry(krb5_context context,
676 krb5_keytab id,
677 krb5_const_principal principal,
678 krb5_kvno kvno,
679 krb5_enctype enctype,
680 krb5_keytab_entry *entry)
682 krb5_error_code ret;
683 krb5_const_principal try_princ;
684 krb5_name_canon_iterator name_canon_iter;
686 if (!principal)
687 return krb5_kt_get_entry_wrapped(context, id, principal, kvno, enctype,
688 entry);
690 ret = krb5_name_canon_iterator_start(context, principal, &name_canon_iter);
691 if (ret)
692 return ret;
694 do {
695 ret = krb5_name_canon_iterate(context, &name_canon_iter, &try_princ,
696 NULL);
697 if (ret)
698 break;
699 if (try_princ == NULL) {
700 ret = KRB5_KT_NOTFOUND;
701 continue;
703 ret = krb5_kt_get_entry_wrapped(context, id, try_princ, kvno,
704 enctype, entry);
705 } while (ret == KRB5_KT_NOTFOUND && name_canon_iter);
707 if (ret != KRB5_KT_NOTFOUND)
708 krb5_set_error_message(context, ret,
709 N_("Name canon failed while searching keytab",
710 ""));
711 krb5_free_name_canon_iterator(context, name_canon_iter);
712 return ret;
716 * Copy the contents of `in' into `out'.
718 * @param context a Keberos context.
719 * @param in the keytab entry to copy.
720 * @param out the copy of the keytab entry, free with krb5_kt_free_entry().
722 * @return Return an error code or 0, see krb5_get_error_message().
724 * @ingroup krb5_keytab
727 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
728 krb5_kt_copy_entry_contents(krb5_context context,
729 const krb5_keytab_entry *in,
730 krb5_keytab_entry *out)
732 krb5_error_code ret;
734 memset(out, 0, sizeof(*out));
735 out->vno = in->vno;
737 ret = krb5_copy_principal (context, in->principal, &out->principal);
738 if (ret)
739 goto fail;
740 ret = krb5_copy_keyblock_contents (context,
741 &in->keyblock,
742 &out->keyblock);
743 if (ret)
744 goto fail;
745 out->timestamp = in->timestamp;
746 return 0;
747 fail:
748 krb5_kt_free_entry (context, out);
749 return ret;
753 * Free the contents of `entry'.
755 * @param context a Keberos context.
756 * @param entry the entry to free
758 * @return Return an error code or 0, see krb5_get_error_message().
760 * @ingroup krb5_keytab
763 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
764 krb5_kt_free_entry(krb5_context context,
765 krb5_keytab_entry *entry)
767 krb5_free_principal (context, entry->principal);
768 krb5_free_keyblock_contents (context, &entry->keyblock);
769 memset(entry, 0, sizeof(*entry));
770 return 0;
774 * Set `cursor' to point at the beginning of `id'.
776 * @param context a Keberos context.
777 * @param id a keytab.
778 * @param cursor a newly allocated cursor, free with krb5_kt_end_seq_get().
780 * @return Return an error code or 0, see krb5_get_error_message().
782 * @ingroup krb5_keytab
785 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
786 krb5_kt_start_seq_get(krb5_context context,
787 krb5_keytab id,
788 krb5_kt_cursor *cursor)
790 if(id->start_seq_get == NULL) {
791 krb5_set_error_message(context, HEIM_ERR_OPNOTSUPP,
792 N_("start_seq_get is not supported "
793 "in the %s keytab type", ""),
794 id->prefix);
795 return HEIM_ERR_OPNOTSUPP;
797 return (*id->start_seq_get)(context, id, cursor);
801 * Get the next entry from keytab, advance the cursor. On last entry
802 * the function will return KRB5_KT_END.
804 * @param context a Keberos context.
805 * @param id a keytab.
806 * @param entry the returned entry, free with krb5_kt_free_entry().
807 * @param cursor the cursor of the iteration.
809 * @return Return an error code or 0, see krb5_get_error_message().
811 * @ingroup krb5_keytab
814 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
815 krb5_kt_next_entry(krb5_context context,
816 krb5_keytab id,
817 krb5_keytab_entry *entry,
818 krb5_kt_cursor *cursor)
820 if(id->next_entry == NULL) {
821 krb5_set_error_message(context, HEIM_ERR_OPNOTSUPP,
822 N_("next_entry is not supported in the %s "
823 " keytab", ""),
824 id->prefix);
825 return HEIM_ERR_OPNOTSUPP;
827 return (*id->next_entry)(context, id, entry, cursor);
831 * Release all resources associated with `cursor'.
833 * @param context a Keberos context.
834 * @param id a keytab.
835 * @param cursor the cursor to free.
837 * @return Return an error code or 0, see krb5_get_error_message().
839 * @ingroup krb5_keytab
842 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
843 krb5_kt_end_seq_get(krb5_context context,
844 krb5_keytab id,
845 krb5_kt_cursor *cursor)
847 if(id->end_seq_get == NULL) {
848 krb5_set_error_message(context, HEIM_ERR_OPNOTSUPP,
849 "end_seq_get is not supported in the %s "
850 " keytab", id->prefix);
851 return HEIM_ERR_OPNOTSUPP;
853 return (*id->end_seq_get)(context, id, cursor);
857 * Add the entry in `entry' to the keytab `id'.
859 * @param context a Keberos context.
860 * @param id a keytab.
861 * @param entry the entry to add
863 * @return Return an error code or 0, see krb5_get_error_message().
865 * @ingroup krb5_keytab
868 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
869 krb5_kt_add_entry(krb5_context context,
870 krb5_keytab id,
871 krb5_keytab_entry *entry)
873 if(id->add == NULL) {
874 krb5_set_error_message(context, KRB5_KT_NOWRITE,
875 N_("Add is not supported in the %s keytab", ""),
876 id->prefix);
877 return KRB5_KT_NOWRITE;
879 entry->timestamp = time(NULL);
880 return (*id->add)(context, id,entry);
884 * Remove an entry from the keytab, matching is done using
885 * krb5_kt_compare().
887 * @param context a Keberos context.
888 * @param id a keytab.
889 * @param entry the entry to remove
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_remove_entry(krb5_context context,
898 krb5_keytab id,
899 krb5_keytab_entry *entry)
901 if(id->remove == NULL) {
902 krb5_set_error_message(context, KRB5_KT_NOWRITE,
903 N_("Remove is not supported in the %s keytab", ""),
904 id->prefix);
905 return KRB5_KT_NOWRITE;
907 return (*id->remove)(context, id, entry);
911 * Return true if the keytab exists and have entries
913 * @param context a Keberos context.
914 * @param id a keytab.
916 * @return Return an error code or 0, see krb5_get_error_message().
918 * @ingroup krb5_keytab
921 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
922 krb5_kt_have_content(krb5_context context,
923 krb5_keytab id)
925 krb5_keytab_entry entry;
926 krb5_kt_cursor cursor;
927 krb5_error_code ret;
928 char *name;
930 ret = krb5_kt_start_seq_get(context, id, &cursor);
931 if (ret)
932 goto notfound;
934 ret = krb5_kt_next_entry(context, id, &entry, &cursor);
935 krb5_kt_end_seq_get(context, id, &cursor);
936 if (ret)
937 goto notfound;
939 krb5_kt_free_entry(context, &entry);
941 return 0;
943 notfound:
944 ret = krb5_kt_get_full_name(context, id, &name);
945 if (ret == 0) {
946 krb5_set_error_message(context, KRB5_KT_NOTFOUND,
947 N_("No entry in keytab: %s", ""), name);
948 free(name);
950 return KRB5_KT_NOTFOUND;