No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / kcm / cache.c
blob92d1af26753038be2eb93b15813d91db850f99e5
1 /*
2 * Copyright (c) 2005, PADL Software Pty Ltd.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of PADL Software nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY PADL SOFTWARE AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL PADL SOFTWARE OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #include "kcm_locl.h"
35 __RCSID("$Heimdal: cache.c 14566 2005-02-06 01:22:49Z lukeh $"
36 "$NetBSD$");
38 static HEIMDAL_MUTEX ccache_mutex = HEIMDAL_MUTEX_INITIALIZER;
39 static kcm_ccache_data *ccache_head = NULL;
40 static unsigned int ccache_nextid = 0;
42 char *kcm_ccache_nextid(pid_t pid, uid_t uid, gid_t gid)
44 unsigned n;
45 char *name;
47 HEIMDAL_MUTEX_lock(&ccache_mutex);
48 n = ++ccache_nextid;
49 HEIMDAL_MUTEX_unlock(&ccache_mutex);
51 asprintf(&name, "%d:%u", uid, n);
53 return name;
56 static krb5_error_code
57 kcm_ccache_resolve_internal(krb5_context context,
58 const char *name,
59 kcm_ccache *ccache)
61 kcm_ccache p;
62 krb5_error_code ret;
64 *ccache = NULL;
66 ret = KRB5_FCC_NOFILE;
68 HEIMDAL_MUTEX_lock(&ccache_mutex);
70 for (p = ccache_head; p != NULL; p = p->next) {
71 if ((p->flags & KCM_FLAGS_VALID) == 0)
72 continue;
73 if (strcmp(p->name, name) == 0) {
74 ret = 0;
75 break;
79 if (ret == 0) {
80 kcm_retain_ccache(context, p);
81 *ccache = p;
84 HEIMDAL_MUTEX_unlock(&ccache_mutex);
86 return ret;
89 krb5_error_code kcm_debug_ccache(krb5_context context)
91 kcm_ccache p;
93 for (p = ccache_head; p != NULL; p = p->next) {
94 char *cpn = NULL, *spn = NULL;
95 int ncreds = 0;
96 struct kcm_creds *k;
98 if ((p->flags & KCM_FLAGS_VALID) == 0) {
99 kcm_log(7, "cache %08x: empty slot");
100 continue;
103 KCM_ASSERT_VALID(p);
105 for (k = p->creds; k != NULL; k = k->next)
106 ncreds++;
108 if (p->client != NULL)
109 krb5_unparse_name(context, p->client, &cpn);
110 if (p->server != NULL)
111 krb5_unparse_name(context, p->server, &spn);
113 kcm_log(7, "cache %08x: name %s refcnt %d flags %04x mode %04o "
114 "uid %d gid %d client %s server %s ncreds %d",
115 p, p->name, p->refcnt, p->flags, p->mode, p->uid, p->gid,
116 (cpn == NULL) ? "<none>" : cpn,
117 (spn == NULL) ? "<none>" : spn,
118 ncreds);
120 if (cpn != NULL)
121 free(cpn);
122 if (spn != NULL)
123 free(spn);
126 return 0;
129 static krb5_error_code
130 kcm_ccache_destroy_internal(krb5_context context, const char *name)
132 kcm_ccache *p;
133 krb5_error_code ret;
135 ret = KRB5_FCC_NOFILE;
137 HEIMDAL_MUTEX_lock(&ccache_mutex);
138 for (p = &ccache_head; *p != NULL; p = &(*p)->next) {
139 if (((*p)->flags & KCM_FLAGS_VALID) == 0)
140 continue;
141 if (strcmp((*p)->name, name) == 0) {
142 ret = 0;
143 break;
147 if (ret)
148 goto out;
150 kcm_release_ccache(context, p);
152 out:
153 HEIMDAL_MUTEX_unlock(&ccache_mutex);
155 return ret;
158 static krb5_error_code
159 kcm_ccache_alloc(krb5_context context,
160 const char *name,
161 kcm_ccache *ccache)
163 kcm_ccache slot = NULL, p;
164 krb5_error_code ret;
165 int new_slot = 0;
167 *ccache = NULL;
169 /* First, check for duplicates */
170 HEIMDAL_MUTEX_lock(&ccache_mutex);
171 ret = 0;
172 for (p = ccache_head; p != NULL; p = p->next) {
173 if (p->flags & KCM_FLAGS_VALID) {
174 if (strcmp(p->name, name) == 0) {
175 ret = KRB5_CC_WRITE;
176 break;
178 } else if (slot == NULL)
179 slot = p;
182 if (ret)
183 goto out;
186 * Then try and find an empty slot
187 * XXX we need to recycle slots for this to actually do anything
189 if (slot == NULL) {
190 for (; p != NULL; p = p->next) {
191 if ((p->flags & KCM_FLAGS_VALID) == 0) {
192 slot = p;
193 break;
197 if (slot == NULL) {
198 slot = (kcm_ccache_data *)malloc(sizeof(*slot));
199 if (slot == NULL) {
200 ret = KRB5_CC_NOMEM;
201 goto out;
203 slot->next = ccache_head;
204 HEIMDAL_MUTEX_init(&slot->mutex);
205 new_slot = 1;
209 slot->name = strdup(name);
210 if (slot->name == NULL) {
211 ret = KRB5_CC_NOMEM;
212 goto out;
215 slot->refcnt = 1;
216 slot->flags = KCM_FLAGS_VALID;
217 slot->mode = S_IRUSR | S_IWUSR;
218 slot->uid = -1;
219 slot->gid = -1;
220 slot->client = NULL;
221 slot->server = NULL;
222 slot->creds = NULL;
223 slot->n_cursor = 0;
224 slot->cursors = NULL;
225 slot->key.keytab = NULL;
226 slot->tkt_life = 0;
227 slot->renew_life = 0;
229 if (new_slot)
230 ccache_head = slot;
232 *ccache = slot;
234 HEIMDAL_MUTEX_unlock(&ccache_mutex);
235 return 0;
237 out:
238 HEIMDAL_MUTEX_unlock(&ccache_mutex);
239 if (new_slot && slot != NULL) {
240 HEIMDAL_MUTEX_destroy(&slot->mutex);
241 free(slot);
243 return ret;
246 krb5_error_code
247 kcm_ccache_remove_creds_internal(krb5_context context,
248 kcm_ccache ccache)
250 struct kcm_creds *k;
251 struct kcm_cursor *c;
253 k = ccache->creds;
254 while (k != NULL) {
255 struct kcm_creds *old;
257 krb5_free_cred_contents(context, &k->cred);
258 old = k;
259 k = k->next;
260 free(old);
262 ccache->creds = NULL;
264 /* remove anything that would have pointed into the creds too */
266 ccache->n_cursor = 0;
268 c = ccache->cursors;
269 while (c != NULL) {
270 struct kcm_cursor *old;
272 old = c;
273 c = c->next;
274 free(old);
276 ccache->cursors = NULL;
278 return 0;
281 krb5_error_code
282 kcm_ccache_remove_creds(krb5_context context,
283 kcm_ccache ccache)
285 krb5_error_code ret;
287 KCM_ASSERT_VALID(ccache);
289 HEIMDAL_MUTEX_lock(&ccache->mutex);
290 ret = kcm_ccache_remove_creds_internal(context, ccache);
291 HEIMDAL_MUTEX_unlock(&ccache->mutex);
293 return ret;
296 krb5_error_code
297 kcm_zero_ccache_data_internal(krb5_context context,
298 kcm_ccache_data *cache)
300 if (cache->client != NULL) {
301 krb5_free_principal(context, cache->client);
302 cache->client = NULL;
305 if (cache->server != NULL) {
306 krb5_free_principal(context, cache->server);
307 cache->server = NULL;
310 kcm_ccache_remove_creds_internal(context, cache);
312 return 0;
315 krb5_error_code
316 kcm_zero_ccache_data(krb5_context context,
317 kcm_ccache cache)
319 krb5_error_code ret;
321 KCM_ASSERT_VALID(cache);
323 HEIMDAL_MUTEX_lock(&cache->mutex);
324 ret = kcm_zero_ccache_data_internal(context, cache);
325 HEIMDAL_MUTEX_unlock(&cache->mutex);
327 return ret;
330 static krb5_error_code
331 kcm_free_ccache_data_internal(krb5_context context,
332 kcm_ccache_data *cache)
334 KCM_ASSERT_VALID(cache);
336 if (cache->name != NULL) {
337 free(cache->name);
338 cache->name = NULL;
341 if (cache->flags & KCM_FLAGS_USE_KEYTAB) {
342 krb5_kt_close(context, cache->key.keytab);
343 cache->key.keytab = NULL;
344 } else if (cache->flags & KCM_FLAGS_USE_CACHED_KEY) {
345 krb5_free_keyblock_contents(context, &cache->key.keyblock);
346 krb5_keyblock_zero(&cache->key.keyblock);
349 cache->flags = 0;
350 cache->mode = 0;
351 cache->uid = -1;
352 cache->gid = -1;
354 kcm_zero_ccache_data_internal(context, cache);
356 cache->tkt_life = 0;
357 cache->renew_life = 0;
359 cache->next = NULL;
360 cache->refcnt = 0;
362 HEIMDAL_MUTEX_unlock(&cache->mutex);
363 HEIMDAL_MUTEX_destroy(&cache->mutex);
365 return 0;
368 krb5_error_code
369 kcm_retain_ccache(krb5_context context,
370 kcm_ccache ccache)
372 KCM_ASSERT_VALID(ccache);
374 HEIMDAL_MUTEX_lock(&ccache->mutex);
375 ccache->refcnt++;
376 HEIMDAL_MUTEX_unlock(&ccache->mutex);
378 return 0;
381 krb5_error_code
382 kcm_release_ccache(krb5_context context,
383 kcm_ccache *ccache)
385 kcm_ccache c = *ccache;
386 krb5_error_code ret = 0;
388 KCM_ASSERT_VALID(c);
390 HEIMDAL_MUTEX_lock(&c->mutex);
391 if (c->refcnt == 1) {
392 ret = kcm_free_ccache_data_internal(context, c);
393 if (ret == 0)
394 free(c);
395 } else {
396 c->refcnt--;
397 HEIMDAL_MUTEX_unlock(&c->mutex);
400 *ccache = NULL;
402 return ret;
405 krb5_error_code
406 kcm_ccache_gen_new(krb5_context context,
407 pid_t pid,
408 uid_t uid,
409 gid_t gid,
410 kcm_ccache *ccache)
412 krb5_error_code ret;
413 char *name;
415 name = kcm_ccache_nextid(pid, uid, gid);
416 if (name == NULL) {
417 return KRB5_CC_NOMEM;
420 ret = kcm_ccache_new(context, name, ccache);
422 free(name);
423 return ret;
426 krb5_error_code
427 kcm_ccache_new(krb5_context context,
428 const char *name,
429 kcm_ccache *ccache)
431 krb5_error_code ret;
433 ret = kcm_ccache_alloc(context, name, ccache);
434 if (ret == 0) {
436 * one reference is held by the linked list,
437 * one by the caller
439 kcm_retain_ccache(context, *ccache);
442 return ret;
445 krb5_error_code
446 kcm_ccache_resolve(krb5_context context,
447 const char *name,
448 kcm_ccache *ccache)
450 krb5_error_code ret;
452 ret = kcm_ccache_resolve_internal(context, name, ccache);
454 return ret;
457 krb5_error_code
458 kcm_ccache_destroy(krb5_context context,
459 const char *name)
461 krb5_error_code ret;
463 ret = kcm_ccache_destroy_internal(context, name);
465 return ret;
468 krb5_error_code
469 kcm_ccache_destroy_if_empty(krb5_context context,
470 kcm_ccache ccache)
472 krb5_error_code ret;
474 KCM_ASSERT_VALID(ccache);
476 if (ccache->creds == NULL) {
477 ret = kcm_ccache_destroy_internal(context, ccache->name);
478 } else
479 ret = 0;
481 return ret;
484 krb5_error_code
485 kcm_ccache_store_cred(krb5_context context,
486 kcm_ccache ccache,
487 krb5_creds *creds,
488 int copy)
490 krb5_error_code ret;
491 krb5_creds *tmp;
493 KCM_ASSERT_VALID(ccache);
495 HEIMDAL_MUTEX_lock(&ccache->mutex);
496 ret = kcm_ccache_store_cred_internal(context, ccache, creds, copy, &tmp);
497 HEIMDAL_MUTEX_unlock(&ccache->mutex);
499 return ret;
502 krb5_error_code
503 kcm_ccache_store_cred_internal(krb5_context context,
504 kcm_ccache ccache,
505 krb5_creds *creds,
506 int copy,
507 krb5_creds **credp)
509 struct kcm_creds **c;
510 krb5_error_code ret;
512 for (c = &ccache->creds; *c != NULL; c = &(*c)->next)
515 *c = (struct kcm_creds *)malloc(sizeof(struct kcm_creds));
516 if (*c == NULL) {
517 return KRB5_CC_NOMEM;
520 *credp = &(*c)->cred;
522 if (copy) {
523 ret = krb5_copy_creds_contents(context, creds, *credp);
524 if (ret) {
525 free(*c);
526 *c = NULL;
528 } else {
529 **credp = *creds;
530 ret = 0;
533 (*c)->next = NULL;
535 return ret;
538 static void
539 remove_cred(krb5_context context,
540 struct kcm_creds **c)
542 struct kcm_creds *cred;
544 cred = *c;
546 *c = cred->next;
548 krb5_free_cred_contents(context, &cred->cred);
549 free(cred);
552 krb5_error_code
553 kcm_ccache_remove_cred_internal(krb5_context context,
554 kcm_ccache ccache,
555 krb5_flags whichfields,
556 const krb5_creds *mcreds)
558 krb5_error_code ret;
559 struct kcm_creds **c;
561 ret = KRB5_CC_NOTFOUND;
563 for (c = &ccache->creds; *c != NULL; c = &(*c)->next) {
564 if (krb5_compare_creds(context, whichfields, mcreds, &(*c)->cred)) {
565 remove_cred(context, c);
566 ret = 0;
570 return ret;
573 krb5_error_code
574 kcm_ccache_remove_cred(krb5_context context,
575 kcm_ccache ccache,
576 krb5_flags whichfields,
577 const krb5_creds *mcreds)
579 krb5_error_code ret;
581 KCM_ASSERT_VALID(ccache);
583 HEIMDAL_MUTEX_lock(&ccache->mutex);
584 ret = kcm_ccache_remove_cred_internal(context, ccache, whichfields, mcreds);
585 HEIMDAL_MUTEX_unlock(&ccache->mutex);
587 return ret;
590 krb5_error_code
591 kcm_ccache_retrieve_cred_internal(krb5_context context,
592 kcm_ccache ccache,
593 krb5_flags whichfields,
594 const krb5_creds *mcreds,
595 krb5_creds **creds)
597 krb5_boolean match;
598 struct kcm_creds *c;
599 krb5_error_code ret;
601 memset(creds, 0, sizeof(*creds));
603 ret = KRB5_CC_END;
605 match = FALSE;
606 for (c = ccache->creds; c != NULL; c = c->next) {
607 match = krb5_compare_creds(context, whichfields, mcreds, &c->cred);
608 if (match)
609 break;
612 if (match) {
613 ret = 0;
614 *creds = &c->cred;
617 return ret;
620 krb5_error_code
621 kcm_ccache_retrieve_cred(krb5_context context,
622 kcm_ccache ccache,
623 krb5_flags whichfields,
624 const krb5_creds *mcreds,
625 krb5_creds **credp)
627 krb5_error_code ret;
629 KCM_ASSERT_VALID(ccache);
631 HEIMDAL_MUTEX_lock(&ccache->mutex);
632 ret = kcm_ccache_retrieve_cred_internal(context, ccache,
633 whichfields, mcreds, credp);
634 HEIMDAL_MUTEX_unlock(&ccache->mutex);
636 return ret;