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
)
47 HEIMDAL_MUTEX_lock(&ccache_mutex
);
49 HEIMDAL_MUTEX_unlock(&ccache_mutex
);
51 ret
= asprintf(&name
, "%ld:%u", (long)uid
, n
);
59 kcm_ccache_resolve(krb5_context context
,
68 ret
= KRB5_FCC_NOFILE
;
70 HEIMDAL_MUTEX_lock(&ccache_mutex
);
72 for (p
= ccache_head
; p
!= NULL
; p
= p
->next
) {
73 if ((p
->flags
& KCM_FLAGS_VALID
) == 0)
75 if (strcmp(p
->name
, name
) == 0) {
82 kcm_retain_ccache(context
, p
);
86 HEIMDAL_MUTEX_unlock(&ccache_mutex
);
92 kcm_ccache_resolve_by_uuid(krb5_context context
,
101 ret
= KRB5_FCC_NOFILE
;
103 HEIMDAL_MUTEX_lock(&ccache_mutex
);
105 for (p
= ccache_head
; p
!= NULL
; p
= p
->next
) {
106 if ((p
->flags
& KCM_FLAGS_VALID
) == 0)
108 if (memcmp(p
->uuid
, uuid
, sizeof(*uuid
)) == 0) {
115 kcm_retain_ccache(context
, p
);
119 HEIMDAL_MUTEX_unlock(&ccache_mutex
);
125 kcm_ccache_get_uuids(krb5_context context
, kcm_client
*client
, kcm_operation opcode
, krb5_storage
*sp
)
130 ret
= KRB5_FCC_NOFILE
;
132 HEIMDAL_MUTEX_lock(&ccache_mutex
);
134 for (p
= ccache_head
; p
!= NULL
; p
= p
->next
) {
135 if ((p
->flags
& KCM_FLAGS_VALID
) == 0)
137 ret
= kcm_access(context
, client
, opcode
, p
);
142 krb5_storage_write(sp
, p
->uuid
, sizeof(p
->uuid
));
145 HEIMDAL_MUTEX_unlock(&ccache_mutex
);
151 krb5_error_code
kcm_debug_ccache(krb5_context context
)
155 for (p
= ccache_head
; p
!= NULL
; p
= p
->next
) {
156 char *cpn
= NULL
, *spn
= NULL
;
160 if ((p
->flags
& KCM_FLAGS_VALID
) == 0) {
161 kcm_log(7, "cache %08x: empty slot");
167 for (k
= p
->creds
; k
!= NULL
; k
= k
->next
)
170 if (p
->client
!= NULL
)
171 krb5_unparse_name(context
, p
->client
, &cpn
);
172 if (p
->server
!= NULL
)
173 krb5_unparse_name(context
, p
->server
, &spn
);
175 kcm_log(7, "cache %08x: name %s refcnt %d flags %04x mode %04o "
176 "uid %d gid %d client %s server %s ncreds %d",
177 p
, p
->name
, p
->refcnt
, p
->flags
, p
->mode
, p
->uid
, p
->gid
,
178 (cpn
== NULL
) ? "<none>" : cpn
,
179 (spn
== NULL
) ? "<none>" : spn
,
192 kcm_free_ccache_data_internal(krb5_context context
,
193 kcm_ccache_data
*cache
)
195 KCM_ASSERT_VALID(cache
);
197 if (cache
->name
!= NULL
) {
202 if (cache
->flags
& KCM_FLAGS_USE_KEYTAB
) {
203 krb5_kt_close(context
, cache
->key
.keytab
);
204 cache
->key
.keytab
= NULL
;
205 } else if (cache
->flags
& KCM_FLAGS_USE_CACHED_KEY
) {
206 krb5_free_keyblock_contents(context
, &cache
->key
.keyblock
);
207 krb5_keyblock_zero(&cache
->key
.keyblock
);
216 kcm_zero_ccache_data_internal(context
, cache
);
219 cache
->renew_life
= 0;
220 cache
->kdc_offset
= 0;
225 HEIMDAL_MUTEX_unlock(&cache
->mutex
);
226 HEIMDAL_MUTEX_destroy(&cache
->mutex
);
231 kcm_ccache_destroy(krb5_context context
, const char *name
)
233 kcm_ccache
*p
, ccache
;
236 ret
= KRB5_FCC_NOFILE
;
238 HEIMDAL_MUTEX_lock(&ccache_mutex
);
239 for (p
= &ccache_head
; *p
!= NULL
; p
= &(*p
)->next
) {
240 if (((*p
)->flags
& KCM_FLAGS_VALID
) == 0)
242 if (strcmp((*p
)->name
, name
) == 0) {
250 if ((*p
)->refcnt
!= 1) {
257 kcm_free_ccache_data_internal(context
, ccache
);
261 HEIMDAL_MUTEX_unlock(&ccache_mutex
);
266 static krb5_error_code
267 kcm_ccache_alloc(krb5_context context
,
271 kcm_ccache slot
= NULL
, p
;
277 /* First, check for duplicates */
278 HEIMDAL_MUTEX_lock(&ccache_mutex
);
280 for (p
= ccache_head
; p
!= NULL
; p
= p
->next
) {
281 if (p
->flags
& KCM_FLAGS_VALID
) {
282 if (strcmp(p
->name
, name
) == 0) {
286 } else if (slot
== NULL
)
294 * Create an enpty slot for us.
297 slot
= (kcm_ccache_data
*)malloc(sizeof(*slot
));
302 slot
->next
= ccache_head
;
303 HEIMDAL_MUTEX_init(&slot
->mutex
);
307 RAND_bytes(slot
->uuid
, sizeof(slot
->uuid
));
309 slot
->name
= strdup(name
);
310 if (slot
->name
== NULL
) {
316 slot
->flags
= KCM_FLAGS_VALID
;
317 slot
->mode
= S_IRUSR
| S_IWUSR
;
323 slot
->key
.keytab
= NULL
;
325 slot
->renew_life
= 0;
326 slot
->kdc_offset
= 0;
333 HEIMDAL_MUTEX_unlock(&ccache_mutex
);
337 HEIMDAL_MUTEX_unlock(&ccache_mutex
);
338 if (new_slot
&& slot
!= NULL
) {
339 HEIMDAL_MUTEX_destroy(&slot
->mutex
);
346 kcm_ccache_remove_creds_internal(krb5_context context
,
353 struct kcm_creds
*old
;
355 krb5_free_cred_contents(context
, &k
->cred
);
360 ccache
->creds
= NULL
;
366 kcm_ccache_remove_creds(krb5_context context
,
371 KCM_ASSERT_VALID(ccache
);
373 HEIMDAL_MUTEX_lock(&ccache
->mutex
);
374 ret
= kcm_ccache_remove_creds_internal(context
, ccache
);
375 HEIMDAL_MUTEX_unlock(&ccache
->mutex
);
381 kcm_zero_ccache_data_internal(krb5_context context
,
382 kcm_ccache_data
*cache
)
384 if (cache
->client
!= NULL
) {
385 krb5_free_principal(context
, cache
->client
);
386 cache
->client
= NULL
;
389 if (cache
->server
!= NULL
) {
390 krb5_free_principal(context
, cache
->server
);
391 cache
->server
= NULL
;
394 kcm_ccache_remove_creds_internal(context
, cache
);
400 kcm_zero_ccache_data(krb5_context context
,
405 KCM_ASSERT_VALID(cache
);
407 HEIMDAL_MUTEX_lock(&cache
->mutex
);
408 ret
= kcm_zero_ccache_data_internal(context
, cache
);
409 HEIMDAL_MUTEX_unlock(&cache
->mutex
);
415 kcm_retain_ccache(krb5_context context
,
418 KCM_ASSERT_VALID(ccache
);
420 HEIMDAL_MUTEX_lock(&ccache
->mutex
);
422 HEIMDAL_MUTEX_unlock(&ccache
->mutex
);
428 kcm_release_ccache(krb5_context context
, kcm_ccache c
)
430 krb5_error_code ret
= 0;
434 HEIMDAL_MUTEX_lock(&c
->mutex
);
435 if (c
->refcnt
== 1) {
436 kcm_free_ccache_data_internal(context
, c
);
440 HEIMDAL_MUTEX_unlock(&c
->mutex
);
447 kcm_ccache_gen_new(krb5_context context
,
456 name
= kcm_ccache_nextid(pid
, uid
, gid
);
458 return KRB5_CC_NOMEM
;
461 ret
= kcm_ccache_new(context
, name
, ccache
);
468 kcm_ccache_new(krb5_context context
,
474 ret
= kcm_ccache_alloc(context
, name
, ccache
);
477 * one reference is held by the linked list,
480 kcm_retain_ccache(context
, *ccache
);
487 kcm_ccache_destroy_if_empty(krb5_context context
,
492 KCM_ASSERT_VALID(ccache
);
494 if (ccache
->creds
== NULL
) {
495 ret
= kcm_ccache_destroy(context
, ccache
->name
);
503 kcm_ccache_store_cred(krb5_context context
,
511 KCM_ASSERT_VALID(ccache
);
513 HEIMDAL_MUTEX_lock(&ccache
->mutex
);
514 ret
= kcm_ccache_store_cred_internal(context
, ccache
, creds
, copy
, &tmp
);
515 HEIMDAL_MUTEX_unlock(&ccache
->mutex
);
521 kcm_ccache_find_cred_uuid(krb5_context context
,
527 for (c
= ccache
->creds
; c
!= NULL
; c
= c
->next
)
528 if (memcmp(c
->uuid
, uuid
, sizeof(c
->uuid
)) == 0)
537 kcm_ccache_store_cred_internal(krb5_context context
,
543 struct kcm_creds
**c
;
546 for (c
= &ccache
->creds
; *c
!= NULL
; c
= &(*c
)->next
)
549 *c
= (struct kcm_creds
*)calloc(1, sizeof(**c
));
551 return KRB5_CC_NOMEM
;
553 RAND_bytes((*c
)->uuid
, sizeof((*c
)->uuid
));
555 *credp
= &(*c
)->cred
;
558 ret
= krb5_copy_creds_contents(context
, creds
, *credp
);
572 kcm_ccache_remove_cred_internal(krb5_context context
,
574 krb5_flags whichfields
,
575 const krb5_creds
*mcreds
)
578 struct kcm_creds
**c
;
580 ret
= KRB5_CC_NOTFOUND
;
582 for (c
= &ccache
->creds
; *c
!= NULL
; c
= &(*c
)->next
) {
583 if (krb5_compare_creds(context
, whichfields
, mcreds
, &(*c
)->cred
)) {
584 struct kcm_creds
*cred
= *c
;
587 krb5_free_cred_contents(context
, &cred
->cred
);
599 kcm_ccache_remove_cred(krb5_context context
,
601 krb5_flags whichfields
,
602 const krb5_creds
*mcreds
)
606 KCM_ASSERT_VALID(ccache
);
608 HEIMDAL_MUTEX_lock(&ccache
->mutex
);
609 ret
= kcm_ccache_remove_cred_internal(context
, ccache
, whichfields
, mcreds
);
610 HEIMDAL_MUTEX_unlock(&ccache
->mutex
);
616 kcm_ccache_retrieve_cred_internal(krb5_context context
,
618 krb5_flags whichfields
,
619 const krb5_creds
*mcreds
,
626 memset(creds
, 0, sizeof(*creds
));
631 for (c
= ccache
->creds
; c
!= NULL
; c
= c
->next
) {
632 match
= krb5_compare_creds(context
, whichfields
, mcreds
, &c
->cred
);
646 kcm_ccache_retrieve_cred(krb5_context context
,
648 krb5_flags whichfields
,
649 const krb5_creds
*mcreds
,
654 KCM_ASSERT_VALID(ccache
);
656 HEIMDAL_MUTEX_lock(&ccache
->mutex
);
657 ret
= kcm_ccache_retrieve_cred_internal(context
, ccache
,
658 whichfields
, mcreds
, credp
);
659 HEIMDAL_MUTEX_unlock(&ccache
->mutex
);
665 kcm_ccache_first_name(kcm_client
*client
)
670 HEIMDAL_MUTEX_lock(&ccache_mutex
);
672 for (p
= ccache_head
; p
!= NULL
; p
= p
->next
) {
673 if (kcm_is_same_session(client
, p
->uid
, p
->session
))
677 name
= strdup(p
->name
);
678 HEIMDAL_MUTEX_unlock(&ccache_mutex
);