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