2 * Copyright (c) 2005, PADL Software Pty Ltd.
5 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of PADL Software nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY PADL SOFTWARE AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL PADL SOFTWARE OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 HEIMDAL_MUTEX ccache_mutex = HEIMDAL_MUTEX_INITIALIZER;
38 kcm_ccache_data *ccache_head = NULL;
39 static unsigned int ccache_nextid = 0;
41 char *kcm_ccache_nextid(pid_t pid, uid_t uid, gid_t gid)
46 HEIMDAL_MUTEX_lock(&ccache_mutex);
48 HEIMDAL_MUTEX_unlock(&ccache_mutex);
50 asprintf(&name, "%ld:%u", (long)uid, n);
56 kcm_ccache_resolve(krb5_context context,
65 ret = KRB5_FCC_NOFILE;
67 HEIMDAL_MUTEX_lock(&ccache_mutex);
69 for (p = ccache_head; p != NULL; p = p->next) {
70 if ((p->flags & KCM_FLAGS_VALID) == 0)
72 if (strcmp(p->name, name) == 0) {
79 kcm_retain_ccache(context, p);
83 HEIMDAL_MUTEX_unlock(&ccache_mutex);
89 kcm_ccache_resolve_by_uuid(krb5_context context,
98 ret = KRB5_FCC_NOFILE;
100 HEIMDAL_MUTEX_lock(&ccache_mutex);
102 for (p = ccache_head; p != NULL; p = p->next) {
103 if ((p->flags & KCM_FLAGS_VALID) == 0)
105 if (memcmp(p->uuid, uuid, sizeof(kcmuuid_t)) == 0) {
112 kcm_retain_ccache(context, p);
116 HEIMDAL_MUTEX_unlock(&ccache_mutex);
122 kcm_ccache_get_uuids(krb5_context context, kcm_client *client, kcm_operation opcode, krb5_storage *sp)
127 ret = KRB5_FCC_NOFILE;
129 HEIMDAL_MUTEX_lock(&ccache_mutex);
131 for (p = ccache_head; p != NULL; p = p->next) {
132 if ((p->flags & KCM_FLAGS_VALID) == 0)
134 ret = kcm_access(context, client, opcode, p);
139 krb5_storage_write(sp, p->uuid, sizeof(p->uuid));
142 HEIMDAL_MUTEX_unlock(&ccache_mutex);
148 krb5_error_code kcm_debug_ccache(krb5_context context)
152 for (p = ccache_head; p != NULL; p = p->next) {
153 char *cpn = NULL, *spn = NULL;
157 if ((p->flags & KCM_FLAGS_VALID) == 0) {
158 kcm_log(7, "cache %08x: empty slot");
164 for (k = p->creds; k != NULL; k = k->next)
167 if (p->client != NULL)
168 krb5_unparse_name(context, p->client, &cpn);
169 if (p->server != NULL)
170 krb5_unparse_name(context, p->server, &spn);
172 kcm_log(7, "cache %08x: name %s refcnt %d flags %04x mode %04o "
173 "uid %d gid %d client %s server %s ncreds %d",
174 p, p->name, p->refcnt, p->flags, p->mode, p->uid, p->gid,
175 (cpn == NULL) ? "<none>" : cpn,
176 (spn == NULL) ? "<none>" : spn,
189 kcm_free_ccache_data_internal(krb5_context context,
190 kcm_ccache_data *cache)
192 KCM_ASSERT_VALID(cache);
194 if (cache->name != NULL) {
199 if (cache->flags & KCM_FLAGS_USE_KEYTAB) {
200 krb5_kt_close(context, cache->key.keytab);
201 cache->key.keytab = NULL;
202 } else if (cache->flags & KCM_FLAGS_USE_CACHED_KEY) {
203 krb5_free_keyblock_contents(context, &cache->key.keyblock);
204 krb5_keyblock_zero(&cache->key.keyblock);
213 kcm_zero_ccache_data_internal(context, cache);
216 cache->renew_life = 0;
221 HEIMDAL_MUTEX_unlock(&cache->mutex);
222 HEIMDAL_MUTEX_destroy(&cache->mutex);
227 kcm_ccache_destroy(krb5_context context, const char *name)
229 kcm_ccache *p, ccache;
232 ret = KRB5_FCC_NOFILE;
234 HEIMDAL_MUTEX_lock(&ccache_mutex);
235 for (p = &ccache_head; *p != NULL; p = &(*p)->next) {
236 if (((*p)->flags & KCM_FLAGS_VALID) == 0)
238 if (strcmp((*p)->name, name) == 0) {
246 if ((*p)->refcnt != 1) {
253 kcm_free_ccache_data_internal(context, ccache);
257 HEIMDAL_MUTEX_unlock(&ccache_mutex);
262 static krb5_error_code
263 kcm_ccache_alloc(krb5_context context,
267 kcm_ccache slot = NULL, p;
273 /* First, check for duplicates */
274 HEIMDAL_MUTEX_lock(&ccache_mutex);
276 for (p = ccache_head; p != NULL; p = p->next) {
277 if (p->flags & KCM_FLAGS_VALID) {
278 if (strcmp(p->name, name) == 0) {
282 } else if (slot == NULL)
290 * Create an enpty slot for us.
293 slot = (kcm_ccache_data *)malloc(sizeof(*slot));
298 slot->next = ccache_head;
299 HEIMDAL_MUTEX_init(&slot->mutex);
303 RAND_bytes(slot->uuid, sizeof(slot->uuid));
305 slot->name = strdup(name);
306 if (slot->name == NULL) {
312 slot->flags = KCM_FLAGS_VALID;
313 slot->mode = S_IRUSR | S_IWUSR;
319 slot->key.keytab = NULL;
321 slot->renew_life = 0;
328 HEIMDAL_MUTEX_unlock(&ccache_mutex);
332 HEIMDAL_MUTEX_unlock(&ccache_mutex);
333 if (new_slot && slot != NULL) {
334 HEIMDAL_MUTEX_destroy(&slot->mutex);
341 kcm_ccache_remove_creds_internal(krb5_context context,
348 struct kcm_creds *old;
350 krb5_free_cred_contents(context, &k->cred);
355 ccache->creds = NULL;
361 kcm_ccache_remove_creds(krb5_context context,
366 KCM_ASSERT_VALID(ccache);
368 HEIMDAL_MUTEX_lock(&ccache->mutex);
369 ret = kcm_ccache_remove_creds_internal(context, ccache);
370 HEIMDAL_MUTEX_unlock(&ccache->mutex);
376 kcm_zero_ccache_data_internal(krb5_context context,
377 kcm_ccache_data *cache)
379 if (cache->client != NULL) {
380 krb5_free_principal(context, cache->client);
381 cache->client = NULL;
384 if (cache->server != NULL) {
385 krb5_free_principal(context, cache->server);
386 cache->server = NULL;
389 kcm_ccache_remove_creds_internal(context, cache);
395 kcm_zero_ccache_data(krb5_context context,
400 KCM_ASSERT_VALID(cache);
402 HEIMDAL_MUTEX_lock(&cache->mutex);
403 ret = kcm_zero_ccache_data_internal(context, cache);
404 HEIMDAL_MUTEX_unlock(&cache->mutex);
410 kcm_retain_ccache(krb5_context context,
413 KCM_ASSERT_VALID(ccache);
415 HEIMDAL_MUTEX_lock(&ccache->mutex);
417 HEIMDAL_MUTEX_unlock(&ccache->mutex);
423 kcm_release_ccache(krb5_context context, kcm_ccache c)
425 krb5_error_code ret = 0;
429 HEIMDAL_MUTEX_lock(&c->mutex);
430 if (c->refcnt == 1) {
431 kcm_free_ccache_data_internal(context, c);
435 HEIMDAL_MUTEX_unlock(&c->mutex);
442 kcm_ccache_gen_new(krb5_context context,
451 name = kcm_ccache_nextid(pid, uid, gid);
453 return KRB5_CC_NOMEM;
456 ret = kcm_ccache_new(context, name, ccache);
463 kcm_ccache_new(krb5_context context,
469 ret = kcm_ccache_alloc(context, name, ccache);
472 * one reference is held by the linked list,
475 kcm_retain_ccache(context, *ccache);
482 kcm_ccache_destroy_if_empty(krb5_context context,
487 KCM_ASSERT_VALID(ccache);
489 if (ccache->creds == NULL) {
490 ret = kcm_ccache_destroy(context, ccache->name);
498 kcm_ccache_store_cred(krb5_context context,
506 KCM_ASSERT_VALID(ccache);
508 HEIMDAL_MUTEX_lock(&ccache->mutex);
509 ret = kcm_ccache_store_cred_internal(context, ccache, creds, copy, &tmp);
510 HEIMDAL_MUTEX_unlock(&ccache->mutex);
516 kcm_ccache_find_cred_uuid(krb5_context context,
522 for (c = ccache->creds; c != NULL; c = c->next)
523 if (memcmp(c->uuid, uuid, sizeof(c->uuid)) == 0)
532 kcm_ccache_store_cred_internal(krb5_context context,
538 struct kcm_creds **c;
541 for (c = &ccache->creds; *c != NULL; c = &(*c)->next)
544 *c = (struct kcm_creds *)calloc(1, sizeof(**c));
546 return KRB5_CC_NOMEM;
548 RAND_bytes((*c)->uuid, sizeof((*c)->uuid));
550 *credp = &(*c)->cred;
553 ret = krb5_copy_creds_contents(context, creds, *credp);
567 kcm_ccache_remove_cred_internal(krb5_context context,
569 krb5_flags whichfields,
570 const krb5_creds *mcreds)
573 struct kcm_creds **c;
575 ret = KRB5_CC_NOTFOUND;
577 for (c = &ccache->creds; *c != NULL; c = &(*c)->next) {
578 if (krb5_compare_creds(context, whichfields, mcreds, &(*c)->cred)) {
579 struct kcm_creds *cred = *c;
582 krb5_free_cred_contents(context, &cred->cred);
594 kcm_ccache_remove_cred(krb5_context context,
596 krb5_flags whichfields,
597 const krb5_creds *mcreds)
601 KCM_ASSERT_VALID(ccache);
603 HEIMDAL_MUTEX_lock(&ccache->mutex);
604 ret = kcm_ccache_remove_cred_internal(context, ccache, whichfields, mcreds);
605 HEIMDAL_MUTEX_unlock(&ccache->mutex);
611 kcm_ccache_retrieve_cred_internal(krb5_context context,
613 krb5_flags whichfields,
614 const krb5_creds *mcreds,
621 memset(creds, 0, sizeof(*creds));
626 for (c = ccache->creds; c != NULL; c = c->next) {
627 match = krb5_compare_creds(context, whichfields, mcreds, &c->cred);
641 kcm_ccache_retrieve_cred(krb5_context context,
643 krb5_flags whichfields,
644 const krb5_creds *mcreds,
649 KCM_ASSERT_VALID(ccache);
651 HEIMDAL_MUTEX_lock(&ccache->mutex);
652 ret = kcm_ccache_retrieve_cred_internal(context, ccache,
653 whichfields, mcreds, credp);
654 HEIMDAL_MUTEX_unlock(&ccache->mutex);
660 kcm_ccache_first_name(kcm_client *client)
665 HEIMDAL_MUTEX_lock(&ccache_mutex);
667 for (p = ccache_head; p != NULL; p = p->next) {
668 if (kcm_is_same_session(client, p->uid, p->session))
672 name = strdup(p->name);
673 HEIMDAL_MUTEX_unlock(&ccache_mutex);