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