krb5: Do not clobber keytab entry timestamps
[heimdal.git] / lib / krb5 / keytab.c
blobbcb3ed83733172713322ef7464c675c7e27c196f
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 tmp = secure_getenv("KRB5_KTNAME");
254 if(tmp != NULL)
255 return tmp;
256 return context->default_keytab;
260 * copy the name of the default keytab into `name'.
262 * @param context a Keberos context.
263 * @param name buffer where the name will be written
264 * @param namesize length of name
266 * @return Return an error code or 0, see krb5_get_error_message().
268 * @ingroup krb5_keytab
271 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
272 krb5_kt_default_name(krb5_context context, char *name, size_t namesize)
274 if (strlcpy (name, default_ktname(context), namesize) >= namesize) {
275 krb5_clear_error_message (context);
276 return KRB5_CONFIG_NOTENUFSPACE;
278 return 0;
282 * Copy the name of the default modify keytab into `name'.
284 * @param context a Keberos context.
285 * @param name buffer where the name will be written
286 * @param namesize length of name
288 * @return Return an error code or 0, see krb5_get_error_message().
290 * @ingroup krb5_keytab
293 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
294 krb5_kt_default_modify_name(krb5_context context, char *name, size_t namesize)
296 const char *kt;
298 if(context->default_keytab_modify == NULL) {
299 kt = default_ktname(context);
301 if (strncasecmp(kt, "ANY:", 4) == 0) {
302 size_t len = strcspn(kt + 4, ",");
303 if (len >= namesize) {
304 krb5_clear_error_message(context);
305 return KRB5_CONFIG_NOTENUFSPACE;
307 strlcpy(name, kt + 4, namesize);
308 name[len] = '\0';
309 return 0;
311 } else
312 kt = context->default_keytab_modify;
313 if (strlcpy (name, kt, namesize) >= namesize) {
314 krb5_clear_error_message (context);
315 return KRB5_CONFIG_NOTENUFSPACE;
317 return 0;
321 * Set `id' to the default keytab.
323 * @param context a Keberos context.
324 * @param id the new default keytab.
326 * @return Return an error code or 0, see krb5_get_error_message().
328 * @ingroup krb5_keytab
331 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
332 krb5_kt_default(krb5_context context, krb5_keytab *id)
334 return krb5_kt_resolve (context, default_ktname(context), id);
338 * Read the key identified by `(principal, vno, enctype)' from the
339 * keytab in `keyprocarg' (the default if == NULL) into `*key'.
341 * @param context a Keberos context.
342 * @param keyprocarg
343 * @param principal
344 * @param vno
345 * @param enctype
346 * @param key
348 * @return Return an error code or 0, see krb5_get_error_message().
350 * @ingroup krb5_keytab
353 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
354 krb5_kt_read_service_key(krb5_context context,
355 krb5_pointer keyprocarg,
356 krb5_principal principal,
357 krb5_kvno vno,
358 krb5_enctype enctype,
359 krb5_keyblock **key)
361 krb5_keytab keytab = NULL; /* Quiet lint */
362 krb5_keytab_entry entry;
363 krb5_error_code ret;
365 memset(&entry, 0, sizeof(entry));
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 if (ret == 0) {
376 ret = krb5_copy_keyblock (context, &entry.keyblock, key);
377 krb5_kt_free_entry(context, &entry);
379 krb5_kt_close (context, keytab);
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 = 0;
488 if (id) {
489 ret = (id->close)(context, id);
490 memset(id, 0, sizeof(*id));
491 free(id);
493 return ret;
497 * Destroy (remove) the keytab in `id'. All resources will be released,
498 * even on errors, does the equvalment of krb5_kt_close() on the resources.
500 * @param context a Keberos context.
501 * @param id keytab to destroy.
503 * @return Return an error code or 0, see krb5_get_error_message().
505 * @ingroup krb5_keytab
508 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
509 krb5_kt_destroy(krb5_context context,
510 krb5_keytab id)
512 krb5_error_code ret;
514 ret = (*id->destroy)(context, id);
515 krb5_kt_close(context, id);
516 return ret;
520 * Match any aliases in keytab `entry' with `principal'.
523 static krb5_boolean
524 compare_aliases(krb5_context context,
525 krb5_keytab_entry *entry,
526 krb5_const_principal principal)
528 unsigned int i;
529 if (entry->aliases == NULL)
530 return FALSE;
531 for (i = 0; i < entry->aliases->len; i++)
532 if (krb5_principal_compare(context, &entry->aliases->val[i], principal))
533 return TRUE;
534 return FALSE;
538 * Compare `entry' against `principal, vno, enctype'.
539 * Any of `principal, vno, enctype' might be 0 which acts as a wildcard.
540 * Return TRUE if they compare the same, FALSE otherwise.
542 * @param context a Keberos context.
543 * @param entry an entry to match with.
544 * @param principal principal to match, NULL matches all principals.
545 * @param vno key version to match, 0 matches all key version numbers.
546 * @param enctype encryption type to match, 0 matches all encryption types.
548 * @return Return TRUE or match, FALSE if not matched.
550 * @ingroup krb5_keytab
553 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
554 krb5_kt_compare(krb5_context context,
555 krb5_keytab_entry *entry,
556 krb5_const_principal principal,
557 krb5_kvno vno,
558 krb5_enctype enctype)
560 /* krb5_principal_compare() does not special-case the referral realm */
561 if (principal != NULL && strcmp(principal->realm, "") == 0 &&
562 !(krb5_principal_compare_any_realm(context, entry->principal, principal) ||
563 compare_aliases(context, entry, principal))) {
564 return FALSE;
565 } else if (principal != NULL && strcmp(principal->realm, "") != 0 &&
566 !(krb5_principal_compare(context, entry->principal, principal) ||
567 compare_aliases(context, entry, principal))) {
568 return FALSE;
570 if (vno && vno != entry->vno)
571 return FALSE;
572 if (enctype && enctype != entry->keyblock.keytype)
573 return FALSE;
574 return TRUE;
577 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
578 _krb5_kt_principal_not_found(krb5_context context,
579 krb5_error_code ret,
580 krb5_keytab id,
581 krb5_const_principal principal,
582 krb5_enctype enctype,
583 int kvno)
585 char kvno_str[25];
586 char *enctype_str = NULL;
587 char *kt_name = NULL;
588 char *princ = NULL;
590 (void) krb5_unparse_name(context, principal, &princ);
591 (void) krb5_kt_get_full_name(context, id, &kt_name);
592 if (enctype)
593 (void) krb5_enctype_to_string(context, enctype, &enctype_str);
595 if (kvno)
596 snprintf(kvno_str, sizeof(kvno_str), "(kvno %d)", kvno);
597 else
598 kvno_str[0] = '\0';
600 krb5_set_error_message(context, ret,
601 N_("Failed to find %s%s in keytab %s (%s)",
602 "principal, kvno, keytab file, enctype"),
603 princ ? princ : "<unknown>",
604 kvno_str,
605 kt_name ? kt_name : "unknown keytab",
606 enctype_str ? enctype_str : "unknown enctype");
607 free(princ);
608 free(kt_name);
609 free(enctype_str);
610 return ret;
613 static krb5_error_code
614 krb5_kt_get_entry_wrapped(krb5_context context,
615 krb5_keytab id,
616 krb5_const_principal principal,
617 krb5_kvno kvno,
618 krb5_enctype enctype,
619 krb5_keytab_entry *entry)
621 krb5_keytab_entry tmp;
622 krb5_error_code ret;
623 krb5_kt_cursor cursor;
625 if(id->get)
626 return (*id->get)(context, id, principal, kvno, enctype, entry);
628 memset(&tmp, 0, sizeof(tmp));
629 ret = krb5_kt_start_seq_get (context, id, &cursor);
630 if (ret) {
631 /* This is needed for krb5_verify_init_creds, but keep error
632 * string from previous error for the human. */
633 context->error_code = KRB5_KT_NOTFOUND;
634 return KRB5_KT_NOTFOUND;
637 entry->vno = 0;
638 while (krb5_kt_next_entry(context, id, &tmp, &cursor) == 0) {
639 if (krb5_kt_compare(context, &tmp, principal, 0, enctype)) {
640 /* the file keytab might only store the lower 8 bits of
641 the kvno, so only compare those bits */
642 if (kvno == tmp.vno
643 || (tmp.vno < 256 && kvno % 256 == tmp.vno)) {
644 krb5_kt_copy_entry_contents (context, &tmp, entry);
645 krb5_kt_free_entry (context, &tmp);
646 krb5_kt_end_seq_get(context, id, &cursor);
647 return 0;
648 } else if (kvno == 0 && tmp.vno > entry->vno) {
649 if (entry->vno)
650 krb5_kt_free_entry (context, entry);
651 krb5_kt_copy_entry_contents (context, &tmp, entry);
654 krb5_kt_free_entry(context, &tmp);
656 krb5_kt_end_seq_get (context, id, &cursor);
657 if (entry->vno == 0)
658 return _krb5_kt_principal_not_found(context, KRB5_KT_NOTFOUND,
659 id, principal, enctype, kvno);
660 return 0;
664 * Retrieve the keytab entry for `principal, kvno, enctype' into `entry'
665 * from the keytab `id'. Matching is done like krb5_kt_compare().
667 * @param context a Keberos context.
668 * @param id a keytab.
669 * @param principal principal to match, NULL matches all principals.
670 * @param kvno key version to match, 0 matches all key version numbers.
671 * @param enctype encryption type to match, 0 matches all encryption types.
672 * @param entry the returned entry, free with krb5_kt_free_entry().
674 * @return Return an error code or 0, see krb5_get_error_message().
676 * @ingroup krb5_keytab
679 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
680 krb5_kt_get_entry(krb5_context context,
681 krb5_keytab id,
682 krb5_const_principal principal,
683 krb5_kvno kvno,
684 krb5_enctype enctype,
685 krb5_keytab_entry *entry)
687 krb5_error_code ret;
688 krb5_const_principal try_princ;
689 krb5_name_canon_iterator name_canon_iter;
691 if (!principal)
692 /* Use `NULL' instead of `principal' to quiet static analizers */
693 return krb5_kt_get_entry_wrapped(context, id, NULL, kvno, enctype,
694 entry);
696 ret = krb5_name_canon_iterator_start(context, principal, &name_canon_iter);
697 if (ret)
698 return ret;
700 do {
701 ret = krb5_name_canon_iterate(context, &name_canon_iter, &try_princ,
702 NULL);
703 if (ret)
704 break;
705 if (try_princ == NULL) {
706 ret = KRB5_KT_NOTFOUND;
707 continue;
709 ret = krb5_kt_get_entry_wrapped(context, id, try_princ, kvno,
710 enctype, entry);
711 } while (ret == KRB5_KT_NOTFOUND && name_canon_iter);
713 if (ret && ret != KRB5_KT_NOTFOUND)
714 krb5_set_error_message(context, ret,
715 N_("Name canon failed while searching keytab",
716 ""));
717 krb5_free_name_canon_iterator(context, name_canon_iter);
718 return ret;
722 * Copy the contents of `in' into `out'.
724 * @param context a Keberos context.
725 * @param in the keytab entry to copy.
726 * @param out the copy of the keytab entry, free with krb5_kt_free_entry().
728 * @return Return an error code or 0, see krb5_get_error_message().
730 * @ingroup krb5_keytab
733 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
734 krb5_kt_copy_entry_contents(krb5_context context,
735 const krb5_keytab_entry *in,
736 krb5_keytab_entry *out)
738 krb5_error_code ret;
740 memset(out, 0, sizeof(*out));
742 ret = krb5_copy_principal (context, in->principal, &out->principal);
743 if (ret)
744 return ret;
745 ret = krb5_copy_keyblock_contents (context,
746 &in->keyblock,
747 &out->keyblock);
748 if (ret) {
749 krb5_free_principal(context, out->principal);
750 memset(out, 0, sizeof(*out));
751 return ret;
753 out->vno = in->vno;
754 out->timestamp = in->timestamp;
755 return 0;
759 * Free the contents of `entry'.
761 * @param context a Keberos context.
762 * @param entry the entry to free
764 * @return Return an error code or 0, see krb5_get_error_message().
766 * @ingroup krb5_keytab
769 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
770 krb5_kt_free_entry(krb5_context context,
771 krb5_keytab_entry *entry)
773 krb5_free_principal (context, entry->principal);
774 krb5_free_keyblock_contents (context, &entry->keyblock);
775 memset(entry, 0, sizeof(*entry));
776 return 0;
780 * Set `cursor' to point at the beginning of `id'.
782 * @param context a Keberos context.
783 * @param id a keytab.
784 * @param cursor a newly allocated cursor, free with krb5_kt_end_seq_get().
786 * @return Return an error code or 0, see krb5_get_error_message().
788 * @ingroup krb5_keytab
791 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
792 krb5_kt_start_seq_get(krb5_context context,
793 krb5_keytab id,
794 krb5_kt_cursor *cursor)
796 if(id->start_seq_get == NULL) {
797 krb5_set_error_message(context, HEIM_ERR_OPNOTSUPP,
798 N_("start_seq_get is not supported "
799 "in the %s keytab type", ""),
800 id->prefix);
801 return HEIM_ERR_OPNOTSUPP;
803 return (*id->start_seq_get)(context, id, cursor);
807 * Get the next entry from keytab, advance the cursor. On last entry
808 * the function will return KRB5_KT_END.
810 * @param context a Keberos context.
811 * @param id a keytab.
812 * @param entry the returned entry, free with krb5_kt_free_entry().
813 * @param cursor the cursor of the iteration.
815 * @return Return an error code or 0, see krb5_get_error_message().
817 * @ingroup krb5_keytab
820 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
821 krb5_kt_next_entry(krb5_context context,
822 krb5_keytab id,
823 krb5_keytab_entry *entry,
824 krb5_kt_cursor *cursor)
826 if(id->next_entry == NULL) {
827 krb5_set_error_message(context, HEIM_ERR_OPNOTSUPP,
828 N_("next_entry is not supported in the %s "
829 " keytab", ""),
830 id->prefix);
831 return HEIM_ERR_OPNOTSUPP;
833 memset(entry, 0x0, sizeof(*entry));
834 return (*id->next_entry)(context, id, entry, cursor);
838 * Release all resources associated with `cursor'.
840 * @param context a Keberos context.
841 * @param id a keytab.
842 * @param cursor the cursor to free.
844 * @return Return an error code or 0, see krb5_get_error_message().
846 * @ingroup krb5_keytab
849 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
850 krb5_kt_end_seq_get(krb5_context context,
851 krb5_keytab id,
852 krb5_kt_cursor *cursor)
854 if(id->end_seq_get == NULL) {
855 krb5_set_error_message(context, HEIM_ERR_OPNOTSUPP,
856 "end_seq_get is not supported in the %s "
857 " keytab", id->prefix);
858 return HEIM_ERR_OPNOTSUPP;
860 return (*id->end_seq_get)(context, id, cursor);
864 * Add the entry in `entry' to the keytab `id'.
866 * @param context a Keberos context.
867 * @param id a keytab.
868 * @param entry the entry to add
870 * @return Return an error code or 0, see krb5_get_error_message().
872 * @ingroup krb5_keytab
875 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
876 krb5_kt_add_entry(krb5_context context,
877 krb5_keytab id,
878 krb5_keytab_entry *entry)
880 if(id->add == NULL) {
881 krb5_set_error_message(context, KRB5_KT_NOWRITE,
882 N_("Add is not supported in the %s keytab", ""),
883 id->prefix);
884 return KRB5_KT_NOWRITE;
886 if (entry->timestamp == 0)
887 entry->timestamp = time(NULL);
888 return (*id->add)(context, id,entry);
892 * Remove an entry from the keytab, matching is done using
893 * krb5_kt_compare().
895 * @param context a Keberos context.
896 * @param id a keytab.
897 * @param entry the entry to remove
899 * @return Return an error code or 0, see krb5_get_error_message().
901 * @ingroup krb5_keytab
904 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
905 krb5_kt_remove_entry(krb5_context context,
906 krb5_keytab id,
907 krb5_keytab_entry *entry)
909 if(id->remove == NULL) {
910 krb5_set_error_message(context, KRB5_KT_NOWRITE,
911 N_("Remove is not supported in the %s keytab", ""),
912 id->prefix);
913 return KRB5_KT_NOWRITE;
915 return (*id->remove)(context, id, entry);
919 * Return true if the keytab exists and have entries
921 * @param context a Keberos context.
922 * @param id a keytab.
924 * @return Return an error code or 0, see krb5_get_error_message().
926 * @ingroup krb5_keytab
929 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
930 krb5_kt_have_content(krb5_context context,
931 krb5_keytab id)
933 krb5_keytab_entry entry;
934 krb5_kt_cursor cursor;
935 krb5_error_code ret;
936 char *name;
938 memset(&entry, 0, sizeof(entry));
939 ret = krb5_kt_start_seq_get(context, id, &cursor);
940 if (ret)
941 goto notfound;
943 ret = krb5_kt_next_entry(context, id, &entry, &cursor);
944 krb5_kt_end_seq_get(context, id, &cursor);
945 if (ret)
946 goto notfound;
948 krb5_kt_free_entry(context, &entry);
950 return 0;
952 notfound:
953 ret = krb5_kt_get_full_name(context, id, &name);
954 if (ret == 0) {
955 krb5_set_error_message(context, KRB5_KT_NOTFOUND,
956 N_("No entry in keytab: %s", ""), name);
957 free(name);
959 return KRB5_KT_NOTFOUND;
962 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
963 _krb5_kt_client_default_name(krb5_context context, char **name)
965 const char *tmp;
967 tmp = secure_getenv("KRB5_CLIENT_KTNAME");
968 if (tmp == NULL)
969 tmp = krb5_config_get_string(context, NULL,
970 "libdefaults",
971 "default_client_keytab_name", NULL);
972 if (tmp == NULL)
973 tmp = CLIENT_KEYTAB_DEFAULT;
975 return _krb5_expand_path_tokens(context, tmp, 1, name);