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(kcmuuid_t
)) == 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;
224 HEIMDAL_MUTEX_unlock(&cache
->mutex
);
225 HEIMDAL_MUTEX_destroy(&cache
->mutex
);
230 kcm_ccache_destroy(krb5_context context
, const char *name
)
232 kcm_ccache
*p
, ccache
;
235 ret
= KRB5_FCC_NOFILE
;
237 HEIMDAL_MUTEX_lock(&ccache_mutex
);
238 for (p
= &ccache_head
; *p
!= NULL
; p
= &(*p
)->next
) {
239 if (((*p
)->flags
& KCM_FLAGS_VALID
) == 0)
241 if (strcmp((*p
)->name
, name
) == 0) {
249 if ((*p
)->refcnt
!= 1) {
256 kcm_free_ccache_data_internal(context
, ccache
);
260 HEIMDAL_MUTEX_unlock(&ccache_mutex
);
265 static krb5_error_code
266 kcm_ccache_alloc(krb5_context context
,
270 kcm_ccache slot
= NULL
, p
;
276 /* First, check for duplicates */
277 HEIMDAL_MUTEX_lock(&ccache_mutex
);
279 for (p
= ccache_head
; p
!= NULL
; p
= p
->next
) {
280 if (p
->flags
& KCM_FLAGS_VALID
) {
281 if (strcmp(p
->name
, name
) == 0) {
285 } else if (slot
== NULL
)
293 * Create an enpty slot for us.
296 slot
= (kcm_ccache_data
*)malloc(sizeof(*slot
));
301 slot
->next
= ccache_head
;
302 HEIMDAL_MUTEX_init(&slot
->mutex
);
306 RAND_bytes(slot
->uuid
, sizeof(slot
->uuid
));
308 slot
->name
= strdup(name
);
309 if (slot
->name
== NULL
) {
315 slot
->flags
= KCM_FLAGS_VALID
;
316 slot
->mode
= S_IRUSR
| S_IWUSR
;
322 slot
->key
.keytab
= NULL
;
324 slot
->renew_life
= 0;
331 HEIMDAL_MUTEX_unlock(&ccache_mutex
);
335 HEIMDAL_MUTEX_unlock(&ccache_mutex
);
336 if (new_slot
&& slot
!= NULL
) {
337 HEIMDAL_MUTEX_destroy(&slot
->mutex
);
344 kcm_ccache_remove_creds_internal(krb5_context context
,
351 struct kcm_creds
*old
;
353 krb5_free_cred_contents(context
, &k
->cred
);
358 ccache
->creds
= NULL
;
364 kcm_ccache_remove_creds(krb5_context context
,
369 KCM_ASSERT_VALID(ccache
);
371 HEIMDAL_MUTEX_lock(&ccache
->mutex
);
372 ret
= kcm_ccache_remove_creds_internal(context
, ccache
);
373 HEIMDAL_MUTEX_unlock(&ccache
->mutex
);
379 kcm_zero_ccache_data_internal(krb5_context context
,
380 kcm_ccache_data
*cache
)
382 if (cache
->client
!= NULL
) {
383 krb5_free_principal(context
, cache
->client
);
384 cache
->client
= NULL
;
387 if (cache
->server
!= NULL
) {
388 krb5_free_principal(context
, cache
->server
);
389 cache
->server
= NULL
;
392 kcm_ccache_remove_creds_internal(context
, cache
);
398 kcm_zero_ccache_data(krb5_context context
,
403 KCM_ASSERT_VALID(cache
);
405 HEIMDAL_MUTEX_lock(&cache
->mutex
);
406 ret
= kcm_zero_ccache_data_internal(context
, cache
);
407 HEIMDAL_MUTEX_unlock(&cache
->mutex
);
413 kcm_retain_ccache(krb5_context context
,
416 KCM_ASSERT_VALID(ccache
);
418 HEIMDAL_MUTEX_lock(&ccache
->mutex
);
420 HEIMDAL_MUTEX_unlock(&ccache
->mutex
);
426 kcm_release_ccache(krb5_context context
, kcm_ccache c
)
428 krb5_error_code ret
= 0;
432 HEIMDAL_MUTEX_lock(&c
->mutex
);
433 if (c
->refcnt
== 1) {
434 kcm_free_ccache_data_internal(context
, c
);
438 HEIMDAL_MUTEX_unlock(&c
->mutex
);
445 kcm_ccache_gen_new(krb5_context context
,
454 name
= kcm_ccache_nextid(pid
, uid
, gid
);
456 return KRB5_CC_NOMEM
;
459 ret
= kcm_ccache_new(context
, name
, ccache
);
466 kcm_ccache_new(krb5_context context
,
472 ret
= kcm_ccache_alloc(context
, name
, ccache
);
475 * one reference is held by the linked list,
478 kcm_retain_ccache(context
, *ccache
);
485 kcm_ccache_destroy_if_empty(krb5_context context
,
490 KCM_ASSERT_VALID(ccache
);
492 if (ccache
->creds
== NULL
) {
493 ret
= kcm_ccache_destroy(context
, ccache
->name
);
501 kcm_ccache_store_cred(krb5_context context
,
509 KCM_ASSERT_VALID(ccache
);
511 HEIMDAL_MUTEX_lock(&ccache
->mutex
);
512 ret
= kcm_ccache_store_cred_internal(context
, ccache
, creds
, copy
, &tmp
);
513 HEIMDAL_MUTEX_unlock(&ccache
->mutex
);
519 kcm_ccache_find_cred_uuid(krb5_context context
,
525 for (c
= ccache
->creds
; c
!= NULL
; c
= c
->next
)
526 if (memcmp(c
->uuid
, uuid
, sizeof(c
->uuid
)) == 0)
535 kcm_ccache_store_cred_internal(krb5_context context
,
541 struct kcm_creds
**c
;
544 for (c
= &ccache
->creds
; *c
!= NULL
; c
= &(*c
)->next
)
547 *c
= (struct kcm_creds
*)calloc(1, sizeof(**c
));
549 return KRB5_CC_NOMEM
;
551 RAND_bytes((*c
)->uuid
, sizeof((*c
)->uuid
));
553 *credp
= &(*c
)->cred
;
556 ret
= krb5_copy_creds_contents(context
, creds
, *credp
);
570 kcm_ccache_remove_cred_internal(krb5_context context
,
572 krb5_flags whichfields
,
573 const krb5_creds
*mcreds
)
576 struct kcm_creds
**c
;
578 ret
= KRB5_CC_NOTFOUND
;
580 for (c
= &ccache
->creds
; *c
!= NULL
; c
= &(*c
)->next
) {
581 if (krb5_compare_creds(context
, whichfields
, mcreds
, &(*c
)->cred
)) {
582 struct kcm_creds
*cred
= *c
;
585 krb5_free_cred_contents(context
, &cred
->cred
);
597 kcm_ccache_remove_cred(krb5_context context
,
599 krb5_flags whichfields
,
600 const krb5_creds
*mcreds
)
604 KCM_ASSERT_VALID(ccache
);
606 HEIMDAL_MUTEX_lock(&ccache
->mutex
);
607 ret
= kcm_ccache_remove_cred_internal(context
, ccache
, whichfields
, mcreds
);
608 HEIMDAL_MUTEX_unlock(&ccache
->mutex
);
614 kcm_ccache_retrieve_cred_internal(krb5_context context
,
616 krb5_flags whichfields
,
617 const krb5_creds
*mcreds
,
624 memset(creds
, 0, sizeof(*creds
));
629 for (c
= ccache
->creds
; c
!= NULL
; c
= c
->next
) {
630 match
= krb5_compare_creds(context
, whichfields
, mcreds
, &c
->cred
);
644 kcm_ccache_retrieve_cred(krb5_context context
,
646 krb5_flags whichfields
,
647 const krb5_creds
*mcreds
,
652 KCM_ASSERT_VALID(ccache
);
654 HEIMDAL_MUTEX_lock(&ccache
->mutex
);
655 ret
= kcm_ccache_retrieve_cred_internal(context
, ccache
,
656 whichfields
, mcreds
, credp
);
657 HEIMDAL_MUTEX_unlock(&ccache
->mutex
);
663 kcm_ccache_first_name(kcm_client
*client
)
668 HEIMDAL_MUTEX_lock(&ccache_mutex
);
670 for (p
= ccache_head
; p
!= NULL
; p
= p
->next
) {
671 if (kcm_is_same_session(client
, p
->uid
, p
->session
))
675 name
= strdup(p
->name
);
676 HEIMDAL_MUTEX_unlock(&ccache_mutex
);