2 * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
34 #include "krb5_locl.h"
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
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:
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.
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.
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.
92 main (int argc, char **argv)
96 krb5_kt_cursor cursor;
97 krb5_keytab_entry entry;
101 if (krb5_init_context (&context) != 0)
102 errx(1, "krb5_context");
104 ret = krb5_kt_default (context, &keytab);
106 krb5_err(context, 1, ret, "krb5_kt_default");
108 ret = krb5_kt_start_seq_get(context, keytab, &cursor);
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);
115 krb5_kt_free_entry(context, &entry);
117 ret = krb5_kt_end_seq_get(context, keytab, &cursor);
119 krb5_err(context, 1, ret, "krb5_kt_end_seq_get");
120 ret = krb5_kt_close(context, keytab);
122 krb5_err(context, 1, ret, "krb5_kt_close");
123 krb5_free_context(context);
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
));
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
++;
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
||
175 /* Avoid treating <drive>:<path> as a keytab type
177 || name
+ 1 == residual
182 *type_len
= strlen(*type
);
186 *type_len
= residual
- name
;
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
,
214 const char *type
, *residual
;
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)
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
));
233 return krb5_enomem(context
);
234 memcpy(k
, &context
->kt_types
[i
], sizeof(*k
));
236 ret
= (*k
->resolve
)(context
, residual
, k
);
246 * Default ktname from context with possible environment
249 static const char *default_ktname(krb5_context context
)
251 const char *tmp
= NULL
;
253 tmp
= secure_getenv("KRB5_KTNAME");
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
;
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
)
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
);
312 kt
= context
->default_keytab_modify
;
313 if (strlcpy (name
, kt
, namesize
) >= namesize
) {
314 krb5_clear_error_message (context
);
315 return KRB5_CONFIG_NOTENUFSPACE
;
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.
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
,
358 krb5_enctype enctype
,
361 krb5_keytab keytab
= NULL
; /* Quiet lint */
362 krb5_keytab_entry entry
;
365 memset(&entry
, 0, sizeof(entry
));
367 ret
= krb5_kt_resolve (context
, keyprocarg
, &keytab
);
369 ret
= krb5_kt_default (context
, &keytab
);
374 ret
= krb5_kt_get_entry (context
, keytab
, principal
, vno
, enctype
, &entry
);
376 ret
= krb5_copy_keyblock (context
, &entry
.keyblock
, key
);
377 krb5_kt_free_entry(context
, &entry
);
379 krb5_kt_close (context
, keytab
);
384 * Return the type of the `keytab' in the string `prefix of length
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
,
403 strlcpy(prefix
, keytab
->prefix
, prefixsize
);
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
,
426 return (*keytab
->get_name
)(context
, keytab
, name
, namesize
);
430 * Retrieve the full name of the keytab `keytab' and store the name in
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
,
448 char type
[KRB5_KT_PREFIX_MAX_LEN
];
449 char name
[MAXPATHLEN
];
454 ret
= krb5_kt_get_type(context
, keytab
, type
, sizeof(type
));
458 ret
= krb5_kt_get_name(context
, keytab
, name
, sizeof(name
));
462 if (asprintf(str
, "%s:%s", type
, name
) == -1) {
464 return krb5_enomem(context
);
471 * Finish using the keytab in `id'. All resources will be released,
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
,
486 krb5_error_code ret
= 0;
489 ret
= (id
->close
)(context
, id
);
490 memset(id
, 0, sizeof(*id
));
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
,
514 ret
= (*id
->destroy
)(context
, id
);
515 krb5_kt_close(context
, id
);
520 * Match any aliases in keytab `entry' with `principal'.
524 compare_aliases(krb5_context context
,
525 krb5_keytab_entry
*entry
,
526 krb5_const_principal principal
)
529 if (entry
->aliases
== NULL
)
531 for (i
= 0; i
< entry
->aliases
->len
; i
++)
532 if (krb5_principal_compare(context
, &entry
->aliases
->val
[i
], principal
))
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
,
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
))) {
565 } else if (principal
!= NULL
&& strcmp(principal
->realm
, "") != 0 &&
566 !(krb5_principal_compare(context
, entry
->principal
, principal
) ||
567 compare_aliases(context
, entry
, principal
))) {
570 if (vno
&& vno
!= entry
->vno
)
572 if (enctype
&& enctype
!= entry
->keyblock
.keytype
)
577 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
578 _krb5_kt_principal_not_found(krb5_context context
,
581 krb5_const_principal principal
,
582 krb5_enctype enctype
,
586 char *enctype_str
= NULL
;
587 char *kt_name
= NULL
;
590 (void) krb5_unparse_name(context
, principal
, &princ
);
591 (void) krb5_kt_get_full_name(context
, id
, &kt_name
);
593 (void) krb5_enctype_to_string(context
, enctype
, &enctype_str
);
596 snprintf(kvno_str
, sizeof(kvno_str
), "(kvno %d)", kvno
);
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>",
605 kt_name
? kt_name
: "unknown keytab",
606 enctype_str
? enctype_str
: "unknown enctype");
613 static krb5_error_code
614 krb5_kt_get_entry_wrapped(krb5_context context
,
616 krb5_const_principal principal
,
618 krb5_enctype enctype
,
619 krb5_keytab_entry
*entry
)
621 krb5_keytab_entry tmp
;
623 krb5_kt_cursor cursor
;
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
);
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
;
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 */
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
);
648 } else if (kvno
== 0 && tmp
.vno
> 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
);
658 return _krb5_kt_principal_not_found(context
, KRB5_KT_NOTFOUND
,
659 id
, principal
, enctype
, kvno
);
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
,
682 krb5_const_principal principal
,
684 krb5_enctype enctype
,
685 krb5_keytab_entry
*entry
)
688 krb5_const_principal try_princ
;
689 krb5_name_canon_iterator name_canon_iter
;
692 /* Use `NULL' instead of `principal' to quiet static analizers */
693 return krb5_kt_get_entry_wrapped(context
, id
, NULL
, kvno
, enctype
,
696 ret
= krb5_name_canon_iterator_start(context
, principal
, &name_canon_iter
);
701 ret
= krb5_name_canon_iterate(context
, &name_canon_iter
, &try_princ
,
705 if (try_princ
== NULL
) {
706 ret
= KRB5_KT_NOTFOUND
;
709 ret
= krb5_kt_get_entry_wrapped(context
, id
, try_princ
, kvno
,
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",
717 krb5_free_name_canon_iterator(context
, name_canon_iter
);
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
)
740 memset(out
, 0, sizeof(*out
));
742 ret
= krb5_copy_principal (context
, in
->principal
, &out
->principal
);
745 ret
= krb5_copy_keyblock_contents (context
,
749 krb5_free_principal(context
, out
->principal
);
750 memset(out
, 0, sizeof(*out
));
754 out
->timestamp
= in
->timestamp
;
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
));
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
,
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", ""),
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
,
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 "
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
,
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
,
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", ""),
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
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
,
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", ""),
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
,
933 krb5_keytab_entry entry
;
934 krb5_kt_cursor cursor
;
938 memset(&entry
, 0, sizeof(entry
));
939 ret
= krb5_kt_start_seq_get(context
, id
, &cursor
);
943 ret
= krb5_kt_next_entry(context
, id
, &entry
, &cursor
);
944 krb5_kt_end_seq_get(context
, id
, &cursor
);
948 krb5_kt_free_entry(context
, &entry
);
953 ret
= krb5_kt_get_full_name(context
, id
, &name
);
955 krb5_set_error_message(context
, KRB5_KT_NOTFOUND
,
956 N_("No entry in keytab: %s", ""), 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
)
967 tmp
= secure_getenv("KRB5_CLIENT_KTNAME");
969 tmp
= krb5_config_get_string(context
, NULL
,
971 "default_client_keytab_name", NULL
);
973 tmp
= CLIENT_KEYTAB_DEFAULT
;
975 return _krb5_expand_path_tokens(context
, tmp
, 1, name
);