2 * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include "krb5_locl.h"
39 * @page krb5_ccache_intro The credential cache functions
40 * @section section_krb5_ccache Kerberos credential caches
42 * krb5_ccache structure holds a Kerberos credential cache.
44 * Heimdal support the follow types of credential caches:
47 * Store the credential in a database
49 * Store the credential in memory
51 * Store the credential in memory
53 * A credential cache server based solution for Mac OS X
55 * A credential cache server based solution for all platforms
59 * This is a minimalistic version of klist:
64 main (int argc, char **argv)
67 krb5_cc_cursor cursor;
72 if (krb5_init_context (&context) != 0)
73 errx(1, "krb5_context");
75 ret = krb5_cc_default (context, &id);
77 krb5_err(context, 1, ret, "krb5_cc_default");
79 ret = krb5_cc_start_seq_get(context, id, &cursor);
81 krb5_err(context, 1, ret, "krb5_cc_start_seq_get");
83 while((ret = krb5_cc_next_cred(context, id, &cursor, &creds)) == 0){
86 krb5_unparse_name(context, creds.server, &principal);
87 printf("principal: %s\\n", principal);
89 krb5_free_cred_contents (context, &creds);
91 ret = krb5_cc_end_seq_get(context, id, &cursor);
93 krb5_err(context, 1, ret, "krb5_cc_end_seq_get");
95 krb5_cc_close(context, id);
97 krb5_free_context(context);
104 * Add a new ccache type with operations `ops', overwriting any
105 * existing one if `override'.
107 * @param context a Keberos context
108 * @param ops type of plugin symbol
109 * @param override flag to select if the registration is to overide
110 * an existing ops with the same name.
112 * @return Return an error code or 0, see krb5_get_error_message().
114 * @ingroup krb5_ccache
117 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
118 krb5_cc_register(krb5_context context
,
119 const krb5_cc_ops
*ops
,
120 krb5_boolean override
)
124 for(i
= 0; i
< context
->num_cc_ops
&& context
->cc_ops
[i
]->prefix
; i
++) {
125 if(strcmp(context
->cc_ops
[i
]->prefix
, ops
->prefix
) == 0) {
127 krb5_set_error_message(context
,
129 N_("cache type %s already exists", "type"),
131 return KRB5_CC_TYPE_EXISTS
;
136 if(i
== context
->num_cc_ops
) {
137 const krb5_cc_ops
**o
= realloc(rk_UNCONST(context
->cc_ops
),
138 (context
->num_cc_ops
+ 1) *
139 sizeof(context
->cc_ops
[0]));
141 krb5_set_error_message(context
, KRB5_CC_NOMEM
,
142 N_("malloc: out of memory", ""));
143 return KRB5_CC_NOMEM
;
146 context
->cc_ops
[context
->num_cc_ops
] = NULL
;
147 context
->num_cc_ops
++;
149 context
->cc_ops
[i
] = ops
;
154 * Allocate the memory for a `id' and the that function table to
155 * `ops'. Returns 0 or and error code.
158 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
159 _krb5_cc_allocate(krb5_context context
,
160 const krb5_cc_ops
*ops
,
165 p
= malloc (sizeof(*p
));
167 krb5_set_error_message(context
, KRB5_CC_NOMEM
,
168 N_("malloc: out of memory", ""));
169 return KRB5_CC_NOMEM
;
178 * Allocate memory for a new ccache in `id' with operations `ops'
179 * and name `residual'. Return 0 or an error code.
182 static krb5_error_code
183 allocate_ccache (krb5_context context
,
184 const krb5_cc_ops
*ops
,
185 const char *residual
,
189 #ifdef KRB5_USE_PATH_TOKENS
190 char * exp_residual
= NULL
;
193 filepath
= (strcmp("FILE", ops
->prefix
) == 0
194 || strcmp("DIR", ops
->prefix
) == 0
195 || strcmp("SCC", ops
->prefix
) == 0);
197 ret
= _krb5_expand_path_tokens(context
, residual
, filepath
, &exp_residual
);
201 residual
= exp_residual
;
204 ret
= _krb5_cc_allocate(context
, ops
, id
);
206 #ifdef KRB5_USE_PATH_TOKENS
213 ret
= (*id
)->ops
->resolve(context
, id
, residual
);
219 #ifdef KRB5_USE_PATH_TOKENS
228 is_possible_path_name(const char * name
)
232 if ((colon
= strchr(name
, ':')) == NULL
)
236 /* <drive letter>:\path\to\cache ? */
238 if (colon
== name
+ 1 &&
239 strchr(colon
+ 1, ':') == NULL
)
247 * Find and allocate a ccache in `id' from the specification in `residual'.
248 * If the ccache name doesn't contain any colon, interpret it as a file name.
250 * @param context a Keberos context.
251 * @param name string name of a credential cache.
252 * @param id return pointer to a found credential cache.
254 * @return Return 0 or an error code. In case of an error, id is set
255 * to NULL, see krb5_get_error_message().
257 * @ingroup krb5_ccache
261 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
262 krb5_cc_resolve(krb5_context context
,
270 for(i
= 0; i
< context
->num_cc_ops
&& context
->cc_ops
[i
]->prefix
; i
++) {
271 size_t prefix_len
= strlen(context
->cc_ops
[i
]->prefix
);
273 if(strncmp(context
->cc_ops
[i
]->prefix
, name
, prefix_len
) == 0
274 && name
[prefix_len
] == ':') {
275 return allocate_ccache (context
, context
->cc_ops
[i
],
276 name
+ prefix_len
+ 1,
280 if (is_possible_path_name(name
))
281 return allocate_ccache (context
, &krb5_fcc_ops
, name
, id
);
283 krb5_set_error_message(context
, KRB5_CC_UNKNOWN_TYPE
,
284 N_("unknown ccache type %s", "name"), name
);
285 return KRB5_CC_UNKNOWN_TYPE
;
290 * Generates a new unique ccache of `type` in `id'. If `type' is NULL,
291 * the library chooses the default credential cache type. The supplied
292 * `hint' (that can be NULL) is a string that the credential cache
293 * type can use to base the name of the credential on, this is to make
294 * it easier for the user to differentiate the credentials.
296 * @return Return an error code or 0, see krb5_get_error_message().
298 * @ingroup krb5_ccache
301 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
302 krb5_cc_new_unique(krb5_context context
, const char *type
,
303 const char *hint
, krb5_ccache
*id
)
305 const krb5_cc_ops
*ops
;
308 ops
= krb5_cc_get_prefix_ops(context
, type
);
310 krb5_set_error_message(context
, KRB5_CC_UNKNOWN_TYPE
,
311 "Credential cache type %s is unknown", type
);
312 return KRB5_CC_UNKNOWN_TYPE
;
315 ret
= _krb5_cc_allocate(context
, ops
, id
);
318 ret
= (*id
)->ops
->gen_new(context
, id
);
327 * Return the name of the ccache `id'
329 * @ingroup krb5_ccache
333 KRB5_LIB_FUNCTION
const char* KRB5_LIB_CALL
334 krb5_cc_get_name(krb5_context context
,
337 return id
->ops
->get_name(context
, id
);
341 * Return the type of the ccache `id'.
343 * @ingroup krb5_ccache
347 KRB5_LIB_FUNCTION
const char* KRB5_LIB_CALL
348 krb5_cc_get_type(krb5_context context
,
351 return id
->ops
->prefix
;
355 * Return the complete resolvable name the cache
357 * @param context a Keberos context
358 * @param id return pointer to a found credential cache
359 * @param str the returned name of a credential cache, free with krb5_xfree()
361 * @return Returns 0 or an error (and then *str is set to NULL).
363 * @ingroup krb5_ccache
367 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
368 krb5_cc_get_full_name(krb5_context context
,
372 const char *type
, *name
;
376 type
= krb5_cc_get_type(context
, id
);
378 krb5_set_error_message(context
, KRB5_CC_UNKNOWN_TYPE
,
379 "cache have no name of type");
380 return KRB5_CC_UNKNOWN_TYPE
;
383 name
= krb5_cc_get_name(context
, id
);
385 krb5_set_error_message(context
, KRB5_CC_BADNAME
,
386 "cache of type %s have no name", type
);
387 return KRB5_CC_BADNAME
;
390 if (asprintf(str
, "%s:%s", type
, name
) == -1) {
392 return krb5_enomem(context
);
398 * Return krb5_cc_ops of a the ccache `id'.
400 * @ingroup krb5_ccache
404 KRB5_LIB_FUNCTION
const krb5_cc_ops
* KRB5_LIB_CALL
405 krb5_cc_get_ops(krb5_context context
, krb5_ccache id
)
411 * Expand variables in `str' into `res'
414 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
415 _krb5_expand_default_cc_name(krb5_context context
, const char *str
, char **res
)
419 filepath
= (strncmp("FILE:", str
, 5) == 0
420 || strncmp("DIR:", str
, 4) == 0
421 || strncmp("SCC:", str
, 4) == 0);
423 return _krb5_expand_path_tokens(context
, str
, filepath
, res
);
427 * Return non-zero if envirnoment that will determine default krb5cc
432 environment_changed(krb5_context context
)
436 /* if the cc name was set, don't change it */
437 if (context
->default_cc_name_set
)
440 /* XXX performance: always ask KCM/API if default name has changed */
441 if (context
->default_cc_name
&&
442 (strncmp(context
->default_cc_name
, "KCM:", 4) == 0 ||
443 strncmp(context
->default_cc_name
, "API:", 4) == 0))
449 e
= getenv("KRB5CCNAME");
451 if (context
->default_cc_name_env
) {
452 free(context
->default_cc_name_env
);
453 context
->default_cc_name_env
= NULL
;
457 if (context
->default_cc_name_env
== NULL
)
459 if (strcmp(e
, context
->default_cc_name_env
) != 0)
466 * Switch the default default credential cache for a specific
467 * credcache type (and name for some implementations).
469 * @return Return an error code or 0, see krb5_get_error_message().
471 * @ingroup krb5_ccache
474 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
475 krb5_cc_switch(krb5_context context
, krb5_ccache id
)
478 _krb5_set_default_cc_name_to_registry(context
, id
);
481 if (id
->ops
->set_default
== NULL
)
484 return (*id
->ops
->set_default
)(context
, id
);
488 * Return true if the default credential cache support switch
490 * @ingroup krb5_ccache
493 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
494 krb5_cc_support_switch(krb5_context context
, const char *type
)
496 const krb5_cc_ops
*ops
;
498 ops
= krb5_cc_get_prefix_ops(context
, type
);
499 if (ops
&& ops
->set_default
)
505 * Set the default cc name for `context' to `name'.
507 * @ingroup krb5_ccache
510 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
511 krb5_cc_set_default_name(krb5_context context
, const char *name
)
513 krb5_error_code ret
= 0;
514 char *p
= NULL
, *exp_p
= NULL
;
518 const char *e
= NULL
;
521 e
= getenv("KRB5CCNAME");
524 if (context
->default_cc_name_env
)
525 free(context
->default_cc_name_env
);
526 context
->default_cc_name_env
= strdup(e
);
532 e
= p
= _krb5_get_default_cc_name_from_registry(context
);
536 e
= krb5_config_get_string(context
, NULL
, "libdefaults",
537 "default_cc_name", NULL
);
539 ret
= _krb5_expand_default_cc_name(context
, e
, &p
);
544 const krb5_cc_ops
*ops
= KRB5_DEFAULT_CCTYPE
;
545 e
= krb5_config_get_string(context
, NULL
, "libdefaults",
546 "default_cc_type", NULL
);
548 ops
= krb5_cc_get_prefix_ops(context
, e
);
550 krb5_set_error_message(context
,
551 KRB5_CC_UNKNOWN_TYPE
,
552 "Credential cache type %s "
554 return KRB5_CC_UNKNOWN_TYPE
;
557 ret
= (*ops
->get_default_name
)(context
, &p
);
562 context
->default_cc_name_set
= 0;
565 context
->default_cc_name_set
= 1;
569 return krb5_enomem(context
);
571 filepath
= (strncmp("FILE:", p
, 5) == 0
572 || strncmp("DIR:", p
, 4) == 0
573 || strncmp("SCC:", p
, 4) == 0);
575 ret
= _krb5_expand_path_tokens(context
, p
, filepath
, &exp_p
);
581 if (context
->default_cc_name
)
582 free(context
->default_cc_name
);
584 context
->default_cc_name
= p
;
590 * Return a pointer to a context static string containing the default
593 * @return String to the default credential cache name.
595 * @ingroup krb5_ccache
599 KRB5_LIB_FUNCTION
const char* KRB5_LIB_CALL
600 krb5_cc_default_name(krb5_context context
)
602 if (context
->default_cc_name
== NULL
|| environment_changed(context
))
603 krb5_cc_set_default_name(context
, NULL
);
605 return context
->default_cc_name
;
609 * Open the default ccache in `id'.
611 * @return Return an error code or 0, see krb5_get_error_message().
613 * @ingroup krb5_ccache
617 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
618 krb5_cc_default(krb5_context context
,
621 const char *p
= krb5_cc_default_name(context
);
624 return krb5_enomem(context
);
625 return krb5_cc_resolve(context
, p
, id
);
629 * Create a new ccache in `id' for `primary_principal'.
631 * @return Return an error code or 0, see krb5_get_error_message().
633 * @ingroup krb5_ccache
637 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
638 krb5_cc_initialize(krb5_context context
,
640 krb5_principal primary_principal
)
642 return (*id
->ops
->init
)(context
, id
, primary_principal
);
647 * Remove the ccache `id'.
649 * @return Return an error code or 0, see krb5_get_error_message().
651 * @ingroup krb5_ccache
655 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
656 krb5_cc_destroy(krb5_context context
,
661 ret
= (*id
->ops
->destroy
)(context
, id
);
662 krb5_cc_close (context
, id
);
667 * Stop using the ccache `id' and free the related resources.
669 * @return Return an error code or 0, see krb5_get_error_message().
671 * @ingroup krb5_ccache
675 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
676 krb5_cc_close(krb5_context context
,
680 ret
= (*id
->ops
->close
)(context
, id
);
686 * Store `creds' in the ccache `id'.
688 * @return Return an error code or 0, see krb5_get_error_message().
690 * @ingroup krb5_ccache
694 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
695 krb5_cc_store_cred(krb5_context context
,
699 return (*id
->ops
->store
)(context
, id
, creds
);
703 * Retrieve the credential identified by `mcreds' (and `whichfields')
704 * from `id' in `creds'. 'creds' must be free by the caller using
705 * krb5_free_cred_contents.
707 * @param context A Kerberos 5 context
708 * @param id a Kerberos 5 credential cache
709 * @param whichfields what fields to use for matching credentials, same
710 * flags as whichfields in krb5_compare_creds()
711 * @param mcreds template credential to use for comparing
712 * @param creds returned credential, free with krb5_free_cred_contents()
714 * @return Return an error code or 0, see krb5_get_error_message().
716 * @ingroup krb5_ccache
720 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
721 krb5_cc_retrieve_cred(krb5_context context
,
723 krb5_flags whichfields
,
724 const krb5_creds
*mcreds
,
728 krb5_cc_cursor cursor
;
730 if (id
->ops
->retrieve
!= NULL
) {
731 return (*id
->ops
->retrieve
)(context
, id
, whichfields
,
735 ret
= krb5_cc_start_seq_get(context
, id
, &cursor
);
738 while((ret
= krb5_cc_next_cred(context
, id
, &cursor
, creds
)) == 0){
739 if(krb5_compare_creds(context
, whichfields
, mcreds
, creds
)){
743 krb5_free_cred_contents (context
, creds
);
745 krb5_cc_end_seq_get(context
, id
, &cursor
);
750 * Return the principal of `id' in `principal'.
752 * @return Return an error code or 0, see krb5_get_error_message().
754 * @ingroup krb5_ccache
758 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
759 krb5_cc_get_principal(krb5_context context
,
761 krb5_principal
*principal
)
763 return (*id
->ops
->get_princ
)(context
, id
, principal
);
767 * Start iterating over `id', `cursor' is initialized to the
768 * beginning. Caller must free the cursor with krb5_cc_end_seq_get().
770 * @return Return an error code or 0, see krb5_get_error_message().
772 * @ingroup krb5_ccache
776 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
777 krb5_cc_start_seq_get (krb5_context context
,
778 const krb5_ccache id
,
779 krb5_cc_cursor
*cursor
)
781 return (*id
->ops
->get_first
)(context
, id
, cursor
);
785 * Retrieve the next cred pointed to by (`id', `cursor') in `creds'
786 * and advance `cursor'.
788 * @return Return an error code or 0, see krb5_get_error_message().
790 * @ingroup krb5_ccache
794 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
795 krb5_cc_next_cred (krb5_context context
,
796 const krb5_ccache id
,
797 krb5_cc_cursor
*cursor
,
800 return (*id
->ops
->get_next
)(context
, id
, cursor
, creds
);
804 * Destroy the cursor `cursor'.
806 * @ingroup krb5_ccache
810 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
811 krb5_cc_end_seq_get (krb5_context context
,
812 const krb5_ccache id
,
813 krb5_cc_cursor
*cursor
)
815 return (*id
->ops
->end_get
)(context
, id
, cursor
);
819 * Remove the credential identified by `cred', `which' from `id'.
821 * @ingroup krb5_ccache
825 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
826 krb5_cc_remove_cred(krb5_context context
,
831 if(id
->ops
->remove_cred
== NULL
) {
832 krb5_set_error_message(context
,
834 "ccache %s does not support remove_cred",
836 return EACCES
; /* XXX */
838 return (*id
->ops
->remove_cred
)(context
, id
, which
, cred
);
842 * Set the flags of `id' to `flags'.
844 * @ingroup krb5_ccache
848 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
849 krb5_cc_set_flags(krb5_context context
,
853 return (*id
->ops
->set_flags
)(context
, id
, flags
);
857 * Get the flags of `id', store them in `flags'.
859 * @ingroup krb5_ccache
862 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
863 krb5_cc_get_flags(krb5_context context
,
872 * Copy the contents of `from' to `to' if the given match function
875 * @param context A Kerberos 5 context.
876 * @param from the cache to copy data from.
877 * @param to the cache to copy data to.
878 * @param match a match function that should return TRUE if cred argument should be copied, if NULL, all credentials are copied.
879 * @param matchctx context passed to match function.
880 * @param matched set to true if there was a credential that matched, may be NULL.
882 * @return Return an error code or 0, see krb5_get_error_message().
884 * @ingroup krb5_ccache
887 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
888 krb5_cc_copy_match_f(krb5_context context
,
889 const krb5_ccache from
,
891 krb5_boolean (*match
)(krb5_context
, void *, const krb5_creds
*),
893 unsigned int *matched
)
896 krb5_cc_cursor cursor
;
898 krb5_principal princ
;
903 ret
= krb5_cc_get_principal(context
, from
, &princ
);
906 ret
= krb5_cc_initialize(context
, to
, princ
);
908 krb5_free_principal(context
, princ
);
911 ret
= krb5_cc_start_seq_get(context
, from
, &cursor
);
913 krb5_free_principal(context
, princ
);
917 while ((ret
= krb5_cc_next_cred(context
, from
, &cursor
, &cred
)) == 0) {
918 if (match
== NULL
|| (*match
)(context
, matchctx
, &cred
)) {
921 ret
= krb5_cc_store_cred(context
, to
, &cred
);
925 krb5_free_cred_contents(context
, &cred
);
927 krb5_cc_end_seq_get(context
, from
, &cursor
);
928 krb5_free_principal(context
, princ
);
929 if (ret
== KRB5_CC_END
)
935 * Just like krb5_cc_copy_match_f(), but copy everything.
937 * @ingroup @krb5_ccache
940 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
941 krb5_cc_copy_cache(krb5_context context
,
942 const krb5_ccache from
,
945 return krb5_cc_copy_match_f(context
, from
, to
, NULL
, NULL
, NULL
);
949 * Return the version of `id'.
951 * @ingroup krb5_ccache
955 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
956 krb5_cc_get_version(krb5_context context
,
957 const krb5_ccache id
)
959 if(id
->ops
->get_version
)
960 return (*id
->ops
->get_version
)(context
, id
);
966 * Clear `mcreds' so it can be used with krb5_cc_retrieve_cred
968 * @ingroup krb5_ccache
972 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
973 krb5_cc_clear_mcred(krb5_creds
*mcred
)
975 memset(mcred
, 0, sizeof(*mcred
));
979 * Get the cc ops that is registered in `context' to handle the
980 * prefix. prefix can be a complete credential cache name or a
981 * prefix, the function will only use part up to the first colon (:)
982 * if there is one. If prefix the argument is NULL, the default ccache
983 * implemtation is returned.
985 * @return Returns NULL if ops not found.
987 * @ingroup krb5_ccache
991 KRB5_LIB_FUNCTION
const krb5_cc_ops
* KRB5_LIB_CALL
992 krb5_cc_get_prefix_ops(krb5_context context
, const char *prefix
)
998 return KRB5_DEFAULT_CCTYPE
;
1000 /* Is absolute path? Or UNC path? */
1001 if (ISPATHSEP(prefix
[0]))
1002 return &krb5_fcc_ops
;
1005 /* Is drive letter? */
1006 if (isalpha(prefix
[0]) && prefix
[1] == ':')
1007 return &krb5_fcc_ops
;
1012 krb5_enomem(context
);
1015 p1
= strchr(p
, ':');
1019 for(i
= 0; i
< context
->num_cc_ops
&& context
->cc_ops
[i
]->prefix
; i
++) {
1020 if(strcmp(context
->cc_ops
[i
]->prefix
, p
) == 0) {
1022 return context
->cc_ops
[i
];
1029 struct krb5_cc_cache_cursor_data
{
1030 const krb5_cc_ops
*ops
;
1031 krb5_cc_cursor cursor
;
1035 * Start iterating over all caches of specified type. See also
1036 * krb5_cccol_cursor_new().
1038 * @param context A Kerberos 5 context
1039 * @param type optional type to iterate over, if NULL, the default cache is used.
1040 * @param cursor cursor should be freed with krb5_cc_cache_end_seq_get().
1042 * @return Return an error code or 0, see krb5_get_error_message().
1044 * @ingroup krb5_ccache
1048 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1049 krb5_cc_cache_get_first (krb5_context context
,
1051 krb5_cc_cache_cursor
*cursor
)
1053 const krb5_cc_ops
*ops
;
1054 krb5_error_code ret
;
1057 type
= krb5_cc_default_name(context
);
1059 ops
= krb5_cc_get_prefix_ops(context
, type
);
1061 krb5_set_error_message(context
, KRB5_CC_UNKNOWN_TYPE
,
1062 "Unknown type \"%s\" when iterating "
1063 "trying to iterate the credential caches", type
);
1064 return KRB5_CC_UNKNOWN_TYPE
;
1067 if (ops
->get_cache_first
== NULL
) {
1068 krb5_set_error_message(context
, KRB5_CC_NOSUPP
,
1069 N_("Credential cache type %s doesn't support "
1070 "iterations over caches", "type"),
1072 return KRB5_CC_NOSUPP
;
1075 *cursor
= calloc(1, sizeof(**cursor
));
1076 if (*cursor
== NULL
)
1077 return krb5_enomem(context
);
1079 (*cursor
)->ops
= ops
;
1081 ret
= ops
->get_cache_first(context
, &(*cursor
)->cursor
);
1090 * Retrieve the next cache pointed to by (`cursor') in `id'
1091 * and advance `cursor'.
1093 * @param context A Kerberos 5 context
1094 * @param cursor the iterator cursor, returned by krb5_cc_cache_get_first()
1095 * @param id next ccache
1097 * @return Return 0 or an error code. Returns KRB5_CC_END when the end
1098 * of caches is reached, see krb5_get_error_message().
1100 * @ingroup krb5_ccache
1104 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1105 krb5_cc_cache_next (krb5_context context
,
1106 krb5_cc_cache_cursor cursor
,
1109 return cursor
->ops
->get_cache_next(context
, cursor
->cursor
, id
);
1113 * Destroy the cursor `cursor'.
1115 * @return Return an error code or 0, see krb5_get_error_message().
1117 * @ingroup krb5_ccache
1121 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1122 krb5_cc_cache_end_seq_get (krb5_context context
,
1123 krb5_cc_cache_cursor cursor
)
1125 krb5_error_code ret
;
1126 ret
= cursor
->ops
->end_cache_get(context
, cursor
->cursor
);
1133 * Search for a matching credential cache that have the
1134 * `principal' as the default principal. On success, `id' needs to be
1135 * freed with krb5_cc_close() or krb5_cc_destroy().
1137 * @param context A Kerberos 5 context
1138 * @param client The principal to search for
1139 * @param id the returned credential cache
1141 * @return On failure, error code is returned and `id' is set to NULL.
1143 * @ingroup krb5_ccache
1147 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1148 krb5_cc_cache_match (krb5_context context
,
1149 krb5_principal client
,
1152 krb5_cccol_cursor cursor
;
1153 krb5_error_code ret
;
1154 krb5_ccache cache
= NULL
;
1155 krb5_ccache expired_match
= NULL
;
1159 ret
= krb5_cccol_cursor_new (context
, &cursor
);
1163 while (krb5_cccol_cursor_next(context
, cursor
, &cache
) == 0 && cache
!= NULL
) {
1164 krb5_principal principal
;
1168 ret
= krb5_cc_get_principal(context
, cache
, &principal
);
1172 if (client
->name
.name_string
.len
== 0)
1173 match
= (strcmp(client
->realm
, principal
->realm
) == 0);
1175 match
= krb5_principal_compare(context
, principal
, client
);
1176 krb5_free_principal(context
, principal
);
1181 if (expired_match
== NULL
&&
1182 (krb5_cc_get_lifetime(context
, cache
, &lifetime
) != 0 || lifetime
== 0)) {
1183 expired_match
= cache
;
1191 krb5_cc_close(context
, cache
);
1195 krb5_cccol_cursor_free(context
, &cursor
);
1197 if (cache
== NULL
&& expired_match
) {
1198 cache
= expired_match
;
1199 expired_match
= NULL
;
1200 } else if (expired_match
) {
1201 krb5_cc_close(context
, expired_match
);
1202 } else if (cache
== NULL
) {
1205 krb5_unparse_name(context
, client
, &str
);
1207 krb5_set_error_message(context
, KRB5_CC_NOTFOUND
,
1208 N_("Principal %s not found in any "
1209 "credential cache", ""),
1210 str
? str
: "<out of memory>");
1213 return KRB5_CC_NOTFOUND
;
1222 * Move the content from one credential cache to another. The
1223 * operation is an atomic switch.
1225 * @param context a Keberos context
1226 * @param from the credential cache to move the content from
1227 * @param to the credential cache to move the content to
1229 * @return On sucess, from is freed. On failure, error code is
1230 * returned and from and to are both still allocated, see krb5_get_error_message().
1232 * @ingroup krb5_ccache
1235 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1236 krb5_cc_move(krb5_context context
, krb5_ccache from
, krb5_ccache to
)
1238 krb5_error_code ret
;
1240 if (strcmp(from
->ops
->prefix
, to
->ops
->prefix
) != 0) {
1241 krb5_set_error_message(context
, KRB5_CC_NOSUPP
,
1242 N_("Moving credentials between diffrent "
1243 "types not yet supported", ""));
1244 return KRB5_CC_NOSUPP
;
1247 ret
= (*to
->ops
->move
)(context
, from
, to
);
1249 memset(from
, 0, sizeof(*from
));
1255 #define KRB5_CONF_NAME "krb5_ccache_conf_data"
1256 #define KRB5_REALM_NAME "X-CACHECONF:"
1258 static krb5_error_code
1259 build_conf_principals(krb5_context context
, krb5_ccache id
,
1260 krb5_const_principal principal
,
1261 const char *name
, krb5_creds
*cred
)
1263 krb5_principal client
;
1264 krb5_error_code ret
;
1267 memset(cred
, 0, sizeof(*cred
));
1269 ret
= krb5_cc_get_principal(context
, id
, &client
);
1274 ret
= krb5_unparse_name(context
, principal
, &pname
);
1279 ret
= krb5_make_principal(context
, &cred
->server
,
1281 KRB5_CONF_NAME
, name
, pname
, NULL
);
1284 krb5_free_principal(context
, client
);
1287 ret
= krb5_copy_principal(context
, client
, &cred
->client
);
1288 krb5_free_principal(context
, client
);
1293 * Return TRUE (non zero) if the principal is a configuration
1294 * principal (generated part of krb5_cc_set_config()). Returns FALSE
1295 * (zero) if not a configuration principal.
1297 * @param context a Keberos context
1298 * @param principal principal to check if it a configuration principal
1300 * @ingroup krb5_ccache
1303 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
1304 krb5_is_config_principal(krb5_context context
,
1305 krb5_const_principal principal
)
1307 if (strcmp(principal
->realm
, KRB5_REALM_NAME
) != 0)
1310 if (principal
->name
.name_string
.len
== 0 ||
1311 strcmp(principal
->name
.name_string
.val
[0], KRB5_CONF_NAME
) != 0)
1318 * Store some configuration for the credential cache in the cache.
1319 * Existing configuration under the same name is over-written.
1321 * @param context a Keberos context
1322 * @param id the credential cache to store the data for
1323 * @param principal configuration for a specific principal, if
1324 * NULL, global for the whole cache.
1325 * @param name name under which the configuraion is stored.
1326 * @param data data to store, if NULL, configure is removed.
1328 * @ingroup krb5_ccache
1331 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1332 krb5_cc_set_config(krb5_context context
, krb5_ccache id
,
1333 krb5_const_principal principal
,
1334 const char *name
, krb5_data
*data
)
1336 krb5_error_code ret
;
1339 ret
= build_conf_principals(context
, id
, principal
, name
, &cred
);
1343 /* Remove old configuration */
1344 ret
= krb5_cc_remove_cred(context
, id
, 0, &cred
);
1345 if (ret
&& ret
!= KRB5_CC_NOTFOUND
)
1349 /* not that anyone care when this expire */
1350 cred
.times
.authtime
= time(NULL
);
1351 cred
.times
.endtime
= cred
.times
.authtime
+ 3600 * 24 * 30;
1353 ret
= krb5_data_copy(&cred
.ticket
, data
->data
, data
->length
);
1357 ret
= krb5_cc_store_cred(context
, id
, &cred
);
1361 krb5_free_cred_contents (context
, &cred
);
1366 * Get some configuration for the credential cache in the cache.
1368 * @param context a Keberos context
1369 * @param id the credential cache to store the data for
1370 * @param principal configuration for a specific principal, if
1371 * NULL, global for the whole cache.
1372 * @param name name under which the configuraion is stored.
1373 * @param data data to fetched, free with krb5_data_free()
1375 * @ingroup krb5_ccache
1379 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1380 krb5_cc_get_config(krb5_context context
, krb5_ccache id
,
1381 krb5_const_principal principal
,
1382 const char *name
, krb5_data
*data
)
1384 krb5_creds mcred
, cred
;
1385 krb5_error_code ret
;
1387 memset(&cred
, 0, sizeof(cred
));
1388 krb5_data_zero(data
);
1390 ret
= build_conf_principals(context
, id
, principal
, name
, &mcred
);
1394 ret
= krb5_cc_retrieve_cred(context
, id
, 0, &mcred
, &cred
);
1398 ret
= krb5_data_copy(data
, cred
.ticket
.data
, cred
.ticket
.length
);
1401 krb5_free_cred_contents (context
, &cred
);
1402 krb5_free_cred_contents (context
, &mcred
);
1410 struct krb5_cccol_cursor_data
{
1412 krb5_cc_cache_cursor cursor
;
1416 * Get a new cache interation cursor that will interate over all
1417 * credentials caches independent of type.
1419 * @param context a Keberos context
1420 * @param cursor passed into krb5_cccol_cursor_next() and free with krb5_cccol_cursor_free().
1422 * @return Returns 0 or and error code, see krb5_get_error_message().
1424 * @ingroup krb5_ccache
1427 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1428 krb5_cccol_cursor_new(krb5_context context
, krb5_cccol_cursor
*cursor
)
1430 *cursor
= calloc(1, sizeof(**cursor
));
1431 if (*cursor
== NULL
)
1432 return krb5_enomem(context
);
1434 (*cursor
)->cursor
= NULL
;
1440 * Get next credential cache from the iteration.
1442 * @param context A Kerberos 5 context
1443 * @param cursor the iteration cursor
1444 * @param cache the returned cursor, pointer is set to NULL on failure
1445 * and a cache on success. The returned cache needs to be freed
1446 * with krb5_cc_close() or destroyed with krb5_cc_destroy().
1447 * MIT Kerberos behavies slightly diffrent and sets cache to NULL
1448 * when all caches are iterated over and return 0.
1450 * @return Return 0 or and error, KRB5_CC_END is returned at the end
1451 * of iteration. See krb5_get_error_message().
1453 * @ingroup krb5_ccache
1457 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1458 krb5_cccol_cursor_next(krb5_context context
, krb5_cccol_cursor cursor
,
1461 krb5_error_code ret
;
1465 while (cursor
->idx
< context
->num_cc_ops
) {
1467 if (cursor
->cursor
== NULL
) {
1468 ret
= krb5_cc_cache_get_first (context
,
1469 context
->cc_ops
[cursor
->idx
]->prefix
,
1476 ret
= krb5_cc_cache_next(context
, cursor
->cursor
, cache
);
1480 krb5_cc_cache_end_seq_get(context
, cursor
->cursor
);
1481 cursor
->cursor
= NULL
;
1482 if (ret
!= KRB5_CC_END
)
1487 if (cursor
->idx
>= context
->num_cc_ops
) {
1488 krb5_set_error_message(context
, KRB5_CC_END
,
1489 N_("Reached end of credential caches", ""));
1497 * End an iteration and free all resources, can be done before end is reached.
1499 * @param context A Kerberos 5 context
1500 * @param cursor the iteration cursor to be freed.
1502 * @return Return 0 or and error, KRB5_CC_END is returned at the end
1503 * of iteration. See krb5_get_error_message().
1505 * @ingroup krb5_ccache
1508 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1509 krb5_cccol_cursor_free(krb5_context context
, krb5_cccol_cursor
*cursor
)
1511 krb5_cccol_cursor c
= *cursor
;
1516 krb5_cc_cache_end_seq_get(context
, c
->cursor
);
1523 * Return the last time the credential cache was modified.
1525 * @param context A Kerberos 5 context
1526 * @param id The credential cache to probe
1527 * @param mtime the last modification time, set to 0 on error.
1529 * @return Return 0 or and error. See krb5_get_error_message().
1531 * @ingroup krb5_ccache
1535 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1536 krb5_cc_last_change_time(krb5_context context
,
1538 krb5_timestamp
*mtime
)
1541 return (*id
->ops
->lastchange
)(context
, id
, mtime
);
1545 * Return the last modfication time for a cache collection. The query
1546 * can be limited to a specific cache type. If the function return 0
1547 * and mtime is 0, there was no credentials in the caches.
1549 * @param context A Kerberos 5 context
1550 * @param type The credential cache to probe, if NULL, all type are traversed.
1551 * @param mtime the last modification time, set to 0 on error.
1553 * @return Return 0 or and error. See krb5_get_error_message().
1555 * @ingroup krb5_ccache
1558 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1559 krb5_cccol_last_change_time(krb5_context context
,
1561 krb5_timestamp
*mtime
)
1563 krb5_cccol_cursor cursor
;
1564 krb5_error_code ret
;
1566 krb5_timestamp t
= 0;
1570 ret
= krb5_cccol_cursor_new (context
, &cursor
);
1574 while (krb5_cccol_cursor_next(context
, cursor
, &id
) == 0 && id
!= NULL
) {
1576 if (type
&& strcmp(krb5_cc_get_type(context
, id
), type
) != 0)
1579 ret
= krb5_cc_last_change_time(context
, id
, &t
);
1580 krb5_cc_close(context
, id
);
1587 krb5_cccol_cursor_free(context
, &cursor
);
1592 * Return a friendly name on credential cache. Free the result with krb5_xfree().
1594 * @return Return an error code or 0, see krb5_get_error_message().
1596 * @ingroup krb5_ccache
1599 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1600 krb5_cc_get_friendly_name(krb5_context context
,
1604 krb5_error_code ret
;
1607 ret
= krb5_cc_get_config(context
, id
, NULL
, "FriendlyName", &data
);
1609 krb5_principal principal
;
1610 ret
= krb5_cc_get_principal(context
, id
, &principal
);
1613 ret
= krb5_unparse_name(context
, principal
, name
);
1614 krb5_free_principal(context
, principal
);
1616 ret
= asprintf(name
, "%.*s", (int)data
.length
, (char *)data
.data
);
1617 krb5_data_free(&data
);
1619 ret
= krb5_enomem(context
);
1628 * Set the friendly name on credential cache.
1630 * @return Return an error code or 0, see krb5_get_error_message().
1632 * @ingroup krb5_ccache
1635 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1636 krb5_cc_set_friendly_name(krb5_context context
,
1642 data
.data
= rk_UNCONST(name
);
1643 data
.length
= strlen(name
);
1645 return krb5_cc_set_config(context
, id
, NULL
, "FriendlyName", &data
);
1649 * Get the lifetime of the initial ticket in the cache
1651 * Get the lifetime of the initial ticket in the cache, if the initial
1652 * ticket was not found, the error code KRB5_CC_END is returned.
1654 * @param context A Kerberos 5 context.
1655 * @param id a credential cache
1656 * @param t the relative lifetime of the initial ticket
1658 * @return Return an error code or 0, see krb5_get_error_message().
1660 * @ingroup krb5_ccache
1663 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1664 krb5_cc_get_lifetime(krb5_context context
, krb5_ccache id
, time_t *t
)
1666 krb5_cc_cursor cursor
;
1667 krb5_error_code ret
;
1669 time_t now
, endtime
= 0;
1674 ret
= krb5_cc_start_seq_get(context
, id
, &cursor
);
1678 while ((ret
= krb5_cc_next_cred(context
, id
, &cursor
, &cred
)) == 0) {
1680 * If we find a krbtgt in the cache, use that as the lifespan.
1682 if (krb5_principal_is_root_krbtgt(context
, cred
.client
)) {
1683 if (now
< cred
.times
.endtime
)
1684 endtime
= cred
.times
.endtime
;
1685 krb5_free_cred_contents(context
, &cred
);
1689 * Skip config entries
1691 if (krb5_is_config_principal(context
, cred
.server
)) {
1692 krb5_free_cred_contents(context
, &cred
);
1696 * If there was no krbtgt, use the shortest lifetime of
1697 * service tickets that have yet to expire. If all
1698 * credentials are expired, krb5_cc_get_lifetime() will fail.
1700 if ((endtime
== 0 || cred
.times
.endtime
< endtime
) && now
< cred
.times
.endtime
)
1701 endtime
= cred
.times
.endtime
;
1702 krb5_free_cred_contents(context
, &cred
);
1705 /* if we found an endtime use that */
1711 krb5_cc_end_seq_get(context
, id
, &cursor
);
1717 * Set the time offset betwen the client and the KDC
1719 * If the backend doesn't support KDC offset, use the context global setting.
1721 * @param context A Kerberos 5 context.
1722 * @param id a credential cache
1723 * @param offset the offset in seconds
1725 * @return Return an error code or 0, see krb5_get_error_message().
1727 * @ingroup krb5_ccache
1730 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1731 krb5_cc_set_kdc_offset(krb5_context context
, krb5_ccache id
, krb5_deltat offset
)
1733 if (id
->ops
->set_kdc_offset
== NULL
) {
1734 context
->kdc_sec_offset
= offset
;
1735 context
->kdc_usec_offset
= 0;
1738 return (*id
->ops
->set_kdc_offset
)(context
, id
, offset
);
1742 * Get the time offset betwen the client and the KDC
1744 * If the backend doesn't support KDC offset, use the context global setting.
1746 * @param context A Kerberos 5 context.
1747 * @param id a credential cache
1748 * @param offset the offset in seconds
1750 * @return Return an error code or 0, see krb5_get_error_message().
1752 * @ingroup krb5_ccache
1755 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1756 krb5_cc_get_kdc_offset(krb5_context context
, krb5_ccache id
, krb5_deltat
*offset
)
1758 if (id
->ops
->get_kdc_offset
== NULL
) {
1759 *offset
= context
->kdc_sec_offset
;
1762 return (*id
->ops
->get_kdc_offset
)(context
, id
, offset
);
1768 #define REGPATH_MIT_KRB5 "SOFTWARE\\MIT\\Kerberos5"
1769 KRB5_LIB_FUNCTION
char * KRB5_LIB_CALL
1770 _krb5_get_default_cc_name_from_registry(krb5_context context
)
1774 char * ccname
= NULL
;
1776 code
= RegOpenKeyEx(HKEY_CURRENT_USER
,
1778 0, KEY_READ
, &hk_k5
);
1780 if (code
!= ERROR_SUCCESS
)
1783 ccname
= _krb5_parse_reg_value_as_string(context
, hk_k5
, "ccname",
1791 KRB5_LIB_FUNCTION
int KRB5_LIB_CALL
1792 _krb5_set_default_cc_name_to_registry(krb5_context context
, krb5_ccache id
)
1797 char * ccname
= NULL
;
1799 code
= RegOpenKeyEx(HKEY_CURRENT_USER
,
1801 0, KEY_READ
|KEY_WRITE
, &hk_k5
);
1803 if (code
!= ERROR_SUCCESS
)
1806 ret
= asprintf(&ccname
, "%s:%s", krb5_cc_get_type(context
, id
), krb5_cc_get_name(context
, id
));
1810 ret
= _krb5_store_string_to_reg_value(context
, hk_k5
, "ccname",
1811 REG_SZ
, ccname
, -1, 0);