1 /*
2 * Copyright (c) 1997 - 2005 Kungliga Tekniska H�gskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #include "krb5_locl.h"
35
36 RCSID("$Id: cache.c,v 1.82 2006/09/12 17:35:33 lha Exp $");
37
38 /*
39 * Add a new ccache type with operations `ops', overwriting any
40 * existing one if `override'.
41 * Return an error code or 0.
42 */
43
44 krb5_error_code KRB5_LIB_FUNCTION
krb5_cc_register(krb5_context context,const krb5_cc_ops * ops,krb5_boolean override)45 krb5_cc_register(krb5_context context,
46 const krb5_cc_ops *ops,
47 krb5_boolean override)
48 {
49 int i;
50
51 for(i = 0; i < context->num_cc_ops && context->cc_ops[i].prefix; i++) {
52 if(strcmp(context->cc_ops[i].prefix, ops->prefix) == 0) {
53 if(!override) {
54 krb5_set_error_string(context,
55 "ccache type %s already exists",
56 ops->prefix);
57 return KRB5_CC_TYPE_EXISTS;
58 }
59 break;
60 }
61 }
62 if(i == context->num_cc_ops) {
63 krb5_cc_ops *o = realloc(context->cc_ops,
64 (context->num_cc_ops + 1) *
65 sizeof(*context->cc_ops));
66 if(o == NULL) {
67 krb5_set_error_string(context, "malloc: out of memory");
68 return KRB5_CC_NOMEM;
69 }
70 context->num_cc_ops++;
71 context->cc_ops = o;
72 memset(context->cc_ops + i, 0,
73 (context->num_cc_ops - i) * sizeof(*context->cc_ops));
74 }
75 memcpy(&context->cc_ops[i], ops, sizeof(context->cc_ops[i]));
76 return 0;
77 }
78
79 /*
80 * Allocate the memory for a `id' and the that function table to
81 * `ops'. Returns 0 or and error code.
82 */
83
84 krb5_error_code
_krb5_cc_allocate(krb5_context context,const krb5_cc_ops * ops,krb5_ccache * id)85 _krb5_cc_allocate(krb5_context context,
86 const krb5_cc_ops *ops,
87 krb5_ccache *id)
88 {
89 krb5_ccache p;
90
91 p = malloc (sizeof(*p));
92 if(p == NULL) {
93 krb5_set_error_string(context, "malloc: out of memory");
94 return KRB5_CC_NOMEM;
95 }
96 p->ops = ops;
97 *id = p;
98
99 return 0;
100 }
101
102 /*
103 * Allocate memory for a new ccache in `id' with operations `ops'
104 * and name `residual'.
105 * Return 0 or an error code.
106 */
107
108 static krb5_error_code
allocate_ccache(krb5_context context,const krb5_cc_ops * ops,const char * residual,krb5_ccache * id)109 allocate_ccache (krb5_context context,
110 const krb5_cc_ops *ops,
111 const char *residual,
112 krb5_ccache *id)
113 {
114 krb5_error_code ret;
115
116 ret = _krb5_cc_allocate(context, ops, id);
117 if (ret)
118 return ret;
119 ret = (*id)->ops->resolve(context, id, residual);
120 if(ret)
121 free(*id);
122 return ret;
123 }
124
125 /*
126 * Find and allocate a ccache in `id' from the specification in `residual'.
127 * If the ccache name doesn't contain any colon, interpret it as a file name.
128 * Return 0 or an error code.
129 */
130
131 krb5_error_code KRB5_LIB_FUNCTION
krb5_cc_resolve(krb5_context context,const char * name,krb5_ccache * id)132 krb5_cc_resolve(krb5_context context,
133 const char *name,
134 krb5_ccache *id)
135 {
136 int i;
137
138 for(i = 0; i < context->num_cc_ops && context->cc_ops[i].prefix; i++) {
139 size_t prefix_len = strlen(context->cc_ops[i].prefix);
140
141 if(strncmp(context->cc_ops[i].prefix, name, prefix_len) == 0
142 && name[prefix_len] == ':') {
143 return allocate_ccache (context, &context->cc_ops[i],
144 name + prefix_len + 1,
145 id);
146 }
147 }
148 if (strchr (name, ':') == NULL)
149 return allocate_ccache (context, &krb5_fcc_ops, name, id);
150 else {
151 krb5_set_error_string(context, "unknown ccache type %s", name);
152 return KRB5_CC_UNKNOWN_TYPE;
153 }
154 }
155
156 /*
157 * Generate a new ccache of type `ops' in `id'.
158 * Return 0 or an error code.
159 */
160
161 krb5_error_code KRB5_LIB_FUNCTION
krb5_cc_gen_new(krb5_context context,const krb5_cc_ops * ops,krb5_ccache * id)162 krb5_cc_gen_new(krb5_context context,
163 const krb5_cc_ops *ops,
164 krb5_ccache *id)
165 {
166 krb5_error_code ret;
167
168 ret = _krb5_cc_allocate(context, ops, id);
169 if (ret)
170 return ret;
171 return (*id)->ops->gen_new(context, id);
172 }
173
174 /*
175 * Generates a new unique ccache of `type` in `id'. If `type' is NULL,
176 * the library chooses the default credential cache type. The supplied
177 * `hint' (that can be NULL) is a string that the credential cache
178 * type can use to base the name of the credential on, this is to make
179 * its easier for the user to differentiate the credentials.
180 *
181 * Returns 0 or an error code.
182 */
183
184 krb5_error_code KRB5_LIB_FUNCTION
krb5_cc_new_unique(krb5_context context,const char * type,const char * hint,krb5_ccache * id)185 krb5_cc_new_unique(krb5_context context, const char *type,
186 const char *hint, krb5_ccache *id)
187 {
188 const krb5_cc_ops *ops;
189
190 if (type == NULL)
191 type = KRB5_DEFAULT_CCNAME;
192
193 ops = krb5_cc_get_prefix_ops(context, type);
194 if (ops == NULL) {
195 krb5_set_error_string(context, "Credential cache type %s is unknown",
196 type);
197 return KRB5_CC_UNKNOWN_TYPE;
198 }
199
200 return krb5_cc_gen_new(context, ops, id);
201 }
202
203 /*
204 * Return the name of the ccache `id'
205 */
206
207 const char* KRB5_LIB_FUNCTION
krb5_cc_get_name(krb5_context context,krb5_ccache id)208 krb5_cc_get_name(krb5_context context,
209 krb5_ccache id)
210 {
211 return id->ops->get_name(context, id);
212 }
213
214 /*
215 * Return the type of the ccache `id'.
216 */
217
218 const char* KRB5_LIB_FUNCTION
krb5_cc_get_type(krb5_context context,krb5_ccache id)219 krb5_cc_get_type(krb5_context context,
220 krb5_ccache id)
221 {
222 return id->ops->prefix;
223 }
224
225 /*
226 * Return the complete resolvable name the ccache `id' in `str�.
227 * `str` should be freed with free(3).
228 * Returns 0 or an error (and then *str is set to NULL).
229 */
230
231 krb5_error_code KRB5_LIB_FUNCTION
krb5_cc_get_full_name(krb5_context context,krb5_ccache id,char ** str)232 krb5_cc_get_full_name(krb5_context context,
233 krb5_ccache id,
234 char **str)
235 {
236 const char *type, *name;
237
238 *str = NULL;
239
240 type = krb5_cc_get_type(context, id);
241 if (type == NULL) {
242 krb5_set_error_string(context, "cache have no name of type");
243 return KRB5_CC_UNKNOWN_TYPE;
244 }
245
246 name = krb5_cc_get_name(context, id);
247 if (name == NULL) {
248 krb5_set_error_string(context, "cache of type %s have no name", type);
249 return KRB5_CC_BADNAME;
250 }
251
252 if (asprintf(str, "%s:%s", type, name) == -1) {
253 krb5_set_error_string(context, "malloc - out of memory");
254 *str = NULL;
255 return ENOMEM;
256 }
257 return 0;
258 }
259
260 /*
261 * Return krb5_cc_ops of a the ccache `id'.
262 */
263
264 const krb5_cc_ops *
krb5_cc_get_ops(krb5_context context,krb5_ccache id)265 krb5_cc_get_ops(krb5_context context, krb5_ccache id)
266 {
267 return id->ops;
268 }
269
270 /*
271 * Expand variables in `str' into `res'
272 */
273
274 krb5_error_code
_krb5_expand_default_cc_name(krb5_context context,const char * str,char ** res)275 _krb5_expand_default_cc_name(krb5_context context, const char *str, char **res)
276 {
277 size_t tlen, len = 0;
278 char *tmp, *tmp2, *append;
279
280 *res = NULL;
281
282 while (str && *str) {
283 tmp = strstr(str, "%{");
284 if (tmp && tmp != str) {
285 append = malloc((tmp - str) + 1);
286 if (append) {
287 memcpy(append, str, tmp - str);
288 append[tmp - str] = '\0';
289 }
290 str = tmp;
291 } else if (tmp) {
292 tmp2 = strchr(tmp, '}');
293 if (tmp2 == NULL) {
294 free(*res);
295 *res = NULL;
296 krb5_set_error_string(context, "variable missing }");
297 return KRB5_CONFIG_BADFORMAT;
298 }
299 if (strncasecmp(tmp, "%{uid}", 6) == 0)
300 asprintf(&append, "%u", (unsigned)getuid());
301 else if (strncasecmp(tmp, "%{null}", 7) == 0)
302 append = strdup("");
303 else {
304 free(*res);
305 *res = NULL;
306 krb5_set_error_string(context,
307 "expand default cache unknown "
308 "variable \"%.*s\"",
309 (int)(tmp2 - tmp) - 2, tmp + 2);
310 return KRB5_CONFIG_BADFORMAT;
311 }
312 str = tmp2 + 1;
313 } else {
314 append = strdup(str);
315 str = NULL;
316 }
317 if (append == NULL) {
318 free(*res);
319 *res = NULL;
320 krb5_set_error_string(context, "malloc - out of memory");
321 return ENOMEM;
322 }
323
324 tlen = strlen(append);
325 tmp = realloc(*res, len + tlen + 1);
326 if (tmp == NULL) {
327 free(append);
328 free(*res);
329 *res = NULL;
330 krb5_set_error_string(context, "malloc - out of memory");
331 return ENOMEM;
332 }
333 *res = tmp;
334 memcpy(*res + len, append, tlen + 1);
335 len = len + tlen;
336 free(append);
337 }
338 return 0;
339 }
340
341 /*
342 * Set the default cc name for `context' to `name'.
343 */
344
345 krb5_error_code KRB5_LIB_FUNCTION
krb5_cc_set_default_name(krb5_context context,const char * name)346 krb5_cc_set_default_name(krb5_context context, const char *name)
347 {
348 krb5_error_code ret = 0;
349 char *p;
350
351 if (name == NULL) {
352 const char *e = NULL;
353
354 if(!issuid()) {
355 e = getenv("KRB5CCNAME");
356 if (e)
357 p = strdup(e);
358 }
359 if (e == NULL) {
360 e = krb5_config_get_string(context, NULL, "libdefaults",
361 "default_cc_name", NULL);
362 if (e == NULL)
363 e = KRB5_DEFAULT_CCNAME;
364 ret = _krb5_expand_default_cc_name(context, e, &p);
365 if (ret)
366 return ret;
367 }
368 } else
369 p = strdup(name);
370
371 if (p == NULL) {
372 krb5_set_error_string(context, "malloc - out of memory");
373 return ENOMEM;
374 }
375
376 if (context->default_cc_name)
377 free(context->default_cc_name);
378
379 context->default_cc_name = p;
380
381 return ret;
382 }
383
384 /*
385 * Return a pointer to a context static string containing the default
386 * ccache name.
387 */
388
389 const char* KRB5_LIB_FUNCTION
krb5_cc_default_name(krb5_context context)390 krb5_cc_default_name(krb5_context context)
391 {
392 if (context->default_cc_name == NULL)
393 krb5_cc_set_default_name(context, NULL);
394
395 return context->default_cc_name;
396 }
397
398 /*
399 * Open the default ccache in `id'.
400 * Return 0 or an error code.
401 */
402
403 krb5_error_code KRB5_LIB_FUNCTION
krb5_cc_default(krb5_context context,krb5_ccache * id)404 krb5_cc_default(krb5_context context,
405 krb5_ccache *id)
406 {
407 const char *p = krb5_cc_default_name(context);
408
409 if (p == NULL) {
410 krb5_set_error_string(context, "malloc - out of memory");
411 return ENOMEM;
412 }
413 return krb5_cc_resolve(context, p, id);
414 }
415
416 /*
417 * Create a new ccache in `id' for `primary_principal'.
418 * Return 0 or an error code.
419 */
420
421 krb5_error_code KRB5_LIB_FUNCTION
krb5_cc_initialize(krb5_context context,krb5_ccache id,krb5_principal primary_principal)422 krb5_cc_initialize(krb5_context context,
423 krb5_ccache id,
424 krb5_principal primary_principal)
425 {
426 return (*id->ops->init)(context, id, primary_principal);
427 }
428
429
430 /*
431 * Remove the ccache `id'.
432 * Return 0 or an error code.
433 */
434
435 krb5_error_code KRB5_LIB_FUNCTION
krb5_cc_destroy(krb5_context context,krb5_ccache id)436 krb5_cc_destroy(krb5_context context,
437 krb5_ccache id)
438 {
439 krb5_error_code ret;
440
441 ret = (*id->ops->destroy)(context, id);
442 krb5_cc_close (context, id);
443 return ret;
444 }
445
446 /*
447 * Stop using the ccache `id' and free the related resources.
448 * Return 0 or an error code.
449 */
450
451 krb5_error_code KRB5_LIB_FUNCTION
krb5_cc_close(krb5_context context,krb5_ccache id)452 krb5_cc_close(krb5_context context,
453 krb5_ccache id)
454 {
455 krb5_error_code ret;
456 ret = (*id->ops->close)(context, id);
457 free(id);
458 return ret;
459 }
460
461 /*
462 * Store `creds' in the ccache `id'.
463 * Return 0 or an error code.
464 */
465
466 krb5_error_code KRB5_LIB_FUNCTION
krb5_cc_store_cred(krb5_context context,krb5_ccache id,krb5_creds * creds)467 krb5_cc_store_cred(krb5_context context,
468 krb5_ccache id,
469 krb5_creds *creds)
470 {
471 return (*id->ops->store)(context, id, creds);
472 }
473
474 /*
475 * Retrieve the credential identified by `mcreds' (and `whichfields')
476 * from `id' in `creds'.
477 * Return 0 or an error code.
478 */
479
480 krb5_error_code KRB5_LIB_FUNCTION
krb5_cc_retrieve_cred(krb5_context context,krb5_ccache id,krb5_flags whichfields,const krb5_creds * mcreds,krb5_creds * creds)481 krb5_cc_retrieve_cred(krb5_context context,
482 krb5_ccache id,
483 krb5_flags whichfields,
484 const krb5_creds *mcreds,
485 krb5_creds *creds)
486 {
487 krb5_error_code ret;
488 krb5_cc_cursor cursor;
489
490 if (id->ops->retrieve != NULL) {
491 return (*id->ops->retrieve)(context, id, whichfields,
492 mcreds, creds);
493 }
494
495 krb5_cc_start_seq_get(context, id, &cursor);
496 while((ret = krb5_cc_next_cred(context, id, &cursor, creds)) == 0){
497 if(krb5_compare_creds(context, whichfields, mcreds, creds)){
498 ret = 0;
499 break;
500 }
501 krb5_free_cred_contents (context, creds);
502 }
503 krb5_cc_end_seq_get(context, id, &cursor);
504 return ret;
505 }
506
507 /*
508 * Return the principal of `id' in `principal'.
509 * Return 0 or an error code.
510 */
511
512 krb5_error_code KRB5_LIB_FUNCTION
krb5_cc_get_principal(krb5_context context,krb5_ccache id,krb5_principal * principal)513 krb5_cc_get_principal(krb5_context context,
514 krb5_ccache id,
515 krb5_principal *principal)
516 {
517 return (*id->ops->get_princ)(context, id, principal);
518 }
519
520 /*
521 * Start iterating over `id', `cursor' is initialized to the
522 * beginning.
523 * Return 0 or an error code.
524 */
525
526 krb5_error_code KRB5_LIB_FUNCTION
krb5_cc_start_seq_get(krb5_context context,const krb5_ccache id,krb5_cc_cursor * cursor)527 krb5_cc_start_seq_get (krb5_context context,
528 const krb5_ccache id,
529 krb5_cc_cursor *cursor)
530 {
531 return (*id->ops->get_first)(context, id, cursor);
532 }
533
534 /*
535 * Retrieve the next cred pointed to by (`id', `cursor') in `creds'
536 * and advance `cursor'.
537 * Return 0 or an error code.
538 */
539
540 krb5_error_code KRB5_LIB_FUNCTION
krb5_cc_next_cred(krb5_context context,const krb5_ccache id,krb5_cc_cursor * cursor,krb5_creds * creds)541 krb5_cc_next_cred (krb5_context context,
542 const krb5_ccache id,
543 krb5_cc_cursor *cursor,
544 krb5_creds *creds)
545 {
546 return (*id->ops->get_next)(context, id, cursor, creds);
547 }
548
549 /* like krb5_cc_next_cred, but allow for selective retrieval */
550
551 krb5_error_code KRB5_LIB_FUNCTION
krb5_cc_next_cred_match(krb5_context context,const krb5_ccache id,krb5_cc_cursor * cursor,krb5_creds * creds,krb5_flags whichfields,const krb5_creds * mcreds)552 krb5_cc_next_cred_match(krb5_context context,
553 const krb5_ccache id,
554 krb5_cc_cursor * cursor,
555 krb5_creds * creds,
556 krb5_flags whichfields,
557 const krb5_creds * mcreds)
558 {
559 krb5_error_code ret;
560 while (1) {
561 ret = krb5_cc_next_cred(context, id, cursor, creds);
562 if (ret)
563 return ret;
564 if (mcreds == NULL || krb5_compare_creds(context, whichfields, mcreds, creds))
565 return 0;
566 krb5_free_cred_contents(context, creds);
567 }
568 }
569
570 /*
571 * Destroy the cursor `cursor'.
572 */
573
574 krb5_error_code KRB5_LIB_FUNCTION
krb5_cc_end_seq_get(krb5_context context,const krb5_ccache id,krb5_cc_cursor * cursor)575 krb5_cc_end_seq_get (krb5_context context,
576 const krb5_ccache id,
577 krb5_cc_cursor *cursor)
578 {
579 return (*id->ops->end_get)(context, id, cursor);
580 }
581
582 /*
583 * Remove the credential identified by `cred', `which' from `id'.
584 */
585
586 krb5_error_code KRB5_LIB_FUNCTION
krb5_cc_remove_cred(krb5_context context,krb5_ccache id,krb5_flags which,krb5_creds * cred)587 krb5_cc_remove_cred(krb5_context context,
588 krb5_ccache id,
589 krb5_flags which,
590 krb5_creds *cred)
591 {
592 if(id->ops->remove_cred == NULL) {
593 krb5_set_error_string(context,
594 "ccache %s does not support remove_cred",
595 id->ops->prefix);
596 return EACCES; /* XXX */
597 }
598 return (*id->ops->remove_cred)(context, id, which, cred);
599 }
600
601 /*
602 * Set the flags of `id' to `flags'.
603 */
604
605 krb5_error_code KRB5_LIB_FUNCTION
krb5_cc_set_flags(krb5_context context,krb5_ccache id,krb5_flags flags)606 krb5_cc_set_flags(krb5_context context,
607 krb5_ccache id,
608 krb5_flags flags)
609 {
610 return (*id->ops->set_flags)(context, id, flags);
611 }
612
613 /*
614 * Copy the contents of `from' to `to'.
615 */
616
617 krb5_error_code KRB5_LIB_FUNCTION
krb5_cc_copy_cache_match(krb5_context context,const krb5_ccache from,krb5_ccache to,krb5_flags whichfields,const krb5_creds * mcreds,unsigned int * matched)618 krb5_cc_copy_cache_match(krb5_context context,
619 const krb5_ccache from,
620 krb5_ccache to,
621 krb5_flags whichfields,
622 const krb5_creds * mcreds,
623 unsigned int *matched)
624 {
625 krb5_error_code ret;
626 krb5_cc_cursor cursor;
627 krb5_creds cred;
628 krb5_principal princ;
629
630 ret = krb5_cc_get_principal(context, from, &princ);
631 if (ret)
632 return ret;
633 ret = krb5_cc_initialize(context, to, princ);
634 if (ret) {
635 krb5_free_principal(context, princ);
636 return ret;
637 }
638 ret = krb5_cc_start_seq_get(context, from, &cursor);
639 if (ret) {
640 krb5_free_principal(context, princ);
641 return ret;
642 }
643 if (matched)
644 *matched = 0;
645 while (ret == 0 &&
646 krb5_cc_next_cred_match(context, from, &cursor, &cred,
647 whichfields, mcreds) == 0) {
648 if (matched)
649 (*matched)++;
650 ret = krb5_cc_store_cred(context, to, &cred);
651 krb5_free_cred_contents(context, &cred);
652 }
653 krb5_cc_end_seq_get(context, from, &cursor);
654 krb5_free_principal(context, princ);
655 return ret;
656 }
657
658 krb5_error_code KRB5_LIB_FUNCTION
krb5_cc_copy_cache(krb5_context context,const krb5_ccache from,krb5_ccache to)659 krb5_cc_copy_cache(krb5_context context,
660 const krb5_ccache from,
661 krb5_ccache to)
662 {
663 return krb5_cc_copy_cache_match(context, from, to, 0, NULL, NULL);
664 }
665
666 /*
667 * Return the version of `id'.
668 */
669
670 krb5_error_code KRB5_LIB_FUNCTION
krb5_cc_get_version(krb5_context context,const krb5_ccache id)671 krb5_cc_get_version(krb5_context context,
672 const krb5_ccache id)
673 {
674 if(id->ops->get_version)
675 return (*id->ops->get_version)(context, id);
676 else
677 return 0;
678 }
679
680 /*
681 * Clear `mcreds' so it can be used with krb5_cc_retrieve_cred
682 */
683
684 void KRB5_LIB_FUNCTION
krb5_cc_clear_mcred(krb5_creds * mcred)685 krb5_cc_clear_mcred(krb5_creds *mcred)
686 {
687 memset(mcred, 0, sizeof(*mcred));
688 }
689
690 /*
691 * Get the cc ops that is registered in `context' to handle the
692 * `prefix'. `prefix' can be a complete credential cache name or a
693 * prefix, the function will only use part up to the first colon (:)
694 * if there is one. Returns NULL if ops not found.
695 */
696
697 const krb5_cc_ops *
krb5_cc_get_prefix_ops(krb5_context context,const char * prefix)698 krb5_cc_get_prefix_ops(krb5_context context, const char *prefix)
699 {
700 char *p, *p1;
701 int i;
702
703 if (prefix[0] == '/')
704 return &krb5_fcc_ops;
705
706 p = strdup(prefix);
707 if (p == NULL) {
708 krb5_set_error_string(context, "malloc - out of memory");
709 return NULL;
710 }
711 p1 = strchr(p, ':');
712 if (p1)
713 *p1 = '\0';
714
715 for(i = 0; i < context->num_cc_ops && context->cc_ops[i].prefix; i++) {
716 if(strcmp(context->cc_ops[i].prefix, p) == 0) {
717 free(p);
718 return &context->cc_ops[i];
719 }
720 }
721 free(p);
722 return NULL;
723 }
724
725 struct krb5_cc_cache_cursor_data {
726 const krb5_cc_ops *ops;
727 krb5_cc_cursor cursor;
728 };
729
730 /*
731 * Start iterating over all caches of `type'. If `type' is NULL, the
732 * default type is * used. `cursor' is initialized to the beginning.
733 * Return 0 or an error code.
734 */
735
736 krb5_error_code KRB5_LIB_FUNCTION
krb5_cc_cache_get_first(krb5_context context,const char * type,krb5_cc_cache_cursor * cursor)737 krb5_cc_cache_get_first (krb5_context context,
738 const char *type,
739 krb5_cc_cache_cursor *cursor)
740 {
741 const krb5_cc_ops *ops;
742 krb5_error_code ret;
743
744 if (type == NULL)
745 type = krb5_cc_default_name(context);
746
747 ops = krb5_cc_get_prefix_ops(context, type);
748 if (ops == NULL) {
749 krb5_set_error_string(context, "Unknown type \"%s\" when iterating "
750 "trying to iterate the credential caches", type);
751 return KRB5_CC_UNKNOWN_TYPE;
752 }
753
754 if (ops->get_cache_first == NULL) {
755 krb5_set_error_string(context, "Credential cache type %s doesn't support "
756 "iterations over caches", ops->prefix);
757 return KRB5_CC_NOSUPP;
758 }
759
760 *cursor = calloc(1, sizeof(**cursor));
761 if (*cursor == NULL) {
762 krb5_set_error_string(context, "malloc - out of memory");
763 return ENOMEM;
764 }
765
766 (*cursor)->ops = ops;
767
768 ret = ops->get_cache_first(context, &(*cursor)->cursor);
769 if (ret) {
770 free(*cursor);
771 *cursor = NULL;
772 }
773 return ret;
774 }
775
776 /*
777 * Retrieve the next cache pointed to by (`cursor') in `id'
778 * and advance `cursor'.
779 * Return 0 or an error code.
780 */
781
782 krb5_error_code KRB5_LIB_FUNCTION
krb5_cc_cache_next(krb5_context context,krb5_cc_cache_cursor cursor,krb5_ccache * id)783 krb5_cc_cache_next (krb5_context context,
784 krb5_cc_cache_cursor cursor,
785 krb5_ccache *id)
786 {
787 return cursor->ops->get_cache_next(context, cursor->cursor, id);
788 }
789
790 /*
791 * Destroy the cursor `cursor'.
792 */
793
794 krb5_error_code KRB5_LIB_FUNCTION
krb5_cc_cache_end_seq_get(krb5_context context,krb5_cc_cache_cursor cursor)795 krb5_cc_cache_end_seq_get (krb5_context context,
796 krb5_cc_cache_cursor cursor)
797 {
798 krb5_error_code ret;
799 ret = cursor->ops->end_cache_get(context, cursor->cursor);
800 cursor->ops = NULL;
801 free(cursor);
802 return ret;
803 }
804
805 /*
806 * Search for a matching credential cache of type `type' that have the
807 * `principal' as the default principal. If NULL is used for `type',
808 * the default type is used. On success, `id' needs to be freed with
809 * krb5_cc_close or krb5_cc_destroy. On failure, error code is
810 * returned and `id' is set to NULL.
811 */
812
813 krb5_error_code KRB5_LIB_FUNCTION
krb5_cc_cache_match(krb5_context context,krb5_principal client,const char * type,krb5_ccache * id)814 krb5_cc_cache_match (krb5_context context,
815 krb5_principal client,
816 const char *type,
817 krb5_ccache *id)
818 {
819 krb5_cc_cache_cursor cursor;
820 krb5_error_code ret;
821 krb5_ccache cache = NULL;
822
823 *id = NULL;
824
825 ret = krb5_cc_cache_get_first (context, type, &cursor);
826 if (ret)
827 return ret;
828
829 while ((ret = krb5_cc_cache_next (context, cursor, &cache)) == 0) {
830 krb5_principal principal;
831
832 ret = krb5_cc_get_principal(context, cache, &principal);
833 if (ret == 0) {
834 krb5_boolean match;
835
836 match = krb5_principal_compare(context, principal, client);
837 krb5_free_principal(context, principal);
838 if (match)
839 break;
840 }
841
842 krb5_cc_close(context, cache);
843 cache = NULL;
844 }
845
846 krb5_cc_cache_end_seq_get(context, cursor);
847
848 if (cache == NULL) {
849 char *str;
850
851 krb5_unparse_name(context, client, &str);
852
853 krb5_set_error_string(context, "Principal %s not found in a "
854 "credential cache", str ? str : "<out of memory>");
855 if (str)
856 free(str);
857 return KRB5_CC_NOTFOUND;
858 }
859 *id = cache;
860
861 return 0;
862 }
863
864