2 * Copyright (c) 2005, PADL Software Pty Ltd.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
35 __RCSID("$Heimdal: cache.c 14566 2005-02-06 01:22:49Z lukeh $"
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
)
47 HEIMDAL_MUTEX_lock(&ccache_mutex
);
49 HEIMDAL_MUTEX_unlock(&ccache_mutex
);
51 asprintf(&name
, "%d:%u", uid
, n
);
56 static krb5_error_code
57 kcm_ccache_resolve_internal(krb5_context context
,
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)
73 if (strcmp(p
->name
, name
) == 0) {
80 kcm_retain_ccache(context
, p
);
84 HEIMDAL_MUTEX_unlock(&ccache_mutex
);
89 krb5_error_code
kcm_debug_ccache(krb5_context context
)
93 for (p
= ccache_head
; p
!= NULL
; p
= p
->next
) {
94 char *cpn
= NULL
, *spn
= NULL
;
98 if ((p
->flags
& KCM_FLAGS_VALID
) == 0) {
99 kcm_log(7, "cache %08x: empty slot");
105 for (k
= p
->creds
; k
!= NULL
; k
= k
->next
)
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
,
129 static krb5_error_code
130 kcm_ccache_destroy_internal(krb5_context context
, const char *name
)
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)
141 if (strcmp((*p
)->name
, name
) == 0) {
150 kcm_release_ccache(context
, p
);
153 HEIMDAL_MUTEX_unlock(&ccache_mutex
);
158 static krb5_error_code
159 kcm_ccache_alloc(krb5_context context
,
163 kcm_ccache slot
= NULL
, p
;
169 /* First, check for duplicates */
170 HEIMDAL_MUTEX_lock(&ccache_mutex
);
172 for (p
= ccache_head
; p
!= NULL
; p
= p
->next
) {
173 if (p
->flags
& KCM_FLAGS_VALID
) {
174 if (strcmp(p
->name
, name
) == 0) {
178 } else if (slot
== NULL
)
186 * Then try and find an empty slot
187 * XXX we need to recycle slots for this to actually do anything
190 for (; p
!= NULL
; p
= p
->next
) {
191 if ((p
->flags
& KCM_FLAGS_VALID
) == 0) {
198 slot
= (kcm_ccache_data
*)malloc(sizeof(*slot
));
203 slot
->next
= ccache_head
;
204 HEIMDAL_MUTEX_init(&slot
->mutex
);
209 slot
->name
= strdup(name
);
210 if (slot
->name
== NULL
) {
216 slot
->flags
= KCM_FLAGS_VALID
;
217 slot
->mode
= S_IRUSR
| S_IWUSR
;
224 slot
->cursors
= NULL
;
225 slot
->key
.keytab
= NULL
;
227 slot
->renew_life
= 0;
234 HEIMDAL_MUTEX_unlock(&ccache_mutex
);
238 HEIMDAL_MUTEX_unlock(&ccache_mutex
);
239 if (new_slot
&& slot
!= NULL
) {
240 HEIMDAL_MUTEX_destroy(&slot
->mutex
);
247 kcm_ccache_remove_creds_internal(krb5_context context
,
251 struct kcm_cursor
*c
;
255 struct kcm_creds
*old
;
257 krb5_free_cred_contents(context
, &k
->cred
);
262 ccache
->creds
= NULL
;
264 /* remove anything that would have pointed into the creds too */
266 ccache
->n_cursor
= 0;
270 struct kcm_cursor
*old
;
276 ccache
->cursors
= NULL
;
282 kcm_ccache_remove_creds(krb5_context context
,
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
);
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
);
316 kcm_zero_ccache_data(krb5_context context
,
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
);
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
) {
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
);
354 kcm_zero_ccache_data_internal(context
, cache
);
357 cache
->renew_life
= 0;
362 HEIMDAL_MUTEX_unlock(&cache
->mutex
);
363 HEIMDAL_MUTEX_destroy(&cache
->mutex
);
369 kcm_retain_ccache(krb5_context context
,
372 KCM_ASSERT_VALID(ccache
);
374 HEIMDAL_MUTEX_lock(&ccache
->mutex
);
376 HEIMDAL_MUTEX_unlock(&ccache
->mutex
);
382 kcm_release_ccache(krb5_context context
,
385 kcm_ccache c
= *ccache
;
386 krb5_error_code ret
= 0;
390 HEIMDAL_MUTEX_lock(&c
->mutex
);
391 if (c
->refcnt
== 1) {
392 ret
= kcm_free_ccache_data_internal(context
, c
);
397 HEIMDAL_MUTEX_unlock(&c
->mutex
);
406 kcm_ccache_gen_new(krb5_context context
,
415 name
= kcm_ccache_nextid(pid
, uid
, gid
);
417 return KRB5_CC_NOMEM
;
420 ret
= kcm_ccache_new(context
, name
, ccache
);
427 kcm_ccache_new(krb5_context context
,
433 ret
= kcm_ccache_alloc(context
, name
, ccache
);
436 * one reference is held by the linked list,
439 kcm_retain_ccache(context
, *ccache
);
446 kcm_ccache_resolve(krb5_context context
,
452 ret
= kcm_ccache_resolve_internal(context
, name
, ccache
);
458 kcm_ccache_destroy(krb5_context context
,
463 ret
= kcm_ccache_destroy_internal(context
, name
);
469 kcm_ccache_destroy_if_empty(krb5_context context
,
474 KCM_ASSERT_VALID(ccache
);
476 if (ccache
->creds
== NULL
) {
477 ret
= kcm_ccache_destroy_internal(context
, ccache
->name
);
485 kcm_ccache_store_cred(krb5_context context
,
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
);
503 kcm_ccache_store_cred_internal(krb5_context context
,
509 struct kcm_creds
**c
;
512 for (c
= &ccache
->creds
; *c
!= NULL
; c
= &(*c
)->next
)
515 *c
= (struct kcm_creds
*)malloc(sizeof(struct kcm_creds
));
517 return KRB5_CC_NOMEM
;
520 *credp
= &(*c
)->cred
;
523 ret
= krb5_copy_creds_contents(context
, creds
, *credp
);
539 remove_cred(krb5_context context
,
540 struct kcm_creds
**c
)
542 struct kcm_creds
*cred
;
548 krb5_free_cred_contents(context
, &cred
->cred
);
553 kcm_ccache_remove_cred_internal(krb5_context context
,
555 krb5_flags whichfields
,
556 const krb5_creds
*mcreds
)
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
);
574 kcm_ccache_remove_cred(krb5_context context
,
576 krb5_flags whichfields
,
577 const krb5_creds
*mcreds
)
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
);
591 kcm_ccache_retrieve_cred_internal(krb5_context context
,
593 krb5_flags whichfields
,
594 const krb5_creds
*mcreds
,
601 memset(creds
, 0, sizeof(*creds
));
606 for (c
= ccache
->creds
; c
!= NULL
; c
= c
->next
) {
607 match
= krb5_compare_creds(context
, whichfields
, mcreds
, &c
->cred
);
621 kcm_ccache_retrieve_cred(krb5_context context
,
623 krb5_flags whichfields
,
624 const krb5_creds
*mcreds
,
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
);