2
* Copyright (c) 1997-2004 Kungliga Tekniska Högskolan
3
* (Royal Institute of Technology, Stockholm, Sweden).
6
* Redistribution and use in source and binary forms, with or without
7
* modification, are permitted provided that the following conditions
10
* 1. Redistributions of source code must retain the above copyright
11
* notice, this list of conditions and the following disclaimer.
13
* 2. Redistributions in binary form must reproduce the above copyright
14
* notice, this list of conditions and the following disclaimer in the
15
* documentation and/or other materials provided with the distribution.
17
* 3. Neither the name of the Institute nor the names of its contributors
18
* may be used to endorse or promote products derived from this software
19
* without specific prior written permission.
21
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34
#include "krb5_locl.h"
38
typedef struct krb5_mcache {
42
krb5_principal primary_principal;
47
struct krb5_mcache *next;
51
static HEIMDAL_MUTEX mcc_mutex = HEIMDAL_MUTEX_INITIALIZER;
52
static struct krb5_mcache *mcc_head;
54
#define MCACHE(X) ((krb5_mcache *)(X)->data.data)
56
#define MISDEAD(X) ((X)->dead)
59
mcc_get_name(krb5_context context,
62
return MCACHE(id)->name;
66
mcc_alloc(const char *name)
74
asprintf(&m->name, "%p", m);
76
m->name = strdup(name);
81
/* check for dups first */
82
HEIMDAL_MUTEX_lock(&mcc_mutex);
83
for (m_c = mcc_head; m_c != NULL; m_c = m_c->next)
84
if (strcmp(m->name, m_c->name) == 0)
89
HEIMDAL_MUTEX_unlock(&mcc_mutex);
95
m->primary_principal = NULL;
97
m->mtime = time(NULL);
100
HEIMDAL_MUTEX_unlock(&mcc_mutex);
104
static krb5_error_code
105
mcc_resolve(krb5_context context, krb5_ccache *id, const char *res)
109
HEIMDAL_MUTEX_lock(&mcc_mutex);
110
for (m = mcc_head; m != NULL; m = m->next)
111
if (strcmp(m->name, res) == 0)
113
HEIMDAL_MUTEX_unlock(&mcc_mutex);
117
(*id)->data.data = m;
118
(*id)->data.length = sizeof(*m);
124
krb5_set_error_message(context, KRB5_CC_NOMEM,
125
N_("malloc: out of memory", ""));
126
return KRB5_CC_NOMEM;
129
(*id)->data.data = m;
130
(*id)->data.length = sizeof(*m);
136
static krb5_error_code
137
mcc_gen_new(krb5_context context, krb5_ccache *id)
144
krb5_set_error_message(context, KRB5_CC_NOMEM,
145
N_("malloc: out of memory", ""));
146
return KRB5_CC_NOMEM;
149
(*id)->data.data = m;
150
(*id)->data.length = sizeof(*m);
155
static krb5_error_code
156
mcc_initialize(krb5_context context,
158
krb5_principal primary_principal)
160
krb5_mcache *m = MCACHE(id);
162
m->mtime = time(NULL);
163
return krb5_copy_principal (context,
165
&m->primary_principal);
169
mcc_close_internal(krb5_mcache *m)
171
if (--m->refcnt != 0)
181
static krb5_error_code
182
mcc_close(krb5_context context,
185
if (mcc_close_internal(MCACHE(id)))
186
krb5_data_free(&id->data);
190
static krb5_error_code
191
mcc_destroy(krb5_context context,
194
krb5_mcache **n, *m = MCACHE(id);
198
krb5_abortx(context, "mcc_destroy: refcnt already 0");
201
/* if this is an active mcache, remove it from the linked
202
list, and free all data */
203
HEIMDAL_MUTEX_lock(&mcc_mutex);
204
for(n = &mcc_head; n && *n; n = &(*n)->next) {
210
HEIMDAL_MUTEX_unlock(&mcc_mutex);
211
if (m->primary_principal != NULL) {
212
krb5_free_principal (context, m->primary_principal);
213
m->primary_principal = NULL;
221
krb5_free_cred_contents (context, &l->cred);
231
static krb5_error_code
232
mcc_store_cred(krb5_context context,
236
krb5_mcache *m = MCACHE(id);
243
l = malloc (sizeof(*l));
245
krb5_set_error_message(context, KRB5_CC_NOMEM,
246
N_("malloc: out of memory", ""));
247
return KRB5_CC_NOMEM;
251
memset (&l->cred, 0, sizeof(l->cred));
252
ret = krb5_copy_creds_contents (context, creds, &l->cred);
258
m->mtime = time(NULL);
262
static krb5_error_code
263
mcc_get_principal(krb5_context context,
265
krb5_principal *principal)
267
krb5_mcache *m = MCACHE(id);
269
if (MISDEAD(m) || m->primary_principal == NULL)
271
return krb5_copy_principal (context,
272
m->primary_principal,
276
static krb5_error_code
277
mcc_get_first (krb5_context context,
279
krb5_cc_cursor *cursor)
281
krb5_mcache *m = MCACHE(id);
290
static krb5_error_code
291
mcc_get_next (krb5_context context,
293
krb5_cc_cursor *cursor,
296
krb5_mcache *m = MCACHE(id);
305
return krb5_copy_creds_contents (context,
312
static krb5_error_code
313
mcc_end_get (krb5_context context,
315
krb5_cc_cursor *cursor)
320
static krb5_error_code
321
mcc_remove_cred(krb5_context context,
326
krb5_mcache *m = MCACHE(id);
328
for(q = &m->creds, p = *q; p; p = *q) {
329
if(krb5_compare_creds(context, which, mcreds, &p->cred)) {
331
krb5_free_cred_contents(context, &p->cred);
333
m->mtime = time(NULL);
340
static krb5_error_code
341
mcc_set_flags(krb5_context context,
352
static krb5_error_code
353
mcc_get_cache_first(krb5_context context, krb5_cc_cursor *cursor)
355
struct mcache_iter *iter;
357
iter = calloc(1, sizeof(*iter));
359
krb5_set_error_message(context, ENOMEM,
360
N_("malloc: out of memory", ""));
364
HEIMDAL_MUTEX_lock(&mcc_mutex);
365
iter->cache = mcc_head;
367
iter->cache->refcnt++;
368
HEIMDAL_MUTEX_unlock(&mcc_mutex);
374
static krb5_error_code
375
mcc_get_cache_next(krb5_context context, krb5_cc_cursor cursor, krb5_ccache *id)
377
struct mcache_iter *iter = cursor;
381
if (iter->cache == NULL)
384
HEIMDAL_MUTEX_lock(&mcc_mutex);
388
iter->cache = m->next;
389
HEIMDAL_MUTEX_unlock(&mcc_mutex);
391
ret = _krb5_cc_allocate(context, &krb5_mcc_ops, id);
395
(*id)->data.data = m;
396
(*id)->data.length = sizeof(*m);
401
static krb5_error_code
402
mcc_end_cache_get(krb5_context context, krb5_cc_cursor cursor)
404
struct mcache_iter *iter = cursor;
407
mcc_close_internal(iter->cache);
413
static krb5_error_code
414
mcc_move(krb5_context context, krb5_ccache from, krb5_ccache to)
416
krb5_mcache *mfrom = MCACHE(from), *mto = MCACHE(to);
418
krb5_principal principal;
421
HEIMDAL_MUTEX_lock(&mcc_mutex);
423
/* drop the from cache from the linked list to avoid lookups */
424
for(n = &mcc_head; n && *n; n = &(*n)->next) {
433
mto->creds = mfrom->creds;
434
mfrom->creds = creds;
436
principal = mto->primary_principal;
437
mto->primary_principal = mfrom->primary_principal;
438
mfrom->primary_principal = principal;
440
mto->mtime = mfrom->mtime = time(NULL);
442
HEIMDAL_MUTEX_unlock(&mcc_mutex);
443
mcc_destroy(context, from);
448
static krb5_error_code
449
mcc_default_name(krb5_context context, char **str)
451
*str = strdup("MEMORY:");
453
krb5_set_error_message(context, ENOMEM,
454
N_("malloc: out of memory", ""));
460
static krb5_error_code
461
mcc_lastchange(krb5_context context, krb5_ccache id, krb5_timestamp *mtime)
463
*mtime = MCACHE(id)->mtime;
469
* Variable containing the MEMORY based credential cache implemention.
471
* @ingroup krb5_ccache
474
KRB5_LIB_VARIABLE const krb5_cc_ops krb5_mcc_ops = {
484
NULL, /* mcc_retrieve */