Don't return a freed pointer in allocate_ccache()
[abartlet/lorikeet-heimdal.git/.git] / lib / krb5 / cache.c
1 /*
2  * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
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.
18  *
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.
22  *
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
33  * SUCH DAMAGE.
34  */
35
36 #include "krb5_locl.h"
37
38 /**
39  * @page krb5_ccache_intro The credential cache functions
40  * @section section_krb5_ccache Kerberos credential caches
41  * 
42  * krb5_ccache structure holds a Kerberos credential cache.
43  *
44  * Heimdal support the follow types of credential caches:
45  *
46  * - SCC
47  *   Store the credential in a database
48  * - FILE
49  *   Store the credential in memory
50  * - MEMORY
51  *   Store the credential in memory
52  * - API
53  *   A credential cache server based solution for Mac OS X
54  * - KCM
55  *   A credential cache server based solution for all platforms
56  *
57  * @subsection Example
58  *
59  * This is a minimalistic version of klist:
60 @code
61 #include <krb5.h>
62
63 int
64 main (int argc, char **argv)
65 {
66     krb5_context context;
67     krb5_cc_cursor cursor;
68     krb5_error_code ret;
69     krb5_ccache id;
70     krb5_creds creds;
71
72     if (krb5_init_context (&context) != 0)
73         errx(1, "krb5_context");
74
75     ret = krb5_cc_default (context, &id);
76     if (ret)
77         krb5_err(context, 1, ret, "krb5_cc_default");
78
79     ret = krb5_cc_start_seq_get(context, id, &cursor);
80     if (ret)
81         krb5_err(context, 1, ret, "krb5_cc_start_seq_get");
82
83     while((ret = krb5_cc_next_cred(context, id, &cursor, &creds)) == 0){
84         char *principal;
85
86         krb5_unparse_name_short(context, creds.server, &principal);
87         printf("principal: %s\\n", principal);
88         free(principal);
89         krb5_free_cred_contents (context, &creds);
90     }
91     ret = krb5_cc_end_seq_get(context, id, &cursor);
92     if (ret)
93         krb5_err(context, 1, ret, "krb5_cc_end_seq_get");
94
95     krb5_cc_close(context, id);
96
97     krb5_free_context(context);
98     return 0;
99 }
100 * @endcode
101 */
102
103 /**
104  * Add a new ccache type with operations `ops', overwriting any
105  * existing one if `override'.
106  *
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.
111  *
112  * @return Return an error code or 0, see krb5_get_error_message().
113  *
114  * @ingroup krb5_ccache
115  */
116
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)
121 {
122     int i;
123
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) {
126             if(!override) {
127                 krb5_set_error_message(context,
128                                        KRB5_CC_TYPE_EXISTS,
129                                        N_("cache type %s already exists", "type"),
130                                        ops->prefix);
131                 return KRB5_CC_TYPE_EXISTS;
132             }
133             break;
134         }
135     }
136     if(i == context->num_cc_ops) {
137         const krb5_cc_ops **o = realloc(context->cc_ops,
138                                         (context->num_cc_ops + 1) *
139                                         sizeof(context->cc_ops[0]));
140         if(o == NULL) {
141             krb5_set_error_message(context, KRB5_CC_NOMEM,
142                                    N_("malloc: out of memory", ""));
143             return KRB5_CC_NOMEM;
144         }
145         context->cc_ops = o;
146         context->cc_ops[context->num_cc_ops] = NULL;
147         context->num_cc_ops++;
148     }
149     context->cc_ops[i] = ops;
150     return 0;
151 }
152
153 /*
154  * Allocate the memory for a `id' and the that function table to
155  * `ops'. Returns 0 or and error code.
156  */
157
158 krb5_error_code
159 _krb5_cc_allocate(krb5_context context,
160                   const krb5_cc_ops *ops,
161                   krb5_ccache *id)
162 {
163     krb5_ccache p;
164
165     p = malloc (sizeof(*p));
166     if(p == NULL) {
167         krb5_set_error_message(context, KRB5_CC_NOMEM,
168                                N_("malloc: out of memory", ""));
169         return KRB5_CC_NOMEM;
170     }
171     p->ops = ops;
172     *id = p;
173
174     return 0;
175 }
176
177 /*
178  * Allocate memory for a new ccache in `id' with operations `ops'
179  * and name `residual'. Return 0 or an error code.
180  */
181
182 static krb5_error_code
183 allocate_ccache (krb5_context context,
184                  const krb5_cc_ops *ops,
185                  const char *residual,
186                  krb5_ccache *id)
187 {
188     krb5_error_code ret;
189 #ifdef KRB5_USE_PATH_TOKENS
190     char * exp_residual = NULL;
191
192     ret = _krb5_expand_path_tokens(context, residual, &exp_residual);
193     if (ret)
194         return ret;
195
196     residual = exp_residual;
197 #endif
198
199     ret = _krb5_cc_allocate(context, ops, id);
200     if (ret) {
201 #ifdef KRB5_USE_PATH_TOKENS
202         if (exp_residual)
203             free(exp_residual);
204 #endif
205         return ret;
206     }
207
208     ret = (*id)->ops->resolve(context, id, residual);
209     if(ret) {
210         free(*id);
211         *id = NULL;
212     }
213
214 #ifdef KRB5_USE_PATH_TOKENS
215     if (exp_residual)
216         free(exp_residual);
217 #endif
218
219     return ret;
220 }
221
222 static int
223 is_possible_path_name(const char * name)
224 {
225     const char * colon;
226
227     if ((colon = strchr(name, ':')) == NULL)
228         return TRUE;
229
230 #ifdef _WIN32
231     /* <drive letter>:\path\to\cache ? */
232
233     if (colon == name + 1 &&
234         strchr(colon + 1, ':') == NULL)
235         return TRUE;
236 #endif
237
238     return FALSE;
239 }
240
241 /**
242  * Find and allocate a ccache in `id' from the specification in `residual'.
243  * If the ccache name doesn't contain any colon, interpret it as a file name.
244  *
245  * @param context a Keberos context.
246  * @param name string name of a credential cache.
247  * @param id return pointer to a found credential cache.
248  *
249  * @return Return 0 or an error code. In case of an error, id is set
250  * to NULL, see krb5_get_error_message().
251  *
252  * @ingroup krb5_ccache
253  */
254
255
256 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
257 krb5_cc_resolve(krb5_context context,
258                 const char *name,
259                 krb5_ccache *id)
260 {
261     int i;
262
263     *id = NULL;
264
265     for(i = 0; i < context->num_cc_ops && context->cc_ops[i]->prefix; i++) {
266         size_t prefix_len = strlen(context->cc_ops[i]->prefix);
267
268         if(strncmp(context->cc_ops[i]->prefix, name, prefix_len) == 0
269            && name[prefix_len] == ':') {
270             return allocate_ccache (context, context->cc_ops[i],
271                                     name + prefix_len + 1,
272                                     id);
273         }
274     }
275     if (is_possible_path_name(name))
276         return allocate_ccache (context, &krb5_fcc_ops, name, id);
277     else {
278         krb5_set_error_message(context, KRB5_CC_UNKNOWN_TYPE,
279                                N_("unknown ccache type %s", "name"), name);
280         return KRB5_CC_UNKNOWN_TYPE;
281     }
282 }
283
284 /**
285  * Generates a new unique ccache of `type` in `id'. If `type' is NULL,
286  * the library chooses the default credential cache type. The supplied
287  * `hint' (that can be NULL) is a string that the credential cache
288  * type can use to base the name of the credential on, this is to make
289  * it easier for the user to differentiate the credentials.
290  *
291  * @return Return an error code or 0, see krb5_get_error_message().
292  *
293  * @ingroup krb5_ccache
294  */
295
296 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
297 krb5_cc_new_unique(krb5_context context, const char *type,
298                    const char *hint, krb5_ccache *id)
299 {
300     const krb5_cc_ops *ops;
301     krb5_error_code ret;
302
303     ops = krb5_cc_get_prefix_ops(context, type);
304     if (ops == NULL) {
305         krb5_set_error_message(context, KRB5_CC_UNKNOWN_TYPE,
306                               "Credential cache type %s is unknown", type);
307         return KRB5_CC_UNKNOWN_TYPE;
308     }
309
310     ret = _krb5_cc_allocate(context, ops, id);
311     if (ret)
312         return ret;
313     ret = (*id)->ops->gen_new(context, id);
314     if (ret) {
315         free(*id);
316         *id = NULL;
317     }
318     return ret;
319 }
320
321 /**
322  * Return the name of the ccache `id'
323  *
324  * @ingroup krb5_ccache
325  */
326
327
328 KRB5_LIB_FUNCTION const char* KRB5_LIB_CALL
329 krb5_cc_get_name(krb5_context context,
330                  krb5_ccache id)
331 {
332     return id->ops->get_name(context, id);
333 }
334
335 /**
336  * Return the type of the ccache `id'.
337  *
338  * @ingroup krb5_ccache
339  */
340
341
342 KRB5_LIB_FUNCTION const char* KRB5_LIB_CALL
343 krb5_cc_get_type(krb5_context context,
344                  krb5_ccache id)
345 {
346     return id->ops->prefix;
347 }
348
349 /**
350  * Return the complete resolvable name the cache
351
352  * @param context a Keberos context
353  * @param id return pointer to a found credential cache
354  * @param str the returned name of a credential cache, free with krb5_xfree()
355  *
356  * @return Returns 0 or an error (and then *str is set to NULL).
357  *
358  * @ingroup krb5_ccache
359  */
360
361
362 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
363 krb5_cc_get_full_name(krb5_context context,
364                       krb5_ccache id,
365                       char **str)
366 {
367     const char *type, *name;
368
369     *str = NULL;
370
371     type = krb5_cc_get_type(context, id);
372     if (type == NULL) {
373         krb5_set_error_message(context, KRB5_CC_UNKNOWN_TYPE,
374                                "cache have no name of type");
375         return KRB5_CC_UNKNOWN_TYPE;
376     }
377
378     name = krb5_cc_get_name(context, id);
379     if (name == NULL) {
380         krb5_set_error_message(context, KRB5_CC_BADNAME,
381                                "cache of type %s have no name", type);
382         return KRB5_CC_BADNAME;
383     }
384
385     if (asprintf(str, "%s:%s", type, name) == -1) {
386         krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
387         *str = NULL;
388         return ENOMEM;
389     }
390     return 0;
391 }
392
393 /**
394  * Return krb5_cc_ops of a the ccache `id'.
395  *
396  * @ingroup krb5_ccache
397  */
398
399
400 const krb5_cc_ops *
401 krb5_cc_get_ops(krb5_context context, krb5_ccache id)
402 {
403     return id->ops;
404 }
405
406 /*
407  * Expand variables in `str' into `res'
408  */
409
410 krb5_error_code
411 _krb5_expand_default_cc_name(krb5_context context, const char *str, char **res)
412 {
413     return _krb5_expand_path_tokens(context, str, res);
414 }
415
416 /*
417  * Return non-zero if envirnoment that will determine default krb5cc
418  * name has changed.
419  */
420
421 static int
422 environment_changed(krb5_context context)
423 {
424     const char *e;
425
426     /* if the cc name was set, don't change it */
427     if (context->default_cc_name_set)
428         return 0;
429
430     /* XXX performance: always ask KCM/API if default name has changed */
431     if (context->default_cc_name &&
432         (strncmp(context->default_cc_name, "KCM:", 4) == 0 ||
433          strncmp(context->default_cc_name, "API:", 4) == 0))
434         return 1;
435
436     if(issuid())
437         return 0;
438
439     e = getenv("KRB5CCNAME");
440     if (e == NULL) {
441         if (context->default_cc_name_env) {
442             free(context->default_cc_name_env);
443             context->default_cc_name_env = NULL;
444             return 1;
445         }
446     } else {
447         if (context->default_cc_name_env == NULL)
448             return 1;
449         if (strcmp(e, context->default_cc_name_env) != 0)
450             return 1;
451     }
452     return 0;
453 }
454
455 /**
456  * Switch the default default credential cache for a specific
457  * credcache type (and name for some implementations).
458  *
459  * @return Return an error code or 0, see krb5_get_error_message().
460  *
461  * @ingroup krb5_ccache
462  */
463
464 krb5_error_code KRB5_LIB_FUNCTION
465 krb5_cc_switch(krb5_context context, krb5_ccache id)
466 {
467
468     if (id->ops->set_default == NULL)
469         return 0;
470
471     return (*id->ops->set_default)(context, id);
472 }
473
474 /**
475  * Return true if the default credential cache support switch
476  *
477  * @ingroup krb5_ccache
478  */
479
480 krb5_boolean KRB5_LIB_FUNCTION
481 krb5_cc_support_switch(krb5_context context, const char *type)
482 {
483     const krb5_cc_ops *ops;
484
485     ops = krb5_cc_get_prefix_ops(context, type);
486     if (ops && ops->set_default)
487         return 1;
488     return FALSE;
489 }
490
491 /**
492  * Set the default cc name for `context' to `name'.
493  *
494  * @ingroup krb5_ccache
495  */
496
497 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
498 krb5_cc_set_default_name(krb5_context context, const char *name)
499 {
500     krb5_error_code ret = 0;
501     char *p = NULL, *exp_p = NULL;
502
503     if (name == NULL) {
504         const char *e = NULL;
505
506         if(!issuid()) {
507             e = getenv("KRB5CCNAME");
508             if (e) {
509                 p = strdup(e);
510                 if (context->default_cc_name_env)
511                     free(context->default_cc_name_env);
512                 context->default_cc_name_env = strdup(e);
513             }
514         }
515         if (e == NULL) {
516             e = krb5_config_get_string(context, NULL, "libdefaults",
517                                        "default_cc_name", NULL);
518             if (e) {
519                 ret = _krb5_expand_default_cc_name(context, e, &p);
520                 if (ret)
521                     return ret;
522             }
523             if (e == NULL) {
524                 const krb5_cc_ops *ops = KRB5_DEFAULT_CCTYPE;
525                 e = krb5_config_get_string(context, NULL, "libdefaults",
526                                            "default_cc_type", NULL);
527                 if (e) {
528                     ops = krb5_cc_get_prefix_ops(context, e);
529                     if (ops == NULL) {
530                         krb5_set_error_message(context,
531                                                KRB5_CC_UNKNOWN_TYPE,
532                                                "Credential cache type %s "
533                                               "is unknown", e);
534                         return KRB5_CC_UNKNOWN_TYPE;
535                     }
536                 }
537                 ret = (*ops->get_default_name)(context, &p);
538                 if (ret)
539                     return ret;
540             }
541         }
542         context->default_cc_name_set = 0;
543     } else {
544         p = strdup(name);
545         context->default_cc_name_set = 1;
546     }
547
548     if (p == NULL) {
549         krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
550         return ENOMEM;
551     }
552
553     ret = _krb5_expand_path_tokens(context, p, &exp_p);
554     free(p);
555     if (ret)
556         return ret;
557
558     if (context->default_cc_name)
559         free(context->default_cc_name);
560
561     context->default_cc_name = exp_p;
562
563     return 0;
564 }
565
566 /**
567  * Return a pointer to a context static string containing the default
568  * ccache name.
569  *
570  * @return String to the default credential cache name.
571  *
572  * @ingroup krb5_ccache
573  */
574
575
576 KRB5_LIB_FUNCTION const char* KRB5_LIB_CALL
577 krb5_cc_default_name(krb5_context context)
578 {
579     if (context->default_cc_name == NULL || environment_changed(context))
580         krb5_cc_set_default_name(context, NULL);
581
582     return context->default_cc_name;
583 }
584
585 /**
586  * Open the default ccache in `id'.
587  *
588  * @return Return an error code or 0, see krb5_get_error_message().
589  *
590  * @ingroup krb5_ccache
591  */
592
593
594 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
595 krb5_cc_default(krb5_context context,
596                 krb5_ccache *id)
597 {
598     const char *p = krb5_cc_default_name(context);
599
600     if (p == NULL) {
601         krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
602         return ENOMEM;
603     }
604     return krb5_cc_resolve(context, p, id);
605 }
606
607 /**
608  * Create a new ccache in `id' for `primary_principal'.
609  *
610  * @return Return an error code or 0, see krb5_get_error_message().
611  *
612  * @ingroup krb5_ccache
613  */
614
615
616 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
617 krb5_cc_initialize(krb5_context context,
618                    krb5_ccache id,
619                    krb5_principal primary_principal)
620 {
621     return (*id->ops->init)(context, id, primary_principal);
622 }
623
624
625 /**
626  * Remove the ccache `id'.
627  *
628  * @return Return an error code or 0, see krb5_get_error_message().
629  *
630  * @ingroup krb5_ccache
631  */
632
633
634 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
635 krb5_cc_destroy(krb5_context context,
636                 krb5_ccache id)
637 {
638     krb5_error_code ret;
639
640     ret = (*id->ops->destroy)(context, id);
641     krb5_cc_close (context, id);
642     return ret;
643 }
644
645 /**
646  * Stop using the ccache `id' and free the related resources.
647  *
648  * @return Return an error code or 0, see krb5_get_error_message().
649  *
650  * @ingroup krb5_ccache
651  */
652
653
654 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
655 krb5_cc_close(krb5_context context,
656               krb5_ccache id)
657 {
658     krb5_error_code ret;
659     ret = (*id->ops->close)(context, id);
660     free(id);
661     return ret;
662 }
663
664 /**
665  * Store `creds' in the ccache `id'.
666  *
667  * @return Return an error code or 0, see krb5_get_error_message().
668  *
669  * @ingroup krb5_ccache
670  */
671
672
673 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
674 krb5_cc_store_cred(krb5_context context,
675                    krb5_ccache id,
676                    krb5_creds *creds)
677 {
678     return (*id->ops->store)(context, id, creds);
679 }
680
681 /**
682  * Retrieve the credential identified by `mcreds' (and `whichfields')
683  * from `id' in `creds'. 'creds' must be free by the caller using
684  * krb5_free_cred_contents.
685  *
686  * @param context A Kerberos 5 context
687  * @param id a Kerberos 5 credential cache
688  * @param whichfields what fields to use for matching credentials, same
689  *        flags as whichfields in krb5_compare_creds()
690  * @param mcreds template credential to use for comparing
691  * @param creds returned credential, free with krb5_free_cred_contents()
692  *
693  * @return Return an error code or 0, see krb5_get_error_message().
694  *
695  * @ingroup krb5_ccache
696  */
697
698
699 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
700 krb5_cc_retrieve_cred(krb5_context context,
701                       krb5_ccache id,
702                       krb5_flags whichfields,
703                       const krb5_creds *mcreds,
704                       krb5_creds *creds)
705 {
706     krb5_error_code ret;
707     krb5_cc_cursor cursor;
708
709     if (id->ops->retrieve != NULL) {
710         return (*id->ops->retrieve)(context, id, whichfields,
711                                     mcreds, creds);
712     }
713
714     ret = krb5_cc_start_seq_get(context, id, &cursor);
715     if (ret)
716         return ret;
717     while((ret = krb5_cc_next_cred(context, id, &cursor, creds)) == 0){
718         if(krb5_compare_creds(context, whichfields, mcreds, creds)){
719             ret = 0;
720             break;
721         }
722         krb5_free_cred_contents (context, creds);
723     }
724     krb5_cc_end_seq_get(context, id, &cursor);
725     return ret;
726 }
727
728 /**
729  * Return the principal of `id' in `principal'.
730  *
731  * @return Return an error code or 0, see krb5_get_error_message().
732  *
733  * @ingroup krb5_ccache
734  */
735
736
737 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
738 krb5_cc_get_principal(krb5_context context,
739                       krb5_ccache id,
740                       krb5_principal *principal)
741 {
742     return (*id->ops->get_princ)(context, id, principal);
743 }
744
745 /**
746  * Start iterating over `id', `cursor' is initialized to the
747  * beginning.  Caller must free the cursor with krb5_cc_end_seq_get().
748  *
749  * @return Return an error code or 0, see krb5_get_error_message().
750  *
751  * @ingroup krb5_ccache
752  */
753
754
755 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
756 krb5_cc_start_seq_get (krb5_context context,
757                        const krb5_ccache id,
758                        krb5_cc_cursor *cursor)
759 {
760     return (*id->ops->get_first)(context, id, cursor);
761 }
762
763 /**
764  * Retrieve the next cred pointed to by (`id', `cursor') in `creds'
765  * and advance `cursor'.
766  *
767  * @return Return an error code or 0, see krb5_get_error_message().
768  *
769  * @ingroup krb5_ccache
770  */
771
772
773 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
774 krb5_cc_next_cred (krb5_context context,
775                    const krb5_ccache id,
776                    krb5_cc_cursor *cursor,
777                    krb5_creds *creds)
778 {
779     return (*id->ops->get_next)(context, id, cursor, creds);
780 }
781
782 /**
783  * Destroy the cursor `cursor'.
784  *
785  * @ingroup krb5_ccache
786  */
787
788
789 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
790 krb5_cc_end_seq_get (krb5_context context,
791                      const krb5_ccache id,
792                      krb5_cc_cursor *cursor)
793 {
794     return (*id->ops->end_get)(context, id, cursor);
795 }
796
797 /**
798  * Remove the credential identified by `cred', `which' from `id'.
799  *
800  * @ingroup krb5_ccache
801  */
802
803
804 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
805 krb5_cc_remove_cred(krb5_context context,
806                     krb5_ccache id,
807                     krb5_flags which,
808                     krb5_creds *cred)
809 {
810     if(id->ops->remove_cred == NULL) {
811         krb5_set_error_message(context,
812                                EACCES,
813                                "ccache %s does not support remove_cred",
814                                id->ops->prefix);
815         return EACCES; /* XXX */
816     }
817     return (*id->ops->remove_cred)(context, id, which, cred);
818 }
819
820 /**
821  * Set the flags of `id' to `flags'.
822  *
823  * @ingroup krb5_ccache
824  */
825
826
827 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
828 krb5_cc_set_flags(krb5_context context,
829                   krb5_ccache id,
830                   krb5_flags flags)
831 {
832     return (*id->ops->set_flags)(context, id, flags);
833 }
834                 
835 /**
836  * Get the flags of `id', store them in `flags'.
837  *
838  * @ingroup krb5_ccache
839  */
840
841 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
842 krb5_cc_get_flags(krb5_context context,
843                   krb5_ccache id,
844                   krb5_flags *flags)
845 {
846     *flags = 0;
847     return 0;
848 }
849
850 /**
851  * Copy the contents of `from' to `to' if the given match function
852  * return true.
853  *
854  * @param context A Kerberos 5 context.
855  * @param from the cache to copy data from.
856  * @param to the cache to copy data to.
857  * @param match a match function that should return TRUE if cred argument should be copied, if NULL, all credentials are copied.
858  * @param matchctx context passed to match function.
859  * @param matched set to true if there was a credential that matched, may be NULL.
860  *
861  * @return Return an error code or 0, see krb5_get_error_message().
862  *
863  * @ingroup krb5_ccache
864  */
865
866 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
867 krb5_cc_copy_match_f(krb5_context context,
868                      const krb5_ccache from,
869                      krb5_ccache to,
870                      krb5_boolean (*match)(krb5_context, void *, const krb5_creds *),
871                      void *matchctx,
872                      unsigned int *matched)
873 {
874     krb5_error_code ret;
875     krb5_cc_cursor cursor;
876     krb5_creds cred;
877     krb5_principal princ;
878
879     if (matched)
880         *matched = 0;
881
882     ret = krb5_cc_get_principal(context, from, &princ);
883     if (ret)
884         return ret;
885     ret = krb5_cc_initialize(context, to, princ);
886     if (ret) {
887         krb5_free_principal(context, princ);
888         return ret;
889     }
890     ret = krb5_cc_start_seq_get(context, from, &cursor);
891     if (ret) {
892         krb5_free_principal(context, princ);
893         return ret;
894     }
895
896     while ((ret = krb5_cc_next_cred(context, from, &cursor, &cred)) == 0) {
897            if (match == NULL || (*match)(context, matchctx, &cred) == 0) {
898                if (matched)
899                    (*matched)++;
900                ret = krb5_cc_store_cred(context, to, &cred);
901                if (ret)
902                    break;
903            }
904            krb5_free_cred_contents(context, &cred);
905     }
906     krb5_cc_end_seq_get(context, from, &cursor);
907     krb5_free_principal(context, princ);
908     if (ret == KRB5_CC_END)
909         ret = 0;
910     return ret;
911 }
912
913 /**
914  * Just like krb5_cc_copy_match_f(), but copy everything.
915  *
916  * @ingroup @krb5_ccache
917  */
918
919 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
920 krb5_cc_copy_cache(krb5_context context,
921                    const krb5_ccache from,
922                    krb5_ccache to)
923 {
924     return krb5_cc_copy_match_f(context, from, to, NULL, NULL, NULL);
925 }
926
927 /**
928  * Return the version of `id'.
929  *
930  * @ingroup krb5_ccache
931  */
932
933
934 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
935 krb5_cc_get_version(krb5_context context,
936                     const krb5_ccache id)
937 {
938     if(id->ops->get_version)
939         return (*id->ops->get_version)(context, id);
940     else
941         return 0;
942 }
943
944 /**
945  * Clear `mcreds' so it can be used with krb5_cc_retrieve_cred
946  *
947  * @ingroup krb5_ccache
948  */
949
950
951 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
952 krb5_cc_clear_mcred(krb5_creds *mcred)
953 {
954     memset(mcred, 0, sizeof(*mcred));
955 }
956
957 /**
958  * Get the cc ops that is registered in `context' to handle the
959  * prefix. prefix can be a complete credential cache name or a
960  * prefix, the function will only use part up to the first colon (:)
961  * if there is one. If prefix the argument is NULL, the default ccache
962  * implemtation is returned.
963  *
964  * @return Returns NULL if ops not found.
965  *
966  * @ingroup krb5_ccache
967  */
968
969
970 const krb5_cc_ops *
971 krb5_cc_get_prefix_ops(krb5_context context, const char *prefix)
972 {
973     char *p, *p1;
974     int i;
975
976     if (prefix == NULL)
977         return KRB5_DEFAULT_CCTYPE;
978     if (prefix[0] == '/')
979         return &krb5_fcc_ops;
980
981     p = strdup(prefix);
982     if (p == NULL) {
983         krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
984         return NULL;
985     }
986     p1 = strchr(p, ':');
987     if (p1)
988         *p1 = '\0';
989
990     for(i = 0; i < context->num_cc_ops && context->cc_ops[i]->prefix; i++) {
991         if(strcmp(context->cc_ops[i]->prefix, p) == 0) {
992             free(p);
993             return context->cc_ops[i];
994         }
995     }
996     free(p);
997     return NULL;
998 }
999
1000 struct krb5_cc_cache_cursor_data {
1001     const krb5_cc_ops *ops;
1002     krb5_cc_cursor cursor;
1003 };
1004
1005 /**
1006  * Start iterating over all caches of specified type. See also
1007  * krb5_cccol_cursor_new().
1008
1009  * @param context A Kerberos 5 context
1010  * @param type optional type to iterate over, if NULL, the default cache is used.
1011  * @param cursor cursor should be freed with krb5_cc_cache_end_seq_get().
1012  *
1013  * @return Return an error code or 0, see krb5_get_error_message().
1014  *
1015  * @ingroup krb5_ccache
1016  */
1017
1018
1019 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1020 krb5_cc_cache_get_first (krb5_context context,
1021                          const char *type,
1022                          krb5_cc_cache_cursor *cursor)
1023 {
1024     const krb5_cc_ops *ops;
1025     krb5_error_code ret;
1026
1027     if (type == NULL)
1028         type = krb5_cc_default_name(context);
1029
1030     ops = krb5_cc_get_prefix_ops(context, type);
1031     if (ops == NULL) {
1032         krb5_set_error_message(context, KRB5_CC_UNKNOWN_TYPE,
1033                                "Unknown type \"%s\" when iterating "
1034                                "trying to iterate the credential caches", type);
1035         return KRB5_CC_UNKNOWN_TYPE;
1036     }
1037
1038     if (ops->get_cache_first == NULL) {
1039         krb5_set_error_message(context, KRB5_CC_NOSUPP,
1040                                N_("Credential cache type %s doesn't support "
1041                                  "iterations over caches", "type"),
1042                                ops->prefix);
1043         return KRB5_CC_NOSUPP;
1044     }
1045
1046     *cursor = calloc(1, sizeof(**cursor));
1047     if (*cursor == NULL) {
1048         krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
1049         return ENOMEM;
1050     }
1051
1052     (*cursor)->ops = ops;
1053
1054     ret = ops->get_cache_first(context, &(*cursor)->cursor);
1055     if (ret) {
1056         free(*cursor);
1057         *cursor = NULL;
1058     }
1059     return ret;
1060 }
1061
1062 /**
1063  * Retrieve the next cache pointed to by (`cursor') in `id'
1064  * and advance `cursor'.
1065  *
1066  * @param context A Kerberos 5 context
1067  * @param cursor the iterator cursor, returned by krb5_cc_cache_get_first()
1068  * @param id next ccache
1069  *
1070  * @return Return 0 or an error code. Returns KRB5_CC_END when the end
1071  *         of caches is reached, see krb5_get_error_message().
1072  *
1073  * @ingroup krb5_ccache
1074  */
1075
1076
1077 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1078 krb5_cc_cache_next (krb5_context context,
1079                    krb5_cc_cache_cursor cursor,
1080                    krb5_ccache *id)
1081 {
1082     return cursor->ops->get_cache_next(context, cursor->cursor, id);
1083 }
1084
1085 /**
1086  * Destroy the cursor `cursor'.
1087  *
1088  * @return Return an error code or 0, see krb5_get_error_message().
1089  *
1090  * @ingroup krb5_ccache
1091  */
1092
1093
1094 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1095 krb5_cc_cache_end_seq_get (krb5_context context,
1096                            krb5_cc_cache_cursor cursor)
1097 {
1098     krb5_error_code ret;
1099     ret = cursor->ops->end_cache_get(context, cursor->cursor);
1100     cursor->ops = NULL;
1101     free(cursor);
1102     return ret;
1103 }
1104
1105 /**
1106  * Search for a matching credential cache that have the
1107  * `principal' as the default principal. On success, `id' needs to be
1108  * freed with krb5_cc_close() or krb5_cc_destroy().
1109  *
1110  * @param context A Kerberos 5 context
1111  * @param client The principal to search for
1112  * @param id the returned credential cache
1113  *
1114  * @return On failure, error code is returned and `id' is set to NULL.
1115  *
1116  * @ingroup krb5_ccache
1117  */
1118
1119
1120 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1121 krb5_cc_cache_match (krb5_context context,
1122                      krb5_principal client,
1123                      krb5_ccache *id)
1124 {
1125     krb5_cccol_cursor cursor;
1126     krb5_error_code ret;
1127     krb5_ccache cache = NULL;
1128
1129     *id = NULL;
1130
1131     ret = krb5_cccol_cursor_new (context, &cursor);
1132     if (ret)
1133         return ret;
1134
1135     while (krb5_cccol_cursor_next (context, cursor, &cache) == 0 && cache != NULL) {
1136         krb5_principal principal;
1137
1138         ret = krb5_cc_get_principal(context, cache, &principal);
1139         if (ret == 0) {
1140             krb5_boolean match;
1141         
1142             match = krb5_principal_compare(context, principal, client);
1143             krb5_free_principal(context, principal);
1144             if (match)
1145                 break;
1146         }
1147
1148         krb5_cc_close(context, cache);
1149         cache = NULL;
1150     }
1151
1152     krb5_cccol_cursor_free(context, &cursor);
1153
1154     if (cache == NULL) {
1155         char *str;
1156
1157         krb5_unparse_name(context, client, &str);
1158
1159         krb5_set_error_message(context, KRB5_CC_NOTFOUND,
1160                                N_("Principal %s not found in any "
1161                                   "credential cache", ""),
1162                                str ? str : "<out of memory>");
1163         if (str)
1164             free(str);
1165         return KRB5_CC_NOTFOUND;
1166     }
1167     *id = cache;
1168
1169     return 0;
1170 }
1171
1172 /**
1173  * Move the content from one credential cache to another. The
1174  * operation is an atomic switch.
1175  *
1176  * @param context a Keberos context
1177  * @param from the credential cache to move the content from
1178  * @param to the credential cache to move the content to
1179
1180  * @return On sucess, from is freed. On failure, error code is
1181  * returned and from and to are both still allocated, see krb5_get_error_message().
1182  *
1183  * @ingroup krb5_ccache
1184  */
1185
1186 krb5_error_code
1187 krb5_cc_move(krb5_context context, krb5_ccache from, krb5_ccache to)
1188 {
1189     krb5_error_code ret;
1190
1191     if (strcmp(from->ops->prefix, to->ops->prefix) != 0) {
1192         krb5_set_error_message(context, KRB5_CC_NOSUPP,
1193                                N_("Moving credentials between diffrent "
1194                                  "types not yet supported", ""));
1195         return KRB5_CC_NOSUPP;
1196     }
1197
1198     ret = (*to->ops->move)(context, from, to);
1199     if (ret == 0) {
1200         memset(from, 0, sizeof(*from));
1201         free(from);
1202     }
1203     return ret;
1204 }
1205
1206 #define KRB5_CONF_NAME "krb5_ccache_conf_data"
1207 #define KRB5_REALM_NAME "X-CACHECONF:"
1208
1209 static krb5_error_code
1210 build_conf_principals(krb5_context context, krb5_ccache id,
1211                       krb5_const_principal principal,
1212                       const char *name, krb5_creds *cred)
1213 {
1214     krb5_principal client;
1215     krb5_error_code ret;
1216     char *pname = NULL;
1217
1218     memset(cred, 0, sizeof(*cred));
1219
1220     ret = krb5_cc_get_principal(context, id, &client);
1221     if (ret)
1222         return ret;
1223
1224     if (principal) {
1225         ret = krb5_unparse_name(context, principal, &pname);
1226         if (ret)
1227             return ret;
1228     }
1229
1230     ret = krb5_make_principal(context, &cred->server,
1231                               KRB5_REALM_NAME,
1232                               KRB5_CONF_NAME, name, pname, NULL);
1233     free(pname);
1234     if (ret) {
1235         krb5_free_principal(context, client);
1236         return ret;
1237     }
1238     ret = krb5_copy_principal(context, client, &cred->client);
1239     krb5_free_principal(context, client);
1240     return ret;
1241 }
1242                 
1243 /**
1244  * Return TRUE (non zero) if the principal is a configuration
1245  * principal (generated part of krb5_cc_set_config()). Returns FALSE
1246  * (zero) if not a configuration principal.
1247  *
1248  * @param context a Keberos context
1249  * @param principal principal to check if it a configuration principal
1250  *
1251  * @ingroup krb5_ccache
1252  */
1253
1254 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
1255 krb5_is_config_principal(krb5_context context,
1256                          krb5_const_principal principal)
1257 {
1258     if (strcmp(principal->realm, KRB5_REALM_NAME) != 0)
1259         return FALSE;
1260
1261     if (principal->name.name_string.len == 0 ||
1262         strcmp(principal->name.name_string.val[0], KRB5_CONF_NAME) != 0)
1263         return FALSE;
1264         
1265     return TRUE;
1266 }
1267
1268 /**
1269  * Store some configuration for the credential cache in the cache.
1270  * Existing configuration under the same name is over-written.
1271  *
1272  * @param context a Keberos context
1273  * @param id the credential cache to store the data for
1274  * @param principal configuration for a specific principal, if
1275  * NULL, global for the whole cache.
1276  * @param name name under which the configuraion is stored.
1277  * @param data data to store, if NULL, configure is removed.
1278  *
1279  * @ingroup krb5_ccache
1280  */
1281
1282 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1283 krb5_cc_set_config(krb5_context context, krb5_ccache id,
1284                    krb5_const_principal principal,
1285                    const char *name, krb5_data *data)
1286 {
1287     krb5_error_code ret;
1288     krb5_creds cred;
1289
1290     ret = build_conf_principals(context, id, principal, name, &cred);
1291     if (ret)
1292         goto out;
1293
1294     /* Remove old configuration */
1295     ret = krb5_cc_remove_cred(context, id, 0, &cred);
1296     if (ret && ret != KRB5_CC_NOTFOUND)
1297         goto out;
1298
1299     if (data) {
1300         /* not that anyone care when this expire */
1301         cred.times.authtime = time(NULL);
1302         cred.times.endtime = cred.times.authtime + 3600 * 24 * 30;
1303         
1304         ret = krb5_data_copy(&cred.ticket, data->data, data->length);
1305         if (ret)
1306             goto out;
1307         
1308         ret = krb5_cc_store_cred(context, id, &cred);
1309     }
1310
1311 out:
1312     krb5_free_cred_contents (context, &cred);
1313     return ret;
1314 }
1315
1316 /**
1317  * Get some configuration for the credential cache in the cache.
1318  *
1319  * @param context a Keberos context
1320  * @param id the credential cache to store the data for
1321  * @param principal configuration for a specific principal, if
1322  * NULL, global for the whole cache.
1323  * @param name name under which the configuraion is stored.
1324  * @param data data to fetched, free with krb5_data_free()
1325  *
1326  * @ingroup krb5_ccache
1327  */
1328
1329
1330 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1331 krb5_cc_get_config(krb5_context context, krb5_ccache id,
1332                    krb5_const_principal principal,
1333                    const char *name, krb5_data *data)
1334 {
1335     krb5_creds mcred, cred;
1336     krb5_error_code ret;
1337
1338     memset(&cred, 0, sizeof(cred));
1339     krb5_data_zero(data);
1340
1341     ret = build_conf_principals(context, id, principal, name, &mcred);
1342     if (ret)
1343         goto out;
1344
1345     ret = krb5_cc_retrieve_cred(context, id, 0, &mcred, &cred);
1346     if (ret)
1347         goto out;
1348
1349     ret = krb5_data_copy(data, cred.ticket.data, cred.ticket.length);
1350
1351 out:
1352     krb5_free_cred_contents (context, &cred);
1353     krb5_free_cred_contents (context, &mcred);
1354     return ret;
1355 }
1356
1357 /*
1358  *
1359  */
1360
1361 struct krb5_cccol_cursor_data {
1362     int idx;
1363     krb5_cc_cache_cursor cursor;
1364 };
1365
1366 /**
1367  * Get a new cache interation cursor that will interate over all
1368  * credentials caches independent of type.
1369  *
1370  * @param context a Keberos context
1371  * @param cursor passed into krb5_cccol_cursor_next() and free with krb5_cccol_cursor_free().
1372  *
1373  * @return Returns 0 or and error code, see krb5_get_error_message().
1374  *
1375  * @ingroup krb5_ccache
1376  */
1377
1378 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1379 krb5_cccol_cursor_new(krb5_context context, krb5_cccol_cursor *cursor)
1380 {
1381     *cursor = calloc(1, sizeof(**cursor));
1382     if (*cursor == NULL) {
1383         krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
1384         return ENOMEM;
1385     }
1386     (*cursor)->idx = 0;
1387     (*cursor)->cursor = NULL;
1388
1389     return 0;
1390 }
1391
1392 /**
1393  * Get next credential cache from the iteration. 
1394  *
1395  * @param context A Kerberos 5 context
1396  * @param cursor the iteration cursor
1397  * @param cache the returned cursor, pointer is set to NULL on failure
1398  *        and a cache on success. The returned cache needs to be freed
1399  *        with krb5_cc_close() or destroyed with krb5_cc_destroy().
1400  *        MIT Kerberos behavies slightly diffrent and sets cache to NULL
1401  *        when all caches are iterated over and return 0.
1402  *
1403  * @return Return 0 or and error, KRB5_CC_END is returned at the end
1404  *        of iteration. See krb5_get_error_message().
1405  *
1406  * @ingroup krb5_ccache
1407  */
1408
1409
1410 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1411 krb5_cccol_cursor_next(krb5_context context, krb5_cccol_cursor cursor,
1412                        krb5_ccache *cache)
1413 {
1414     krb5_error_code ret;
1415     
1416     *cache = NULL;
1417
1418     while (cursor->idx < context->num_cc_ops) {
1419
1420         if (cursor->cursor == NULL) {
1421             ret = krb5_cc_cache_get_first (context, 
1422                                            context->cc_ops[cursor->idx]->prefix,
1423                                            &cursor->cursor);
1424             if (ret) {
1425                 cursor->idx++;
1426                 continue;
1427             }
1428         }
1429         ret = krb5_cc_cache_next(context, cursor->cursor, cache);
1430         if (ret == 0)
1431             break;
1432
1433         krb5_cc_cache_end_seq_get(context, cursor->cursor);
1434         cursor->cursor = NULL;
1435         if (ret != KRB5_CC_END)
1436             break;
1437
1438         cursor->idx++;
1439     }
1440     if (cursor->idx >= context->num_cc_ops) {
1441         krb5_set_error_message(context, KRB5_CC_END,
1442                                N_("Reached end of credential caches", ""));
1443         return KRB5_CC_END;
1444     }
1445
1446     return 0;
1447 }
1448
1449 /**
1450  * End an iteration and free all resources, can be done before end is reached.
1451  *
1452  * @param context A Kerberos 5 context
1453  * @param cursor the iteration cursor to be freed.
1454  *
1455  * @return Return 0 or and error, KRB5_CC_END is returned at the end
1456  *        of iteration. See krb5_get_error_message().
1457  *
1458  * @ingroup krb5_ccache
1459  */
1460
1461 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1462 krb5_cccol_cursor_free(krb5_context context, krb5_cccol_cursor *cursor)
1463 {
1464     krb5_cccol_cursor c = *cursor;
1465
1466     *cursor = NULL;
1467     if (c) {
1468         if (c->cursor)
1469             krb5_cc_cache_end_seq_get(context, c->cursor);
1470         free(c);
1471     }
1472     return 0;
1473 }
1474
1475 /**
1476  * Return the last time the credential cache was modified.
1477  *
1478  * @param context A Kerberos 5 context
1479  * @param id The credential cache to probe
1480  * @param mtime the last modification time, set to 0 on error.
1481
1482  * @return Return 0 or and error. See krb5_get_error_message().
1483  *
1484  * @ingroup krb5_ccache
1485  */
1486
1487
1488 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1489 krb5_cc_last_change_time(krb5_context context,
1490                          krb5_ccache id, 
1491                          krb5_timestamp *mtime)
1492 {
1493     *mtime = 0;
1494     return (*id->ops->lastchange)(context, id, mtime);
1495 }
1496
1497 /**
1498  * Return the last modfication time for a cache collection. The query
1499  * can be limited to a specific cache type. If the function return 0
1500  * and mtime is 0, there was no credentials in the caches.
1501  *
1502  * @param context A Kerberos 5 context
1503  * @param type The credential cache to probe, if NULL, all type are traversed.
1504  * @param mtime the last modification time, set to 0 on error.
1505
1506  * @return Return 0 or and error. See krb5_get_error_message().
1507  *
1508  * @ingroup krb5_ccache
1509  */
1510
1511 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1512 krb5_cccol_last_change_time(krb5_context context,
1513                             const char *type,
1514                             krb5_timestamp *mtime)
1515 {
1516     krb5_cccol_cursor cursor;
1517     krb5_error_code ret;
1518     krb5_ccache id;
1519     krb5_timestamp t = 0;
1520
1521     *mtime = 0;
1522
1523     ret = krb5_cccol_cursor_new (context, &cursor);
1524     if (ret)
1525         return ret;
1526
1527     while (krb5_cccol_cursor_next(context, cursor, &id) == 0 && id != NULL) {
1528
1529         if (type && strcmp(krb5_cc_get_type(context, id), type) != 0)
1530             continue;
1531
1532         ret = krb5_cc_last_change_time(context, id, &t);
1533         krb5_cc_close(context, id);
1534         if (ret)
1535             continue;
1536         if (t > *mtime)
1537             *mtime = t;
1538     }
1539
1540     krb5_cccol_cursor_free(context, &cursor);
1541
1542     return 0;
1543 }
1544 /**
1545  * Return a friendly name on credential cache. Free the result with krb5_xfree().
1546  *
1547  * @return Return an error code or 0, see krb5_get_error_message().
1548  *
1549  * @ingroup krb5_ccache
1550  */
1551
1552 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1553 krb5_cc_get_friendly_name(krb5_context context,
1554                           krb5_ccache id,
1555                           char **name)
1556 {
1557     krb5_error_code ret;
1558     krb5_data data;
1559
1560     ret = krb5_cc_get_config(context, id, NULL, "FriendlyName", &data);
1561     if (ret) {
1562         krb5_principal principal;
1563         ret = krb5_cc_get_principal(context, id, &principal);
1564         if (ret)
1565             return ret;
1566         ret = krb5_unparse_name(context, principal, name);
1567         krb5_free_principal(context, principal);
1568     } else {
1569         ret = asprintf(name, "%.*s", (int)data.length, (char *)data.data);
1570         krb5_data_free(&data);
1571         if (ret <= 0) {
1572             ret = ENOMEM;
1573             krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
1574         } else
1575             ret = 0;
1576     }
1577
1578     return ret;
1579 }
1580
1581 /**
1582  * Set the friendly name on credential cache.
1583  *
1584  * @return Return an error code or 0, see krb5_get_error_message().
1585  *
1586  * @ingroup krb5_ccache
1587  */
1588
1589 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1590 krb5_cc_set_friendly_name(krb5_context context,
1591                           krb5_ccache id,
1592                           const char *name)
1593 {
1594     krb5_data data;
1595
1596     data.data = rk_UNCONST(name);
1597     data.length = strlen(name);
1598
1599     return krb5_cc_set_config(context, id, NULL, "FriendlyName", &data);
1600 }
1601
1602 /**
1603  * Get the lifetime of the initial ticket in the cache
1604  *
1605  * Get the lifetime of the initial ticket in the cache, if the initial
1606  * ticket was not found, the error code KRB5_CC_END is returned.
1607  *
1608  * @param context A Kerberos 5 context.
1609  * @param id a credential cache
1610  * @param t the relative lifetime of the initial ticket
1611  *
1612  * @return Return an error code or 0, see krb5_get_error_message().
1613  *
1614  * @ingroup krb5_ccache
1615  */
1616
1617 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1618 krb5_cc_get_lifetime(krb5_context context, krb5_ccache id, time_t *t)
1619 {
1620     krb5_cc_cursor cursor;
1621     krb5_error_code ret;
1622     krb5_creds cred;
1623     time_t now;
1624
1625     *t = 0;
1626     now = time(NULL);
1627     
1628     ret = krb5_cc_start_seq_get(context, id, &cursor);
1629     if (ret)
1630         return ret;
1631
1632     while ((ret = krb5_cc_next_cred(context, id, &cursor, &cred)) == 0) {
1633         if (cred.flags.b.initial) {
1634             if (now < cred.times.endtime)
1635                 *t = cred.times.endtime - now;
1636             krb5_free_cred_contents(context, &cred);
1637             break;
1638         }
1639         krb5_free_cred_contents(context, &cred);
1640     }
1641     
1642     krb5_cc_end_seq_get(context, id, &cursor);
1643
1644     return ret;
1645 }
1646
1647 /**
1648  * Set the time offset betwen the client and the KDC
1649  *
1650  * If the backend doesn't support KDC offset, use the context global setting.
1651  *
1652  * @param context A Kerberos 5 context.
1653  * @param id a credential cache
1654  * @param offset the offset in seconds
1655  *
1656  * @return Return an error code or 0, see krb5_get_error_message().
1657  *
1658  * @ingroup krb5_ccache
1659  */
1660
1661 krb5_error_code
1662 krb5_cc_set_kdc_offset(krb5_context context, krb5_ccache id, krb5_deltat offset)
1663 {
1664     if (id->ops->set_kdc_offset == NULL) {
1665         context->kdc_sec_offset = offset;
1666         context->kdc_usec_offset = 0;
1667         return 0;
1668     }
1669     return (*id->ops->set_kdc_offset)(context, id, offset);
1670 }
1671
1672 /**
1673  * Get the time offset betwen the client and the KDC
1674  *
1675  * If the backend doesn't support KDC offset, use the context global setting.
1676  *
1677  * @param context A Kerberos 5 context.
1678  * @param id a credential cache
1679  * @param offset the offset in seconds
1680  *
1681  * @return Return an error code or 0, see krb5_get_error_message().
1682  *
1683  * @ingroup krb5_ccache
1684  */
1685
1686 krb5_error_code
1687 krb5_cc_get_kdc_offset(krb5_context context, krb5_ccache id, krb5_deltat *offset)
1688 {
1689     if (id->ops->get_kdc_offset == NULL) {
1690         *offset = context->kdc_sec_offset;
1691         return 0;
1692     }
1693     return (*id->ops->get_kdc_offset)(context, id, offset);
1694 }