2 * Copyright (c) 1997-2004 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include "krb5_locl.h"
38 typedef struct krb5_mcache {
42 krb5_principal primary_principal;
47 struct krb5_mcache *next;
49 krb5_deltat kdc_offset;
52 static HEIMDAL_MUTEX mcc_mutex = HEIMDAL_MUTEX_INITIALIZER;
53 static struct krb5_mcache *mcc_head;
55 #define MCACHE(X) ((krb5_mcache *)(X)->data.data)
57 #define MISDEAD(X) ((X)->dead)
60 mcc_get_name(krb5_context context,
63 return MCACHE(id)->name;
67 mcc_alloc(const char *name)
75 asprintf(&m->name, "%p", m);
77 m->name = strdup(name);
82 /* check for dups first */
83 HEIMDAL_MUTEX_lock(&mcc_mutex);
84 for (m_c = mcc_head; m_c != NULL; m_c = m_c->next)
85 if (strcmp(m->name, m_c->name) == 0)
90 HEIMDAL_MUTEX_unlock(&mcc_mutex);
96 m->primary_principal = NULL;
98 m->mtime = time(NULL);
102 HEIMDAL_MUTEX_unlock(&mcc_mutex);
106 static krb5_error_code
107 mcc_resolve(krb5_context context, krb5_ccache *id, const char *res)
111 HEIMDAL_MUTEX_lock(&mcc_mutex);
112 for (m = mcc_head; m != NULL; m = m->next)
113 if (strcmp(m->name, res) == 0)
115 HEIMDAL_MUTEX_unlock(&mcc_mutex);
119 (*id)->data.data = m;
120 (*id)->data.length = sizeof(*m);
126 krb5_set_error_message(context, KRB5_CC_NOMEM,
127 N_("malloc: out of memory", ""));
128 return KRB5_CC_NOMEM;
131 (*id)->data.data = m;
132 (*id)->data.length = sizeof(*m);
138 static krb5_error_code
139 mcc_gen_new(krb5_context context, krb5_ccache *id)
146 krb5_set_error_message(context, KRB5_CC_NOMEM,
147 N_("malloc: out of memory", ""));
148 return KRB5_CC_NOMEM;
151 (*id)->data.data = m;
152 (*id)->data.length = sizeof(*m);
157 static krb5_error_code
158 mcc_initialize(krb5_context context,
160 krb5_principal primary_principal)
162 krb5_mcache *m = MCACHE(id);
164 m->mtime = time(NULL);
165 return krb5_copy_principal (context,
167 &m->primary_principal);
171 mcc_close_internal(krb5_mcache *m)
173 if (--m->refcnt != 0)
183 static krb5_error_code
184 mcc_close(krb5_context context,
187 if (mcc_close_internal(MCACHE(id)))
188 krb5_data_free(&id->data);
192 static krb5_error_code
193 mcc_destroy(krb5_context context,
196 krb5_mcache **n, *m = MCACHE(id);
200 krb5_abortx(context, "mcc_destroy: refcnt already 0");
203 /* if this is an active mcache, remove it from the linked
204 list, and free all data */
205 HEIMDAL_MUTEX_lock(&mcc_mutex);
206 for(n = &mcc_head; n && *n; n = &(*n)->next) {
212 HEIMDAL_MUTEX_unlock(&mcc_mutex);
213 if (m->primary_principal != NULL) {
214 krb5_free_principal (context, m->primary_principal);
215 m->primary_principal = NULL;
223 krb5_free_cred_contents (context, &l->cred);
233 static krb5_error_code
234 mcc_store_cred(krb5_context context,
238 krb5_mcache *m = MCACHE(id);
245 l = malloc (sizeof(*l));
247 krb5_set_error_message(context, KRB5_CC_NOMEM,
248 N_("malloc: out of memory", ""));
249 return KRB5_CC_NOMEM;
253 memset (&l->cred, 0, sizeof(l->cred));
254 ret = krb5_copy_creds_contents (context, creds, &l->cred);
260 m->mtime = time(NULL);
264 static krb5_error_code
265 mcc_get_principal(krb5_context context,
267 krb5_principal *principal)
269 krb5_mcache *m = MCACHE(id);
271 if (MISDEAD(m) || m->primary_principal == NULL)
273 return krb5_copy_principal (context,
274 m->primary_principal,
278 static krb5_error_code
279 mcc_get_first (krb5_context context,
281 krb5_cc_cursor *cursor)
283 krb5_mcache *m = MCACHE(id);
292 static krb5_error_code
293 mcc_get_next (krb5_context context,
295 krb5_cc_cursor *cursor,
298 krb5_mcache *m = MCACHE(id);
307 return krb5_copy_creds_contents (context,
314 static krb5_error_code
315 mcc_end_get (krb5_context context,
317 krb5_cc_cursor *cursor)
322 static krb5_error_code
323 mcc_remove_cred(krb5_context context,
328 krb5_mcache *m = MCACHE(id);
330 for(q = &m->creds, p = *q; p; p = *q) {
331 if(krb5_compare_creds(context, which, mcreds, &p->cred)) {
333 krb5_free_cred_contents(context, &p->cred);
335 m->mtime = time(NULL);
342 static krb5_error_code
343 mcc_set_flags(krb5_context context,
354 static krb5_error_code
355 mcc_get_cache_first(krb5_context context, krb5_cc_cursor *cursor)
357 struct mcache_iter *iter;
359 iter = calloc(1, sizeof(*iter));
361 krb5_set_error_message(context, ENOMEM,
362 N_("malloc: out of memory", ""));
366 HEIMDAL_MUTEX_lock(&mcc_mutex);
367 iter->cache = mcc_head;
369 iter->cache->refcnt++;
370 HEIMDAL_MUTEX_unlock(&mcc_mutex);
376 static krb5_error_code
377 mcc_get_cache_next(krb5_context context, krb5_cc_cursor cursor, krb5_ccache *id)
379 struct mcache_iter *iter = cursor;
383 if (iter->cache == NULL)
386 HEIMDAL_MUTEX_lock(&mcc_mutex);
390 iter->cache = m->next;
391 HEIMDAL_MUTEX_unlock(&mcc_mutex);
393 ret = _krb5_cc_allocate(context, &krb5_mcc_ops, id);
397 (*id)->data.data = m;
398 (*id)->data.length = sizeof(*m);
403 static krb5_error_code
404 mcc_end_cache_get(krb5_context context, krb5_cc_cursor cursor)
406 struct mcache_iter *iter = cursor;
409 mcc_close_internal(iter->cache);
415 static krb5_error_code
416 mcc_move(krb5_context context, krb5_ccache from, krb5_ccache to)
418 krb5_mcache *mfrom = MCACHE(from), *mto = MCACHE(to);
420 krb5_principal principal;
423 HEIMDAL_MUTEX_lock(&mcc_mutex);
425 /* drop the from cache from the linked list to avoid lookups */
426 for(n = &mcc_head; n && *n; n = &(*n)->next) {
435 mto->creds = mfrom->creds;
436 mfrom->creds = creds;
438 principal = mto->primary_principal;
439 mto->primary_principal = mfrom->primary_principal;
440 mfrom->primary_principal = principal;
442 mto->mtime = mfrom->mtime = time(NULL);
444 HEIMDAL_MUTEX_unlock(&mcc_mutex);
445 mcc_destroy(context, from);
450 static krb5_error_code
451 mcc_default_name(krb5_context context, char **str)
453 *str = strdup("MEMORY:");
455 krb5_set_error_message(context, ENOMEM,
456 N_("malloc: out of memory", ""));
462 static krb5_error_code
463 mcc_lastchange(krb5_context context, krb5_ccache id, krb5_timestamp *mtime)
465 *mtime = MCACHE(id)->mtime;
469 static krb5_error_code
470 mcc_set_kdc_offset(krb5_context context, krb5_ccache id, krb5_deltat kdc_offset)
472 krb5_mcache *m = MCACHE(id);
473 m->kdc_offset = kdc_offset;
477 static krb5_error_code
478 mcc_get_kdc_offset(krb5_context context, krb5_ccache id, krb5_deltat *kdc_offset)
480 krb5_mcache *m = MCACHE(id);
481 *kdc_offset = m->kdc_offset;
487 * Variable containing the MEMORY based credential cache implemention.
489 * @ingroup krb5_ccache
492 KRB5_LIB_VARIABLE const krb5_cc_ops krb5_mcc_ops = {
502 NULL, /* mcc_retrieve */