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