Home | History | Annotate | Line # | Download | only in kcm
      1  1.4  christos /*	$NetBSD: cache.c,v 1.6 2023/06/19 21:41:41 christos Exp $	*/
      2  1.1     elric 
      3  1.1     elric /*
      4  1.1     elric  * Copyright (c) 2005, PADL Software Pty Ltd.
      5  1.1     elric  * All rights reserved.
      6  1.1     elric  *
      7  1.1     elric  * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
      8  1.1     elric  *
      9  1.1     elric  * Redistribution and use in source and binary forms, with or without
     10  1.1     elric  * modification, are permitted provided that the following conditions
     11  1.1     elric  * are met:
     12  1.1     elric  *
     13  1.1     elric  * 1. Redistributions of source code must retain the above copyright
     14  1.1     elric  *    notice, this list of conditions and the following disclaimer.
     15  1.1     elric  *
     16  1.1     elric  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1     elric  *    notice, this list of conditions and the following disclaimer in the
     18  1.1     elric  *    documentation and/or other materials provided with the distribution.
     19  1.1     elric  *
     20  1.1     elric  * 3. Neither the name of PADL Software nor the names of its contributors
     21  1.1     elric  *    may be used to endorse or promote products derived from this software
     22  1.1     elric  *    without specific prior written permission.
     23  1.1     elric  *
     24  1.1     elric  * THIS SOFTWARE IS PROVIDED BY PADL SOFTWARE AND CONTRIBUTORS ``AS IS'' AND
     25  1.1     elric  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.1     elric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.1     elric  * ARE DISCLAIMED.  IN NO EVENT SHALL PADL SOFTWARE OR CONTRIBUTORS BE LIABLE
     28  1.1     elric  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.1     elric  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.1     elric  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.1     elric  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.1     elric  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.1     elric  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.1     elric  * SUCH DAMAGE.
     35  1.1     elric  */
     36  1.1     elric 
     37  1.1     elric #include "kcm_locl.h"
     38  1.1     elric 
     39  1.1     elric HEIMDAL_MUTEX ccache_mutex = HEIMDAL_MUTEX_INITIALIZER;
     40  1.1     elric kcm_ccache_data *ccache_head = NULL;
     41  1.1     elric static unsigned int ccache_nextid = 0;
     42  1.1     elric 
     43  1.1     elric char *kcm_ccache_nextid(pid_t pid, uid_t uid, gid_t gid)
     44  1.1     elric {
     45  1.1     elric     unsigned n;
     46  1.1     elric     char *name;
     47  1.3  christos     int ret;
     48  1.1     elric 
     49  1.1     elric     HEIMDAL_MUTEX_lock(&ccache_mutex);
     50  1.1     elric     n = ++ccache_nextid;
     51  1.1     elric     HEIMDAL_MUTEX_unlock(&ccache_mutex);
     52  1.1     elric 
     53  1.3  christos     ret = asprintf(&name, "%ld:%u", (long)uid, n);
     54  1.3  christos     if (ret == -1)
     55  1.3  christos 	return NULL;
     56  1.1     elric 
     57  1.1     elric     return name;
     58  1.1     elric }
     59  1.1     elric 
     60  1.1     elric krb5_error_code
     61  1.1     elric kcm_ccache_resolve(krb5_context context,
     62  1.1     elric 		   const char *name,
     63  1.1     elric 		   kcm_ccache *ccache)
     64  1.1     elric {
     65  1.1     elric     kcm_ccache p;
     66  1.1     elric     krb5_error_code ret;
     67  1.1     elric 
     68  1.1     elric     *ccache = NULL;
     69  1.1     elric 
     70  1.1     elric     ret = KRB5_FCC_NOFILE;
     71  1.1     elric 
     72  1.1     elric     HEIMDAL_MUTEX_lock(&ccache_mutex);
     73  1.1     elric 
     74  1.1     elric     for (p = ccache_head; p != NULL; p = p->next) {
     75  1.1     elric 	if ((p->flags & KCM_FLAGS_VALID) == 0)
     76  1.1     elric 	    continue;
     77  1.1     elric 	if (strcmp(p->name, name) == 0) {
     78  1.1     elric 	    ret = 0;
     79  1.1     elric 	    break;
     80  1.1     elric 	}
     81  1.1     elric     }
     82  1.1     elric 
     83  1.1     elric     if (ret == 0) {
     84  1.1     elric 	kcm_retain_ccache(context, p);
     85  1.1     elric 	*ccache = p;
     86  1.1     elric     }
     87  1.1     elric 
     88  1.1     elric     HEIMDAL_MUTEX_unlock(&ccache_mutex);
     89  1.1     elric 
     90  1.1     elric     return ret;
     91  1.1     elric }
     92  1.1     elric 
     93  1.1     elric krb5_error_code
     94  1.1     elric kcm_ccache_resolve_by_uuid(krb5_context context,
     95  1.1     elric 			   kcmuuid_t uuid,
     96  1.1     elric 			   kcm_ccache *ccache)
     97  1.1     elric {
     98  1.1     elric     kcm_ccache p;
     99  1.1     elric     krb5_error_code ret;
    100  1.1     elric 
    101  1.1     elric     *ccache = NULL;
    102  1.1     elric 
    103  1.1     elric     ret = KRB5_FCC_NOFILE;
    104  1.1     elric 
    105  1.1     elric     HEIMDAL_MUTEX_lock(&ccache_mutex);
    106  1.1     elric 
    107  1.1     elric     for (p = ccache_head; p != NULL; p = p->next) {
    108  1.1     elric 	if ((p->flags & KCM_FLAGS_VALID) == 0)
    109  1.1     elric 	    continue;
    110  1.6  christos 	if (memcmp(p->uuid, uuid, sizeof(kcmuuid_t)) == 0) {
    111  1.1     elric 	    ret = 0;
    112  1.1     elric 	    break;
    113  1.1     elric 	}
    114  1.1     elric     }
    115  1.1     elric 
    116  1.1     elric     if (ret == 0) {
    117  1.1     elric 	kcm_retain_ccache(context, p);
    118  1.1     elric 	*ccache = p;
    119  1.1     elric     }
    120  1.1     elric 
    121  1.1     elric     HEIMDAL_MUTEX_unlock(&ccache_mutex);
    122  1.1     elric 
    123  1.1     elric     return ret;
    124  1.1     elric }
    125  1.1     elric 
    126  1.1     elric krb5_error_code
    127  1.1     elric kcm_ccache_get_uuids(krb5_context context, kcm_client *client, kcm_operation opcode, krb5_storage *sp)
    128  1.1     elric {
    129  1.1     elric     krb5_error_code ret;
    130  1.1     elric     kcm_ccache p;
    131  1.1     elric 
    132  1.1     elric     ret = KRB5_FCC_NOFILE;
    133  1.1     elric 
    134  1.1     elric     HEIMDAL_MUTEX_lock(&ccache_mutex);
    135  1.1     elric 
    136  1.1     elric     for (p = ccache_head; p != NULL; p = p->next) {
    137  1.1     elric 	if ((p->flags & KCM_FLAGS_VALID) == 0)
    138  1.1     elric 	    continue;
    139  1.1     elric 	ret = kcm_access(context, client, opcode, p);
    140  1.1     elric 	if (ret) {
    141  1.1     elric 	    ret = 0;
    142  1.1     elric 	    continue;
    143  1.1     elric 	}
    144  1.1     elric 	krb5_storage_write(sp, p->uuid, sizeof(p->uuid));
    145  1.1     elric     }
    146  1.1     elric 
    147  1.1     elric     HEIMDAL_MUTEX_unlock(&ccache_mutex);
    148  1.1     elric 
    149  1.1     elric     return ret;
    150  1.1     elric }
    151  1.1     elric 
    152  1.1     elric 
    153  1.1     elric krb5_error_code kcm_debug_ccache(krb5_context context)
    154  1.1     elric {
    155  1.1     elric     kcm_ccache p;
    156  1.1     elric 
    157  1.1     elric     for (p = ccache_head; p != NULL; p = p->next) {
    158  1.1     elric 	char *cpn = NULL, *spn = NULL;
    159  1.1     elric 	int ncreds = 0;
    160  1.1     elric 	struct kcm_creds *k;
    161  1.1     elric 
    162  1.1     elric 	if ((p->flags & KCM_FLAGS_VALID) == 0) {
    163  1.1     elric 	    kcm_log(7, "cache %08x: empty slot");
    164  1.1     elric 	    continue;
    165  1.1     elric 	}
    166  1.1     elric 
    167  1.1     elric 	KCM_ASSERT_VALID(p);
    168  1.1     elric 
    169  1.1     elric 	for (k = p->creds; k != NULL; k = k->next)
    170  1.1     elric 	    ncreds++;
    171  1.1     elric 
    172  1.1     elric 	if (p->client != NULL)
    173  1.1     elric 	    krb5_unparse_name(context, p->client, &cpn);
    174  1.1     elric 	if (p->server != NULL)
    175  1.1     elric 	    krb5_unparse_name(context, p->server, &spn);
    176  1.2     joerg 
    177  1.1     elric 	kcm_log(7, "cache %08x: name %s refcnt %d flags %04x mode %04o "
    178  1.1     elric 		"uid %d gid %d client %s server %s ncreds %d",
    179  1.1     elric 		p, p->name, p->refcnt, p->flags, p->mode, p->uid, p->gid,
    180  1.1     elric 		(cpn == NULL) ? "<none>" : cpn,
    181  1.1     elric 		(spn == NULL) ? "<none>" : spn,
    182  1.1     elric 		ncreds);
    183  1.1     elric 
    184  1.1     elric 	if (cpn != NULL)
    185  1.1     elric 	    free(cpn);
    186  1.1     elric 	if (spn != NULL)
    187  1.1     elric 	    free(spn);
    188  1.1     elric     }
    189  1.1     elric 
    190  1.1     elric     return 0;
    191  1.1     elric }
    192  1.1     elric 
    193  1.1     elric static void
    194  1.1     elric kcm_free_ccache_data_internal(krb5_context context,
    195  1.1     elric 			      kcm_ccache_data *cache)
    196  1.1     elric {
    197  1.1     elric     KCM_ASSERT_VALID(cache);
    198  1.1     elric 
    199  1.1     elric     if (cache->name != NULL) {
    200  1.1     elric 	free(cache->name);
    201  1.1     elric 	cache->name = NULL;
    202  1.1     elric     }
    203  1.1     elric 
    204  1.1     elric     if (cache->flags & KCM_FLAGS_USE_KEYTAB) {
    205  1.1     elric 	krb5_kt_close(context, cache->key.keytab);
    206  1.1     elric 	cache->key.keytab = NULL;
    207  1.1     elric     } else if (cache->flags & KCM_FLAGS_USE_CACHED_KEY) {
    208  1.1     elric 	krb5_free_keyblock_contents(context, &cache->key.keyblock);
    209  1.1     elric 	krb5_keyblock_zero(&cache->key.keyblock);
    210  1.1     elric     }
    211  1.1     elric 
    212  1.1     elric     cache->flags = 0;
    213  1.1     elric     cache->mode = 0;
    214  1.1     elric     cache->uid = -1;
    215  1.1     elric     cache->gid = -1;
    216  1.1     elric     cache->session = -1;
    217  1.1     elric 
    218  1.1     elric     kcm_zero_ccache_data_internal(context, cache);
    219  1.1     elric 
    220  1.1     elric     cache->tkt_life = 0;
    221  1.1     elric     cache->renew_life = 0;
    222  1.1     elric 
    223  1.1     elric     cache->next = NULL;
    224  1.1     elric     cache->refcnt = 0;
    225  1.1     elric 
    226  1.1     elric     HEIMDAL_MUTEX_unlock(&cache->mutex);
    227  1.1     elric     HEIMDAL_MUTEX_destroy(&cache->mutex);
    228  1.1     elric }
    229  1.1     elric 
    230  1.1     elric 
    231  1.1     elric krb5_error_code
    232  1.1     elric kcm_ccache_destroy(krb5_context context, const char *name)
    233  1.1     elric {
    234  1.1     elric     kcm_ccache *p, ccache;
    235  1.1     elric     krb5_error_code ret;
    236  1.1     elric 
    237  1.1     elric     ret = KRB5_FCC_NOFILE;
    238  1.1     elric 
    239  1.1     elric     HEIMDAL_MUTEX_lock(&ccache_mutex);
    240  1.1     elric     for (p = &ccache_head; *p != NULL; p = &(*p)->next) {
    241  1.1     elric 	if (((*p)->flags & KCM_FLAGS_VALID) == 0)
    242  1.1     elric 	    continue;
    243  1.1     elric 	if (strcmp((*p)->name, name) == 0) {
    244  1.1     elric 	    ret = 0;
    245  1.1     elric 	    break;
    246  1.1     elric 	}
    247  1.1     elric     }
    248  1.1     elric     if (ret)
    249  1.1     elric 	goto out;
    250  1.1     elric 
    251  1.1     elric     if ((*p)->refcnt != 1) {
    252  1.1     elric 	ret = EAGAIN;
    253  1.1     elric 	goto out;
    254  1.1     elric     }
    255  1.1     elric 
    256  1.1     elric     ccache = *p;
    257  1.1     elric     *p = (*p)->next;
    258  1.1     elric     kcm_free_ccache_data_internal(context, ccache);
    259  1.1     elric     free(ccache);
    260  1.1     elric 
    261  1.1     elric out:
    262  1.1     elric     HEIMDAL_MUTEX_unlock(&ccache_mutex);
    263  1.1     elric 
    264  1.1     elric     return ret;
    265  1.1     elric }
    266  1.1     elric 
    267  1.1     elric static krb5_error_code
    268  1.1     elric kcm_ccache_alloc(krb5_context context,
    269  1.1     elric 		 const char *name,
    270  1.1     elric 		 kcm_ccache *ccache)
    271  1.1     elric {
    272  1.1     elric     kcm_ccache slot = NULL, p;
    273  1.1     elric     krb5_error_code ret;
    274  1.1     elric     int new_slot = 0;
    275  1.1     elric 
    276  1.1     elric     *ccache = NULL;
    277  1.1     elric 
    278  1.1     elric     /* First, check for duplicates */
    279  1.1     elric     HEIMDAL_MUTEX_lock(&ccache_mutex);
    280  1.1     elric     ret = 0;
    281  1.1     elric     for (p = ccache_head; p != NULL; p = p->next) {
    282  1.1     elric 	if (p->flags & KCM_FLAGS_VALID) {
    283  1.1     elric 	    if (strcmp(p->name, name) == 0) {
    284  1.1     elric 		ret = KRB5_CC_WRITE;
    285  1.1     elric 		break;
    286  1.1     elric 	    }
    287  1.1     elric 	} else if (slot == NULL)
    288  1.1     elric 	    slot = p;
    289  1.1     elric     }
    290  1.1     elric 
    291  1.1     elric     if (ret)
    292  1.1     elric 	goto out;
    293  1.1     elric 
    294  1.1     elric     /*
    295  1.1     elric      * Create an enpty slot for us.
    296  1.1     elric      */
    297  1.1     elric     if (slot == NULL) {
    298  1.1     elric 	slot = (kcm_ccache_data *)malloc(sizeof(*slot));
    299  1.1     elric 	if (slot == NULL) {
    300  1.1     elric 	    ret = KRB5_CC_NOMEM;
    301  1.1     elric 	    goto out;
    302  1.1     elric 	}
    303  1.1     elric 	slot->next = ccache_head;
    304  1.1     elric 	HEIMDAL_MUTEX_init(&slot->mutex);
    305  1.1     elric 	new_slot = 1;
    306  1.1     elric     }
    307  1.1     elric 
    308  1.1     elric     RAND_bytes(slot->uuid, sizeof(slot->uuid));
    309  1.1     elric 
    310  1.1     elric     slot->name = strdup(name);
    311  1.1     elric     if (slot->name == NULL) {
    312  1.1     elric 	ret = KRB5_CC_NOMEM;
    313  1.1     elric 	goto out;
    314  1.1     elric     }
    315  1.1     elric 
    316  1.1     elric     slot->refcnt = 1;
    317  1.1     elric     slot->flags = KCM_FLAGS_VALID;
    318  1.1     elric     slot->mode = S_IRUSR | S_IWUSR;
    319  1.1     elric     slot->uid = -1;
    320  1.1     elric     slot->gid = -1;
    321  1.1     elric     slot->client = NULL;
    322  1.1     elric     slot->server = NULL;
    323  1.1     elric     slot->creds = NULL;
    324  1.1     elric     slot->key.keytab = NULL;
    325  1.1     elric     slot->tkt_life = 0;
    326  1.1     elric     slot->renew_life = 0;
    327  1.4  christos     slot->kdc_offset = 0;
    328  1.1     elric 
    329  1.1     elric     if (new_slot)
    330  1.1     elric 	ccache_head = slot;
    331  1.1     elric 
    332  1.1     elric     *ccache = slot;
    333  1.1     elric 
    334  1.1     elric     HEIMDAL_MUTEX_unlock(&ccache_mutex);
    335  1.1     elric     return 0;
    336  1.1     elric 
    337  1.1     elric out:
    338  1.1     elric     HEIMDAL_MUTEX_unlock(&ccache_mutex);
    339  1.1     elric     if (new_slot && slot != NULL) {
    340  1.1     elric 	HEIMDAL_MUTEX_destroy(&slot->mutex);
    341  1.1     elric 	free(slot);
    342  1.1     elric     }
    343  1.1     elric     return ret;
    344  1.1     elric }
    345  1.1     elric 
    346  1.1     elric krb5_error_code
    347  1.1     elric kcm_ccache_remove_creds_internal(krb5_context context,
    348  1.1     elric 				 kcm_ccache ccache)
    349  1.1     elric {
    350  1.1     elric     struct kcm_creds *k;
    351  1.1     elric 
    352  1.1     elric     k = ccache->creds;
    353  1.1     elric     while (k != NULL) {
    354  1.1     elric 	struct kcm_creds *old;
    355  1.1     elric 
    356  1.1     elric 	krb5_free_cred_contents(context, &k->cred);
    357  1.1     elric 	old = k;
    358  1.1     elric 	k = k->next;
    359  1.1     elric 	free(old);
    360  1.1     elric     }
    361  1.1     elric     ccache->creds = NULL;
    362  1.1     elric 
    363  1.1     elric     return 0;
    364  1.1     elric }
    365  1.1     elric 
    366  1.1     elric krb5_error_code
    367  1.1     elric kcm_ccache_remove_creds(krb5_context context,
    368  1.1     elric 			kcm_ccache ccache)
    369  1.1     elric {
    370  1.1     elric     krb5_error_code ret;
    371  1.1     elric 
    372  1.1     elric     KCM_ASSERT_VALID(ccache);
    373  1.1     elric 
    374  1.1     elric     HEIMDAL_MUTEX_lock(&ccache->mutex);
    375  1.1     elric     ret = kcm_ccache_remove_creds_internal(context, ccache);
    376  1.1     elric     HEIMDAL_MUTEX_unlock(&ccache->mutex);
    377  1.1     elric 
    378  1.1     elric     return ret;
    379  1.1     elric }
    380  1.1     elric 
    381  1.1     elric krb5_error_code
    382  1.1     elric kcm_zero_ccache_data_internal(krb5_context context,
    383  1.1     elric 			      kcm_ccache_data *cache)
    384  1.1     elric {
    385  1.1     elric     if (cache->client != NULL) {
    386  1.1     elric 	krb5_free_principal(context, cache->client);
    387  1.1     elric 	cache->client = NULL;
    388  1.1     elric     }
    389  1.1     elric 
    390  1.1     elric     if (cache->server != NULL) {
    391  1.1     elric 	krb5_free_principal(context, cache->server);
    392  1.1     elric 	cache->server = NULL;
    393  1.1     elric     }
    394  1.1     elric 
    395  1.1     elric     kcm_ccache_remove_creds_internal(context, cache);
    396  1.1     elric 
    397  1.1     elric     return 0;
    398  1.1     elric }
    399  1.1     elric 
    400  1.1     elric krb5_error_code
    401  1.1     elric kcm_zero_ccache_data(krb5_context context,
    402  1.1     elric 		     kcm_ccache cache)
    403  1.1     elric {
    404  1.1     elric     krb5_error_code ret;
    405  1.1     elric 
    406  1.1     elric     KCM_ASSERT_VALID(cache);
    407  1.1     elric 
    408  1.1     elric     HEIMDAL_MUTEX_lock(&cache->mutex);
    409  1.1     elric     ret = kcm_zero_ccache_data_internal(context, cache);
    410  1.1     elric     HEIMDAL_MUTEX_unlock(&cache->mutex);
    411  1.1     elric 
    412  1.1     elric     return ret;
    413  1.1     elric }
    414  1.1     elric 
    415  1.1     elric krb5_error_code
    416  1.1     elric kcm_retain_ccache(krb5_context context,
    417  1.1     elric 		  kcm_ccache ccache)
    418  1.1     elric {
    419  1.1     elric     KCM_ASSERT_VALID(ccache);
    420  1.1     elric 
    421  1.1     elric     HEIMDAL_MUTEX_lock(&ccache->mutex);
    422  1.1     elric     ccache->refcnt++;
    423  1.1     elric     HEIMDAL_MUTEX_unlock(&ccache->mutex);
    424  1.1     elric 
    425  1.1     elric     return 0;
    426  1.1     elric }
    427  1.1     elric 
    428  1.1     elric krb5_error_code
    429  1.1     elric kcm_release_ccache(krb5_context context, kcm_ccache c)
    430  1.1     elric {
    431  1.1     elric     krb5_error_code ret = 0;
    432  1.1     elric 
    433  1.1     elric     KCM_ASSERT_VALID(c);
    434  1.1     elric 
    435  1.1     elric     HEIMDAL_MUTEX_lock(&c->mutex);
    436  1.1     elric     if (c->refcnt == 1) {
    437  1.1     elric 	kcm_free_ccache_data_internal(context, c);
    438  1.1     elric 	free(c);
    439  1.1     elric     } else {
    440  1.1     elric 	c->refcnt--;
    441  1.1     elric 	HEIMDAL_MUTEX_unlock(&c->mutex);
    442  1.1     elric     }
    443  1.1     elric 
    444  1.1     elric     return ret;
    445  1.1     elric }
    446  1.1     elric 
    447  1.1     elric krb5_error_code
    448  1.1     elric kcm_ccache_gen_new(krb5_context context,
    449  1.1     elric 		   pid_t pid,
    450  1.1     elric 		   uid_t uid,
    451  1.1     elric 		   gid_t gid,
    452  1.1     elric 		   kcm_ccache *ccache)
    453  1.1     elric {
    454  1.1     elric     krb5_error_code ret;
    455  1.1     elric     char *name;
    456  1.1     elric 
    457  1.1     elric     name = kcm_ccache_nextid(pid, uid, gid);
    458  1.1     elric     if (name == NULL) {
    459  1.1     elric 	return KRB5_CC_NOMEM;
    460  1.1     elric     }
    461  1.1     elric 
    462  1.1     elric     ret = kcm_ccache_new(context, name, ccache);
    463  1.1     elric 
    464  1.1     elric     free(name);
    465  1.1     elric     return ret;
    466  1.1     elric }
    467  1.1     elric 
    468  1.1     elric krb5_error_code
    469  1.1     elric kcm_ccache_new(krb5_context context,
    470  1.1     elric 	       const char *name,
    471  1.1     elric 	       kcm_ccache *ccache)
    472  1.1     elric {
    473  1.1     elric     krb5_error_code ret;
    474  1.1     elric 
    475  1.1     elric     ret = kcm_ccache_alloc(context, name, ccache);
    476  1.1     elric     if (ret == 0) {
    477  1.1     elric 	/*
    478  1.1     elric 	 * one reference is held by the linked list,
    479  1.1     elric 	 * one by the caller
    480  1.1     elric 	 */
    481  1.1     elric 	kcm_retain_ccache(context, *ccache);
    482  1.1     elric     }
    483  1.1     elric 
    484  1.1     elric     return ret;
    485  1.1     elric }
    486  1.1     elric 
    487  1.1     elric krb5_error_code
    488  1.1     elric kcm_ccache_destroy_if_empty(krb5_context context,
    489  1.1     elric 			    kcm_ccache ccache)
    490  1.1     elric {
    491  1.1     elric     krb5_error_code ret;
    492  1.1     elric 
    493  1.1     elric     KCM_ASSERT_VALID(ccache);
    494  1.1     elric 
    495  1.1     elric     if (ccache->creds == NULL) {
    496  1.1     elric 	ret = kcm_ccache_destroy(context, ccache->name);
    497  1.1     elric     } else
    498  1.1     elric 	ret = 0;
    499  1.1     elric 
    500  1.1     elric     return ret;
    501  1.1     elric }
    502  1.1     elric 
    503  1.1     elric krb5_error_code
    504  1.1     elric kcm_ccache_store_cred(krb5_context context,
    505  1.1     elric 		      kcm_ccache ccache,
    506  1.1     elric 		      krb5_creds *creds,
    507  1.1     elric 		      int copy)
    508  1.1     elric {
    509  1.1     elric     krb5_error_code ret;
    510  1.1     elric     krb5_creds *tmp;
    511  1.1     elric 
    512  1.1     elric     KCM_ASSERT_VALID(ccache);
    513  1.1     elric 
    514  1.1     elric     HEIMDAL_MUTEX_lock(&ccache->mutex);
    515  1.1     elric     ret = kcm_ccache_store_cred_internal(context, ccache, creds, copy, &tmp);
    516  1.1     elric     HEIMDAL_MUTEX_unlock(&ccache->mutex);
    517  1.1     elric 
    518  1.1     elric     return ret;
    519  1.1     elric }
    520  1.1     elric 
    521  1.1     elric struct kcm_creds *
    522  1.1     elric kcm_ccache_find_cred_uuid(krb5_context context,
    523  1.1     elric 			  kcm_ccache ccache,
    524  1.1     elric 			  kcmuuid_t uuid)
    525  1.1     elric {
    526  1.1     elric     struct kcm_creds *c;
    527  1.1     elric 
    528  1.1     elric     for (c = ccache->creds; c != NULL; c = c->next)
    529  1.1     elric 	if (memcmp(c->uuid, uuid, sizeof(c->uuid)) == 0)
    530  1.1     elric 	    return c;
    531  1.1     elric 
    532  1.1     elric     return NULL;
    533  1.1     elric }
    534  1.1     elric 
    535  1.1     elric 
    536  1.1     elric 
    537  1.1     elric krb5_error_code
    538  1.1     elric kcm_ccache_store_cred_internal(krb5_context context,
    539  1.1     elric 			       kcm_ccache ccache,
    540  1.1     elric 			       krb5_creds *creds,
    541  1.1     elric 			       int copy,
    542  1.1     elric 			       krb5_creds **credp)
    543  1.1     elric {
    544  1.1     elric     struct kcm_creds **c;
    545  1.1     elric     krb5_error_code ret;
    546  1.1     elric 
    547  1.1     elric     for (c = &ccache->creds; *c != NULL; c = &(*c)->next)
    548  1.1     elric 	;
    549  1.1     elric 
    550  1.1     elric     *c = (struct kcm_creds *)calloc(1, sizeof(**c));
    551  1.1     elric     if (*c == NULL)
    552  1.1     elric 	return KRB5_CC_NOMEM;
    553  1.1     elric 
    554  1.1     elric     RAND_bytes((*c)->uuid, sizeof((*c)->uuid));
    555  1.1     elric 
    556  1.1     elric     *credp = &(*c)->cred;
    557  1.1     elric 
    558  1.1     elric     if (copy) {
    559  1.1     elric 	ret = krb5_copy_creds_contents(context, creds, *credp);
    560  1.1     elric 	if (ret) {
    561  1.1     elric 	    free(*c);
    562  1.1     elric 	    *c = NULL;
    563  1.1     elric 	}
    564  1.1     elric     } else {
    565  1.1     elric 	**credp = *creds;
    566  1.1     elric 	ret = 0;
    567  1.1     elric     }
    568  1.1     elric 
    569  1.1     elric     return ret;
    570  1.1     elric }
    571  1.1     elric 
    572  1.1     elric krb5_error_code
    573  1.1     elric kcm_ccache_remove_cred_internal(krb5_context context,
    574  1.1     elric 				kcm_ccache ccache,
    575  1.1     elric 				krb5_flags whichfields,
    576  1.1     elric 				const krb5_creds *mcreds)
    577  1.1     elric {
    578  1.1     elric     krb5_error_code ret;
    579  1.1     elric     struct kcm_creds **c;
    580  1.1     elric 
    581  1.1     elric     ret = KRB5_CC_NOTFOUND;
    582  1.1     elric 
    583  1.1     elric     for (c = &ccache->creds; *c != NULL; c = &(*c)->next) {
    584  1.1     elric 	if (krb5_compare_creds(context, whichfields, mcreds, &(*c)->cred)) {
    585  1.1     elric 	    struct kcm_creds *cred = *c;
    586  1.1     elric 
    587  1.1     elric 	    *c = cred->next;
    588  1.1     elric 	    krb5_free_cred_contents(context, &cred->cred);
    589  1.1     elric 	    free(cred);
    590  1.1     elric 	    ret = 0;
    591  1.1     elric 	    if (*c == NULL)
    592  1.1     elric 		break;
    593  1.1     elric 	}
    594  1.1     elric     }
    595  1.1     elric 
    596  1.1     elric     return ret;
    597  1.1     elric }
    598  1.1     elric 
    599  1.1     elric krb5_error_code
    600  1.1     elric kcm_ccache_remove_cred(krb5_context context,
    601  1.1     elric 		       kcm_ccache ccache,
    602  1.1     elric 		       krb5_flags whichfields,
    603  1.1     elric 		       const krb5_creds *mcreds)
    604  1.1     elric {
    605  1.1     elric     krb5_error_code ret;
    606  1.1     elric 
    607  1.1     elric     KCM_ASSERT_VALID(ccache);
    608  1.1     elric 
    609  1.1     elric     HEIMDAL_MUTEX_lock(&ccache->mutex);
    610  1.1     elric     ret = kcm_ccache_remove_cred_internal(context, ccache, whichfields, mcreds);
    611  1.1     elric     HEIMDAL_MUTEX_unlock(&ccache->mutex);
    612  1.1     elric 
    613  1.1     elric     return ret;
    614  1.1     elric }
    615  1.1     elric 
    616  1.1     elric krb5_error_code
    617  1.1     elric kcm_ccache_retrieve_cred_internal(krb5_context context,
    618  1.1     elric 			 	  kcm_ccache ccache,
    619  1.1     elric 			 	  krb5_flags whichfields,
    620  1.1     elric 			 	  const krb5_creds *mcreds,
    621  1.1     elric 			 	  krb5_creds **creds)
    622  1.1     elric {
    623  1.1     elric     krb5_boolean match;
    624  1.1     elric     struct kcm_creds *c;
    625  1.1     elric     krb5_error_code ret;
    626  1.1     elric 
    627  1.1     elric     memset(creds, 0, sizeof(*creds));
    628  1.1     elric 
    629  1.1     elric     ret = KRB5_CC_END;
    630  1.1     elric 
    631  1.1     elric     match = FALSE;
    632  1.1     elric     for (c = ccache->creds; c != NULL; c = c->next) {
    633  1.1     elric 	match = krb5_compare_creds(context, whichfields, mcreds, &c->cred);
    634  1.1     elric 	if (match)
    635  1.1     elric 	    break;
    636  1.1     elric     }
    637  1.1     elric 
    638  1.1     elric     if (match) {
    639  1.1     elric 	ret = 0;
    640  1.1     elric 	*creds = &c->cred;
    641  1.1     elric     }
    642  1.1     elric 
    643  1.1     elric     return ret;
    644  1.1     elric }
    645  1.1     elric 
    646  1.1     elric krb5_error_code
    647  1.1     elric kcm_ccache_retrieve_cred(krb5_context context,
    648  1.1     elric 			 kcm_ccache ccache,
    649  1.1     elric 			 krb5_flags whichfields,
    650  1.1     elric 			 const krb5_creds *mcreds,
    651  1.1     elric 			 krb5_creds **credp)
    652  1.1     elric {
    653  1.1     elric     krb5_error_code ret;
    654  1.1     elric 
    655  1.1     elric     KCM_ASSERT_VALID(ccache);
    656  1.1     elric 
    657  1.1     elric     HEIMDAL_MUTEX_lock(&ccache->mutex);
    658  1.1     elric     ret = kcm_ccache_retrieve_cred_internal(context, ccache,
    659  1.1     elric 					    whichfields, mcreds, credp);
    660  1.1     elric     HEIMDAL_MUTEX_unlock(&ccache->mutex);
    661  1.1     elric 
    662  1.1     elric     return ret;
    663  1.1     elric }
    664  1.1     elric 
    665  1.1     elric char *
    666  1.1     elric kcm_ccache_first_name(kcm_client *client)
    667  1.1     elric {
    668  1.1     elric     kcm_ccache p;
    669  1.1     elric     char *name = NULL;
    670  1.1     elric 
    671  1.1     elric     HEIMDAL_MUTEX_lock(&ccache_mutex);
    672  1.1     elric 
    673  1.1     elric     for (p = ccache_head; p != NULL; p = p->next) {
    674  1.1     elric 	if (kcm_is_same_session(client, p->uid, p->session))
    675  1.1     elric 	    break;
    676  1.1     elric     }
    677  1.1     elric     if (p)
    678  1.1     elric 	name = strdup(p->name);
    679  1.1     elric     HEIMDAL_MUTEX_unlock(&ccache_mutex);
    680  1.1     elric     return name;
    681  1.1     elric }
    682