2 * Copyright (c) 1997 - 2008 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_ccache_intro The credential cache functions
38 * @section section_krb5_ccache Kerberos credential caches
40 * krb5_ccache structure holds a Kerberos credential cache.
42 * Heimdal support the follow types of credential caches:
45 * Store the credential in a database
47 * Store the credential in memory
49 * Store the credential in memory
51 * A credential cache server based solution for Mac OS X
53 * A credential cache server based solution for all platforms
57 * This is a minimalistic version of klist:
62 main (int argc, char **argv)
65 krb5_cc_cursor cursor;
70 if (krb5_init_context (&context) != 0)
71 errx(1, "krb5_context");
73 ret = krb5_cc_default (context, &id);
75 krb5_err(context, 1, ret, "krb5_cc_default");
77 ret = krb5_cc_start_seq_get(context, id, &cursor);
79 krb5_err(context, 1, ret, "krb5_cc_start_seq_get");
81 while((ret = krb5_cc_next_cred(context, id, &cursor, &creds)) == 0){
84 krb5_unparse_name_short(context, creds.server, &principal);
85 printf("principal: %s\\n", principal);
87 krb5_free_cred_contents (context, &creds);
89 ret = krb5_cc_end_seq_get(context, id, &cursor);
91 krb5_err(context, 1, ret, "krb5_cc_end_seq_get");
93 krb5_cc_close(context, id);
95 krb5_free_context(context);
102 * Add a new ccache type with operations `ops', overwriting any
103 * existing one if `override'.
105 * @param context a Keberos context
106 * @param ops type of plugin symbol
107 * @param override flag to select if the registration is to overide
108 * an existing ops with the same name.
110 * @return Return an error code or 0, see krb5_get_error_message().
112 * @ingroup krb5_ccache
115 krb5_error_code KRB5_LIB_FUNCTION
116 krb5_cc_register(krb5_context context
,
117 const krb5_cc_ops
*ops
,
118 krb5_boolean override
)
122 for(i
= 0; i
< context
->num_cc_ops
&& context
->cc_ops
[i
].prefix
; i
++) {
123 if(strcmp(context
->cc_ops
[i
].prefix
, ops
->prefix
) == 0) {
125 krb5_set_error_message(context
,
127 N_("cache type %s already exists", "type"),
129 return KRB5_CC_TYPE_EXISTS
;
134 if(i
== context
->num_cc_ops
) {
135 krb5_cc_ops
*o
= realloc(context
->cc_ops
,
136 (context
->num_cc_ops
+ 1) *
137 sizeof(*context
->cc_ops
));
139 krb5_set_error_message(context
, KRB5_CC_NOMEM
,
140 N_("malloc: out of memory", ""));
141 return KRB5_CC_NOMEM
;
143 context
->num_cc_ops
++;
145 memset(context
->cc_ops
+ i
, 0,
146 (context
->num_cc_ops
- i
) * sizeof(*context
->cc_ops
));
148 memcpy(&context
->cc_ops
[i
], ops
, sizeof(context
->cc_ops
[i
]));
153 * Allocate the memory for a `id' and the that function table to
154 * `ops'. Returns 0 or and error code.
158 _krb5_cc_allocate(krb5_context context
,
159 const krb5_cc_ops
*ops
,
164 p
= malloc (sizeof(*p
));
166 krb5_set_error_message(context
, KRB5_CC_NOMEM
,
167 N_("malloc: out of memory", ""));
168 return KRB5_CC_NOMEM
;
177 * Allocate memory for a new ccache in `id' with operations `ops'
178 * and name `residual'. Return 0 or an error code.
181 static krb5_error_code
182 allocate_ccache (krb5_context context
,
183 const krb5_cc_ops
*ops
,
184 const char *residual
,
189 ret
= _krb5_cc_allocate(context
, ops
, id
);
192 ret
= (*id
)->ops
->resolve(context
, id
, residual
);
199 * Find and allocate a ccache in `id' from the specification in `residual'.
200 * If the ccache name doesn't contain any colon, interpret it as a file name.
202 * @param context a Keberos context.
203 * @param name string name of a credential cache.
204 * @param id return pointer to a found credential cache.
206 * @return Return 0 or an error code. In case of an error, id is set
207 * to NULL, see krb5_get_error_message().
209 * @ingroup krb5_ccache
213 krb5_error_code KRB5_LIB_FUNCTION
214 krb5_cc_resolve(krb5_context context
,
222 for(i
= 0; i
< context
->num_cc_ops
&& context
->cc_ops
[i
].prefix
; i
++) {
223 size_t prefix_len
= strlen(context
->cc_ops
[i
].prefix
);
225 if(strncmp(context
->cc_ops
[i
].prefix
, name
, prefix_len
) == 0
226 && name
[prefix_len
] == ':') {
227 return allocate_ccache (context
, &context
->cc_ops
[i
],
228 name
+ prefix_len
+ 1,
232 if (strchr (name
, ':') == NULL
)
233 return allocate_ccache (context
, &krb5_fcc_ops
, name
, id
);
235 krb5_set_error_message(context
, KRB5_CC_UNKNOWN_TYPE
,
236 N_("unknown ccache type %s", "name"), name
);
237 return KRB5_CC_UNKNOWN_TYPE
;
242 * Generates a new unique ccache of `type` in `id'. If `type' is NULL,
243 * the library chooses the default credential cache type. The supplied
244 * `hint' (that can be NULL) is a string that the credential cache
245 * type can use to base the name of the credential on, this is to make
246 * it easier for the user to differentiate the credentials.
248 * @return Return an error code or 0, see krb5_get_error_message().
250 * @ingroup krb5_ccache
253 krb5_error_code KRB5_LIB_FUNCTION
254 krb5_cc_new_unique(krb5_context context
, const char *type
,
255 const char *hint
, krb5_ccache
*id
)
257 const krb5_cc_ops
*ops
;
260 ops
= krb5_cc_get_prefix_ops(context
, type
);
262 krb5_set_error_message(context
, KRB5_CC_UNKNOWN_TYPE
,
263 "Credential cache type %s is unknown", type
);
264 return KRB5_CC_UNKNOWN_TYPE
;
267 ret
= _krb5_cc_allocate(context
, ops
, id
);
270 ret
= (*id
)->ops
->gen_new(context
, id
);
279 * Return the name of the ccache `id'
281 * @ingroup krb5_ccache
285 const char* KRB5_LIB_FUNCTION
286 krb5_cc_get_name(krb5_context context
,
289 return id
->ops
->get_name(context
, id
);
293 * Return the type of the ccache `id'.
295 * @ingroup krb5_ccache
299 const char* KRB5_LIB_FUNCTION
300 krb5_cc_get_type(krb5_context context
,
303 return id
->ops
->prefix
;
307 * Return the complete resolvable name the ccache `id' in `str´.
308 * `str` should be freed with free(3).
309 * Returns 0 or an error (and then *str is set to NULL).
311 * @ingroup krb5_ccache
315 krb5_error_code KRB5_LIB_FUNCTION
316 krb5_cc_get_full_name(krb5_context context
,
320 const char *type
, *name
;
324 type
= krb5_cc_get_type(context
, id
);
326 krb5_set_error_message(context
, KRB5_CC_UNKNOWN_TYPE
,
327 "cache have no name of type");
328 return KRB5_CC_UNKNOWN_TYPE
;
331 name
= krb5_cc_get_name(context
, id
);
333 krb5_set_error_message(context
, KRB5_CC_BADNAME
,
334 "cache of type %s have no name", type
);
335 return KRB5_CC_BADNAME
;
338 if (asprintf(str
, "%s:%s", type
, name
) == -1) {
339 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
347 * Return krb5_cc_ops of a the ccache `id'.
349 * @ingroup krb5_ccache
354 krb5_cc_get_ops(krb5_context context
, krb5_ccache id
)
360 * Expand variables in `str' into `res'
364 _krb5_expand_default_cc_name(krb5_context context
, const char *str
, char **res
)
366 size_t tlen
, len
= 0;
367 char *tmp
, *tmp2
, *append
;
371 while (str
&& *str
) {
372 tmp
= strstr(str
, "%{");
373 if (tmp
&& tmp
!= str
) {
374 append
= malloc((tmp
- str
) + 1);
376 memcpy(append
, str
, tmp
- str
);
377 append
[tmp
- str
] = '\0';
381 tmp2
= strchr(tmp
, '}');
385 krb5_set_error_message(context
, KRB5_CONFIG_BADFORMAT
,
386 "variable missing }");
387 return KRB5_CONFIG_BADFORMAT
;
389 if (strncasecmp(tmp
, "%{uid}", 6) == 0)
390 asprintf(&append
, "%u", (unsigned)getuid());
391 else if (strncasecmp(tmp
, "%{null}", 7) == 0)
396 krb5_set_error_message(context
,
397 KRB5_CONFIG_BADFORMAT
,
398 "expand default cache unknown "
400 (int)(tmp2
- tmp
) - 2, tmp
+ 2);
401 return KRB5_CONFIG_BADFORMAT
;
405 append
= strdup(str
);
408 if (append
== NULL
) {
411 krb5_set_error_message(context
, ENOMEM
,
412 N_("malloc: out of memory", ""));
416 tlen
= strlen(append
);
417 tmp
= realloc(*res
, len
+ tlen
+ 1);
422 krb5_set_error_message(context
, ENOMEM
,
423 N_("malloc: out of memory", ""));
427 memcpy(*res
+ len
, append
, tlen
+ 1);
435 * Return non-zero if envirnoment that will determine default krb5cc
440 environment_changed(krb5_context context
)
444 /* if the cc name was set, don't change it */
445 if (context
->default_cc_name_set
)
451 e
= getenv("KRB5CCNAME");
453 if (context
->default_cc_name_env
) {
454 free(context
->default_cc_name_env
);
455 context
->default_cc_name_env
= NULL
;
459 if (context
->default_cc_name_env
== NULL
)
461 if (strcmp(e
, context
->default_cc_name_env
) != 0)
468 * Switch the default default credential cache for a specific
469 * credcache type (and name for some implementations).
471 * @return Return an error code or 0, see krb5_get_error_message().
473 * @ingroup krb5_ccache
477 krb5_cc_switch(krb5_context context
, krb5_ccache id
)
480 if (id
->ops
->set_default
== NULL
)
483 return (*id
->ops
->set_default
)(context
, id
);
487 * Set the default cc name for `context' to `name'.
489 * @ingroup krb5_ccache
492 krb5_error_code KRB5_LIB_FUNCTION
493 krb5_cc_set_default_name(krb5_context context
, const char *name
)
495 krb5_error_code ret
= 0;
499 const char *e
= NULL
;
502 e
= getenv("KRB5CCNAME");
505 if (context
->default_cc_name_env
)
506 free(context
->default_cc_name_env
);
507 context
->default_cc_name_env
= strdup(e
);
511 e
= krb5_config_get_string(context
, NULL
, "libdefaults",
512 "default_cc_name", NULL
);
514 ret
= _krb5_expand_default_cc_name(context
, e
, &p
);
519 const krb5_cc_ops
*ops
= KRB5_DEFAULT_CCTYPE
;
520 e
= krb5_config_get_string(context
, NULL
, "libdefaults",
521 "default_cc_type", NULL
);
523 ops
= krb5_cc_get_prefix_ops(context
, e
);
525 krb5_set_error_message(context
,
526 KRB5_CC_UNKNOWN_TYPE
,
527 "Credential cache type %s "
529 return KRB5_CC_UNKNOWN_TYPE
;
532 ret
= (*ops
->get_default_name
)(context
, &p
);
537 context
->default_cc_name_set
= 0;
540 context
->default_cc_name_set
= 1;
544 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
548 if (context
->default_cc_name
)
549 free(context
->default_cc_name
);
551 context
->default_cc_name
= p
;
557 * Return a pointer to a context static string containing the default
560 * @return String to the default credential cache name.
562 * @ingroup krb5_ccache
566 const char* KRB5_LIB_FUNCTION
567 krb5_cc_default_name(krb5_context context
)
569 if (context
->default_cc_name
== NULL
|| environment_changed(context
))
570 krb5_cc_set_default_name(context
, NULL
);
572 return context
->default_cc_name
;
576 * Open the default ccache in `id'.
578 * @return Return an error code or 0, see krb5_get_error_message().
580 * @ingroup krb5_ccache
584 krb5_error_code KRB5_LIB_FUNCTION
585 krb5_cc_default(krb5_context context
,
588 const char *p
= krb5_cc_default_name(context
);
591 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
594 return krb5_cc_resolve(context
, p
, id
);
598 * Create a new ccache in `id' for `primary_principal'.
600 * @return Return an error code or 0, see krb5_get_error_message().
602 * @ingroup krb5_ccache
606 krb5_error_code KRB5_LIB_FUNCTION
607 krb5_cc_initialize(krb5_context context
,
609 krb5_principal primary_principal
)
611 return (*id
->ops
->init
)(context
, id
, primary_principal
);
616 * Remove the ccache `id'.
618 * @return Return an error code or 0, see krb5_get_error_message().
620 * @ingroup krb5_ccache
624 krb5_error_code KRB5_LIB_FUNCTION
625 krb5_cc_destroy(krb5_context context
,
630 ret
= (*id
->ops
->destroy
)(context
, id
);
631 krb5_cc_close (context
, id
);
636 * Stop using the ccache `id' and free the related resources.
638 * @return Return an error code or 0, see krb5_get_error_message().
640 * @ingroup krb5_ccache
644 krb5_error_code KRB5_LIB_FUNCTION
645 krb5_cc_close(krb5_context context
,
649 ret
= (*id
->ops
->close
)(context
, id
);
655 * Store `creds' in the ccache `id'.
657 * @return Return an error code or 0, see krb5_get_error_message().
659 * @ingroup krb5_ccache
663 krb5_error_code KRB5_LIB_FUNCTION
664 krb5_cc_store_cred(krb5_context context
,
668 return (*id
->ops
->store
)(context
, id
, creds
);
672 * Retrieve the credential identified by `mcreds' (and `whichfields')
673 * from `id' in `creds'. 'creds' must be free by the caller using
674 * krb5_free_cred_contents.
676 * @return Return an error code or 0, see krb5_get_error_message().
678 * @ingroup krb5_ccache
682 krb5_error_code KRB5_LIB_FUNCTION
683 krb5_cc_retrieve_cred(krb5_context context
,
685 krb5_flags whichfields
,
686 const krb5_creds
*mcreds
,
690 krb5_cc_cursor cursor
;
692 if (id
->ops
->retrieve
!= NULL
) {
693 return (*id
->ops
->retrieve
)(context
, id
, whichfields
,
697 ret
= krb5_cc_start_seq_get(context
, id
, &cursor
);
700 while((ret
= krb5_cc_next_cred(context
, id
, &cursor
, creds
)) == 0){
701 if(krb5_compare_creds(context
, whichfields
, mcreds
, creds
)){
705 krb5_free_cred_contents (context
, creds
);
707 krb5_cc_end_seq_get(context
, id
, &cursor
);
712 * Return the principal of `id' in `principal'.
714 * @return Return an error code or 0, see krb5_get_error_message().
716 * @ingroup krb5_ccache
720 krb5_error_code KRB5_LIB_FUNCTION
721 krb5_cc_get_principal(krb5_context context
,
723 krb5_principal
*principal
)
725 return (*id
->ops
->get_princ
)(context
, id
, principal
);
729 * Start iterating over `id', `cursor' is initialized to the
730 * beginning. Caller must free the cursor with krb5_cc_end_seq_get().
732 * @return Return an error code or 0, see krb5_get_error_message().
734 * @ingroup krb5_ccache
738 krb5_error_code KRB5_LIB_FUNCTION
739 krb5_cc_start_seq_get (krb5_context context
,
740 const krb5_ccache id
,
741 krb5_cc_cursor
*cursor
)
743 return (*id
->ops
->get_first
)(context
, id
, cursor
);
747 * Retrieve the next cred pointed to by (`id', `cursor') in `creds'
748 * and advance `cursor'.
750 * @return Return an error code or 0, see krb5_get_error_message().
752 * @ingroup krb5_ccache
756 krb5_error_code KRB5_LIB_FUNCTION
757 krb5_cc_next_cred (krb5_context context
,
758 const krb5_ccache id
,
759 krb5_cc_cursor
*cursor
,
762 return (*id
->ops
->get_next
)(context
, id
, cursor
, creds
);
766 * Destroy the cursor `cursor'.
768 * @ingroup krb5_ccache
772 krb5_error_code KRB5_LIB_FUNCTION
773 krb5_cc_end_seq_get (krb5_context context
,
774 const krb5_ccache id
,
775 krb5_cc_cursor
*cursor
)
777 return (*id
->ops
->end_get
)(context
, id
, cursor
);
781 * Remove the credential identified by `cred', `which' from `id'.
783 * @ingroup krb5_ccache
787 krb5_error_code KRB5_LIB_FUNCTION
788 krb5_cc_remove_cred(krb5_context context
,
793 if(id
->ops
->remove_cred
== NULL
) {
794 krb5_set_error_message(context
,
796 "ccache %s does not support remove_cred",
798 return EACCES
; /* XXX */
800 return (*id
->ops
->remove_cred
)(context
, id
, which
, cred
);
804 * Set the flags of `id' to `flags'.
806 * @ingroup krb5_ccache
810 krb5_error_code KRB5_LIB_FUNCTION
811 krb5_cc_set_flags(krb5_context context
,
815 return (*id
->ops
->set_flags
)(context
, id
, flags
);
819 * Get the flags of `id', store them in `flags'.
821 * @ingroup krb5_ccache
824 krb5_error_code KRB5_LIB_FUNCTION
825 krb5_cc_get_flags(krb5_context context
,
834 * Copy the contents of `from' to `to' if the given match function
837 * @param context A Kerberos 5 context.
838 * @param from the cache to copy data from.
839 * @param to the cache to copy data to.
840 * @param match a match function that should return TRUE if cred argument should be copied, if NULL, all credentials are copied.
841 * @param matchctx context passed to match function.
842 * @param matched set to true if there was a credential that matched, may be NULL.
844 * @return Return an error code or 0, see krb5_get_error_message().
846 * @ingroup krb5_ccache
849 krb5_error_code KRB5_LIB_FUNCTION
850 krb5_cc_copy_match_f(krb5_context context
,
851 const krb5_ccache from
,
853 krb5_boolean (*match
)(krb5_context
, void *, const krb5_creds
*),
855 unsigned int *matched
)
858 krb5_cc_cursor cursor
;
860 krb5_principal princ
;
865 ret
= krb5_cc_get_principal(context
, from
, &princ
);
868 ret
= krb5_cc_initialize(context
, to
, princ
);
870 krb5_free_principal(context
, princ
);
873 ret
= krb5_cc_start_seq_get(context
, from
, &cursor
);
875 krb5_free_principal(context
, princ
);
879 while ((ret
= krb5_cc_next_cred(context
, from
, &cursor
, &cred
)) == 0) {
880 if (match
== NULL
|| (*match
)(context
, matchctx
, &cred
) == 0) {
883 ret
= krb5_cc_store_cred(context
, to
, &cred
);
887 krb5_free_cred_contents(context
, &cred
);
889 krb5_cc_end_seq_get(context
, from
, &cursor
);
890 krb5_free_principal(context
, princ
);
891 if (ret
== KRB5_CC_END
)
897 * Just like krb5_cc_copy_match_f(), but copy everything.
899 * @ingroup @krb5_ccache
902 krb5_error_code KRB5_LIB_FUNCTION
903 krb5_cc_copy_cache(krb5_context context
,
904 const krb5_ccache from
,
907 return krb5_cc_copy_match_f(context
, from
, to
, NULL
, NULL
, NULL
);
911 * Return the version of `id'.
913 * @ingroup krb5_ccache
917 krb5_error_code KRB5_LIB_FUNCTION
918 krb5_cc_get_version(krb5_context context
,
919 const krb5_ccache id
)
921 if(id
->ops
->get_version
)
922 return (*id
->ops
->get_version
)(context
, id
);
928 * Clear `mcreds' so it can be used with krb5_cc_retrieve_cred
930 * @ingroup krb5_ccache
934 void KRB5_LIB_FUNCTION
935 krb5_cc_clear_mcred(krb5_creds
*mcred
)
937 memset(mcred
, 0, sizeof(*mcred
));
941 * Get the cc ops that is registered in `context' to handle the
942 * prefix. prefix can be a complete credential cache name or a
943 * prefix, the function will only use part up to the first colon (:)
944 * if there is one. If prefix the argument is NULL, the default ccache
945 * implemtation is returned.
947 * @return Returns NULL if ops not found.
949 * @ingroup krb5_ccache
954 krb5_cc_get_prefix_ops(krb5_context context
, const char *prefix
)
960 return KRB5_DEFAULT_CCTYPE
;
961 if (prefix
[0] == '/')
962 return &krb5_fcc_ops
;
966 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
973 for(i
= 0; i
< context
->num_cc_ops
&& context
->cc_ops
[i
].prefix
; i
++) {
974 if(strcmp(context
->cc_ops
[i
].prefix
, p
) == 0) {
976 return &context
->cc_ops
[i
];
983 struct krb5_cc_cache_cursor_data
{
984 const krb5_cc_ops
*ops
;
985 krb5_cc_cursor cursor
;
989 * Start iterating over all caches of specified type. See also
990 * krb5_cccol_cursor_new().
992 * @param context A Kerberos 5 context
993 * @param type optional type to iterate over, if NULL, the default cache is used.
994 * @param cursor cursor should be freed with krb5_cc_cache_end_seq_get().
996 * @return Return an error code or 0, see krb5_get_error_message().
998 * @ingroup krb5_ccache
1002 krb5_error_code KRB5_LIB_FUNCTION
1003 krb5_cc_cache_get_first (krb5_context context
,
1005 krb5_cc_cache_cursor
*cursor
)
1007 const krb5_cc_ops
*ops
;
1008 krb5_error_code ret
;
1011 type
= krb5_cc_default_name(context
);
1013 ops
= krb5_cc_get_prefix_ops(context
, type
);
1015 krb5_set_error_message(context
, KRB5_CC_UNKNOWN_TYPE
,
1016 "Unknown type \"%s\" when iterating "
1017 "trying to iterate the credential caches", type
);
1018 return KRB5_CC_UNKNOWN_TYPE
;
1021 if (ops
->get_cache_first
== NULL
) {
1022 krb5_set_error_message(context
, KRB5_CC_NOSUPP
,
1023 N_("Credential cache type %s doesn't support "
1024 "iterations over caches", "type"),
1026 return KRB5_CC_NOSUPP
;
1029 *cursor
= calloc(1, sizeof(**cursor
));
1030 if (*cursor
== NULL
) {
1031 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
1035 (*cursor
)->ops
= ops
;
1037 ret
= ops
->get_cache_first(context
, &(*cursor
)->cursor
);
1046 * Retrieve the next cache pointed to by (`cursor') in `id'
1047 * and advance `cursor'.
1049 * @param context A Kerberos 5 context
1050 * @param cursor the iterator cursor, returned by krb5_cc_cache_get_first()
1051 * @param id next ccache
1053 * @return Return 0 or an error code. Returns KRB5_CC_END when the end
1054 * of caches is reached, see krb5_get_error_message().
1056 * @ingroup krb5_ccache
1060 krb5_error_code KRB5_LIB_FUNCTION
1061 krb5_cc_cache_next (krb5_context context
,
1062 krb5_cc_cache_cursor cursor
,
1065 return cursor
->ops
->get_cache_next(context
, cursor
->cursor
, id
);
1069 * Destroy the cursor `cursor'.
1071 * @return Return an error code or 0, see krb5_get_error_message().
1073 * @ingroup krb5_ccache
1077 krb5_error_code KRB5_LIB_FUNCTION
1078 krb5_cc_cache_end_seq_get (krb5_context context
,
1079 krb5_cc_cache_cursor cursor
)
1081 krb5_error_code ret
;
1082 ret
= cursor
->ops
->end_cache_get(context
, cursor
->cursor
);
1089 * Search for a matching credential cache that have the
1090 * `principal' as the default principal. On success, `id' needs to be
1091 * freed with krb5_cc_close() or krb5_cc_destroy().
1093 * @param context A Kerberos 5 context
1094 * @param client The principal to search for
1095 * @param id the returned credential cache
1097 * @return On failure, error code is returned and `id' is set to NULL.
1099 * @ingroup krb5_ccache
1103 krb5_error_code KRB5_LIB_FUNCTION
1104 krb5_cc_cache_match (krb5_context context
,
1105 krb5_principal client
,
1108 krb5_cccol_cursor cursor
;
1109 krb5_error_code ret
;
1110 krb5_ccache cache
= NULL
;
1114 ret
= krb5_cccol_cursor_new (context
, &cursor
);
1118 while (krb5_cccol_cursor_next (context
, cursor
, &cache
) == 0 && cache
!= NULL
) {
1119 krb5_principal principal
;
1121 ret
= krb5_cc_get_principal(context
, cache
, &principal
);
1125 match
= krb5_principal_compare(context
, principal
, client
);
1126 krb5_free_principal(context
, principal
);
1131 krb5_cc_close(context
, cache
);
1135 krb5_cccol_cursor_free(context
, &cursor
);
1137 if (cache
== NULL
) {
1140 krb5_unparse_name(context
, client
, &str
);
1142 krb5_set_error_message(context
, KRB5_CC_NOTFOUND
,
1143 N_("Principal %s not found in any "
1144 "credential cache", ""),
1145 str
? str
: "<out of memory>");
1148 return KRB5_CC_NOTFOUND
;
1156 * Move the content from one credential cache to another. The
1157 * operation is an atomic switch.
1159 * @param context a Keberos context
1160 * @param from the credential cache to move the content from
1161 * @param to the credential cache to move the content to
1163 * @return On sucess, from is freed. On failure, error code is
1164 * returned and from and to are both still allocated, see krb5_get_error_message().
1166 * @ingroup krb5_ccache
1170 krb5_cc_move(krb5_context context
, krb5_ccache from
, krb5_ccache to
)
1172 krb5_error_code ret
;
1174 if (strcmp(from
->ops
->prefix
, to
->ops
->prefix
) != 0) {
1175 krb5_set_error_message(context
, KRB5_CC_NOSUPP
,
1176 N_("Moving credentials between diffrent "
1177 "types not yet supported", ""));
1178 return KRB5_CC_NOSUPP
;
1181 ret
= (*to
->ops
->move
)(context
, from
, to
);
1183 memset(from
, 0, sizeof(*from
));
1189 #define KRB5_CONF_NAME "krb5_ccache_conf_data"
1190 #define KRB5_REALM_NAME "X-CACHECONF:"
1192 static krb5_error_code
1193 build_conf_principals(krb5_context context
, krb5_ccache id
,
1194 krb5_const_principal principal
,
1195 const char *name
, krb5_creds
*cred
)
1197 krb5_principal client
;
1198 krb5_error_code ret
;
1201 memset(cred
, 0, sizeof(*cred
));
1203 ret
= krb5_cc_get_principal(context
, id
, &client
);
1208 ret
= krb5_unparse_name(context
, principal
, &pname
);
1213 ret
= krb5_make_principal(context
, &cred
->server
,
1215 KRB5_CONF_NAME
, name
, pname
, NULL
);
1218 krb5_free_principal(context
, client
);
1221 ret
= krb5_copy_principal(context
, client
, &cred
->client
);
1222 krb5_free_principal(context
, client
);
1227 * Return TRUE (non zero) if the principal is a configuration
1228 * principal (generated part of krb5_cc_set_config()). Returns FALSE
1229 * (zero) if not a configuration principal.
1231 * @param context a Keberos context
1232 * @param principal principal to check if it a configuration principal
1234 * @ingroup krb5_ccache
1237 krb5_boolean KRB5_LIB_FUNCTION
1238 krb5_is_config_principal(krb5_context context
,
1239 krb5_const_principal principal
)
1241 if (strcmp(principal
->realm
, KRB5_REALM_NAME
) != 0)
1244 if (principal
->name
.name_string
.len
== 0 ||
1245 strcmp(principal
->name
.name_string
.val
[0], KRB5_CONF_NAME
) != 0)
1252 * Store some configuration for the credential cache in the cache.
1253 * Existing configuration under the same name is over-written.
1255 * @param context a Keberos context
1256 * @param id the credential cache to store the data for
1257 * @param principal configuration for a specific principal, if
1258 * NULL, global for the whole cache.
1259 * @param name name under which the configuraion is stored.
1260 * @param data data to store, if NULL, configure is removed.
1262 * @ingroup krb5_ccache
1265 krb5_error_code KRB5_LIB_FUNCTION
1266 krb5_cc_set_config(krb5_context context
, krb5_ccache id
,
1267 krb5_const_principal principal
,
1268 const char *name
, krb5_data
*data
)
1270 krb5_error_code ret
;
1273 ret
= build_conf_principals(context
, id
, principal
, name
, &cred
);
1277 /* Remove old configuration */
1278 ret
= krb5_cc_remove_cred(context
, id
, 0, &cred
);
1279 if (ret
&& ret
!= KRB5_CC_NOTFOUND
)
1283 /* not that anyone care when this expire */
1284 cred
.times
.authtime
= time(NULL
);
1285 cred
.times
.endtime
= cred
.times
.authtime
+ 3600 * 24 * 30;
1287 ret
= krb5_data_copy(&cred
.ticket
, data
->data
, data
->length
);
1291 ret
= krb5_cc_store_cred(context
, id
, &cred
);
1295 krb5_free_cred_contents (context
, &cred
);
1300 * Get some configuration for the credential cache in the cache.
1302 * @param context a Keberos context
1303 * @param id the credential cache to store the data for
1304 * @param principal configuration for a specific principal, if
1305 * NULL, global for the whole cache.
1306 * @param name name under which the configuraion is stored.
1307 * @param data data to fetched, free with krb5_data_free()
1309 * @ingroup krb5_ccache
1313 krb5_error_code KRB5_LIB_FUNCTION
1314 krb5_cc_get_config(krb5_context context
, krb5_ccache id
,
1315 krb5_const_principal principal
,
1316 const char *name
, krb5_data
*data
)
1318 krb5_creds mcred
, cred
;
1319 krb5_error_code ret
;
1321 memset(&cred
, 0, sizeof(cred
));
1322 krb5_data_zero(data
);
1324 ret
= build_conf_principals(context
, id
, principal
, name
, &mcred
);
1328 ret
= krb5_cc_retrieve_cred(context
, id
, 0, &mcred
, &cred
);
1332 ret
= krb5_data_copy(data
, cred
.ticket
.data
, cred
.ticket
.length
);
1335 krb5_free_cred_contents (context
, &cred
);
1336 krb5_free_cred_contents (context
, &mcred
);
1344 struct krb5_cccol_cursor
{
1346 krb5_cc_cache_cursor cursor
;
1350 * Get a new cache interation cursor that will interate over all
1351 * credentials caches independent of type.
1353 * @param context a Keberos context
1354 * @param cursor passed into krb5_cccol_cursor_next() and free with krb5_cccol_cursor_free().
1356 * @return Returns 0 or and error code, see krb5_get_error_message().
1358 * @ingroup krb5_ccache
1361 krb5_error_code KRB5_LIB_FUNCTION
1362 krb5_cccol_cursor_new(krb5_context context
, krb5_cccol_cursor
*cursor
)
1364 *cursor
= calloc(1, sizeof(**cursor
));
1365 if (*cursor
== NULL
) {
1366 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
1370 (*cursor
)->cursor
= NULL
;
1376 * Get next credential cache from the iteration.
1378 * @param context A Kerberos 5 context
1379 * @param cursor the iteration cursor
1380 * @param cache the returned cursor, pointer is set to NULL on failure
1381 * and a cache on success. The returned cache needs to be freed
1382 * with krb5_cc_close() or destroyed with krb5_cc_destroy().
1383 * MIT Kerberos behavies slightly diffrent and sets cache to NULL
1384 * when all caches are iterated over and return 0.
1386 * @return Return 0 or and error, KRB5_CC_END is returned at the end
1387 * of iteration. See krb5_get_error_message().
1389 * @ingroup krb5_ccache
1393 krb5_error_code KRB5_LIB_FUNCTION
1394 krb5_cccol_cursor_next(krb5_context context
, krb5_cccol_cursor cursor
,
1397 krb5_error_code ret
;
1401 while (cursor
->idx
< context
->num_cc_ops
) {
1403 if (cursor
->cursor
== NULL
) {
1404 ret
= krb5_cc_cache_get_first (context
,
1405 context
->cc_ops
[cursor
->idx
].prefix
,
1412 ret
= krb5_cc_cache_next(context
, cursor
->cursor
, cache
);
1416 krb5_cc_cache_end_seq_get(context
, cursor
->cursor
);
1417 cursor
->cursor
= NULL
;
1418 if (ret
!= KRB5_CC_END
)
1423 if (cursor
->idx
>= context
->num_cc_ops
) {
1424 krb5_set_error_message(context
, KRB5_CC_END
,
1425 N_("Reached end of credential caches", ""));
1433 * End an iteration and free all resources, can be done before end is reached.
1435 * @param context A Kerberos 5 context
1436 * @param cursor the iteration cursor to be freed.
1438 * @return Return 0 or and error, KRB5_CC_END is returned at the end
1439 * of iteration. See krb5_get_error_message().
1441 * @ingroup krb5_ccache
1444 krb5_error_code KRB5_LIB_FUNCTION
1445 krb5_cccol_cursor_free(krb5_context context
, krb5_cccol_cursor
*cursor
)
1447 krb5_cccol_cursor c
= *cursor
;
1452 krb5_cc_cache_end_seq_get(context
, c
->cursor
);
1459 * Return the last time the credential cache was modified.
1461 * @param context A Kerberos 5 context
1462 * @param id The credential cache to probe
1463 * @param mtime the last modification time, set to 0 on error.
1465 * @return Return 0 or and error. See krb5_get_error_message().
1467 * @ingroup krb5_ccache
1471 krb5_error_code KRB5_LIB_FUNCTION
1472 krb5_cc_last_change_time(krb5_context context
,
1474 krb5_timestamp
*mtime
)
1477 return (*id
->ops
->lastchange
)(context
, id
, mtime
);
1481 * Return the last modfication time for a cache collection. The query
1482 * can be limited to a specific cache type. If the function return 0
1483 * and mtime is 0, there was no credentials in the caches.
1485 * @param context A Kerberos 5 context
1486 * @param type The credential cache to probe, if NULL, all type are traversed.
1487 * @param mtime the last modification time, set to 0 on error.
1489 * @return Return 0 or and error. See krb5_get_error_message().
1491 * @ingroup krb5_ccache
1494 krb5_error_code KRB5_LIB_FUNCTION
1495 krb5_cccol_last_change_time(krb5_context context
,
1497 krb5_timestamp
*mtime
)
1499 krb5_cccol_cursor cursor
;
1500 krb5_error_code ret
;
1502 krb5_timestamp t
= 0;
1506 ret
= krb5_cccol_cursor_new (context
, &cursor
);
1510 while (krb5_cccol_cursor_next(context
, cursor
, &id
) == 0 && id
!= NULL
) {
1512 if (type
&& strcmp(krb5_cc_get_type(context
, id
), type
) != 0)
1515 ret
= krb5_cc_last_change_time(context
, id
, &t
);
1516 krb5_cc_close(context
, id
);
1523 krb5_cccol_cursor_free(context
, &cursor
);
1528 * Return a friendly name on credential cache. Free the result with krb5_xfree().
1530 * @return Return an error code or 0, see krb5_get_error_message().
1532 * @ingroup krb5_ccache
1535 krb5_error_code KRB5_LIB_FUNCTION
1536 krb5_cc_get_friendly_name(krb5_context context
,
1540 krb5_error_code ret
;
1543 ret
= krb5_cc_get_config(context
, id
, NULL
, "FriendlyName", &data
);
1545 krb5_principal principal
;
1546 ret
= krb5_cc_get_principal(context
, id
, &principal
);
1549 ret
= krb5_unparse_name(context
, principal
, name
);
1550 krb5_free_principal(context
, principal
);
1552 ret
= asprintf(name
, "%.*s", (int)data
.length
, (char *)data
.data
);
1553 krb5_data_free(&data
);
1556 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
1565 * Set the friendly name on credential cache.
1567 * @return Return an error code or 0, see krb5_get_error_message().
1569 * @ingroup krb5_ccache
1572 krb5_error_code KRB5_LIB_FUNCTION
1573 krb5_cc_set_friendly_name(krb5_context context
,
1579 data
.data
= rk_UNCONST(name
);
1580 data
.length
= strlen(name
);
1582 return krb5_cc_set_config(context
, id
, NULL
, "FriendlyName", &data
);
1586 * Get the lifetime of the initial ticket in the cache
1588 * Get the lifetime of the initial ticket in the cache, if the initial
1589 * ticket was not found, the error code KRB5_CC_END is returned.
1591 * @param context A Kerberos 5 context.
1592 * @param id a credential cache
1593 * @param t the relative lifetime of the initial ticket
1595 * @return Return an error code or 0, see krb5_get_error_message().
1597 * @ingroup krb5_ccache
1600 krb5_error_code KRB5_LIB_FUNCTION
1601 krb5_cc_get_lifetime(krb5_context context
, krb5_ccache id
, time_t *t
)
1603 krb5_cc_cursor cursor
;
1604 krb5_error_code ret
;
1611 ret
= krb5_cc_start_seq_get(context
, id
, &cursor
);
1615 while ((ret
= krb5_cc_next_cred(context
, id
, &cursor
, &cred
)) == 0) {
1616 if (cred
.flags
.b
.initial
) {
1617 if (now
< cred
.times
.endtime
)
1618 *t
= cred
.times
.endtime
- now
;
1619 krb5_free_cred_contents(context
, &cred
);
1622 krb5_free_cred_contents(context
, &cred
);
1626 krb5_cc_end_seq_get(context
, id
, &cursor
);