2 * Copyright (c) 1997-2002 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"
36 RCSID("$Id: mcache.c,v 1.15 2002/04/18 09:40:33 joda Exp $");
38 typedef struct krb5_mcache {
41 krb5_principal primary_principal;
46 struct krb5_mcache *next;
49 static struct krb5_mcache *mcc_head;
51 #define MCACHE(X) ((krb5_mcache *)(X)->data.data)
53 #define MISDEAD(X) ((X)->primary_principal == NULL)
55 #define MCC_CURSOR(C) ((struct link*)(C))
58 mcc_get_name(krb5_context context,
61 return MCACHE(id)->name;
65 mcc_alloc(const char *name)
73 asprintf(&m->name, "%p", m);
75 m->name = strdup(name);
81 m->primary_principal = NULL;
88 static krb5_error_code
89 mcc_resolve(krb5_context context, krb5_ccache *id, const char *res)
93 for (m = mcc_head; m != NULL; m = m->next)
94 if (strcmp(m->name, res) == 0)
100 (*id)->data.length = sizeof(*m);
106 krb5_set_error_string (context, "malloc: out of memory");
107 return KRB5_CC_NOMEM;
110 (*id)->data.data = m;
111 (*id)->data.length = sizeof(*m);
117 static krb5_error_code
118 mcc_gen_new(krb5_context context, krb5_ccache *id)
125 krb5_set_error_string (context, "malloc: out of memory");
126 return KRB5_CC_NOMEM;
129 (*id)->data.data = m;
130 (*id)->data.length = sizeof(*m);
135 static krb5_error_code
136 mcc_initialize(krb5_context context,
138 krb5_principal primary_principal)
140 return krb5_copy_principal (context,
142 &MCACHE(id)->primary_principal);
145 static krb5_error_code
146 mcc_close(krb5_context context,
149 krb5_mcache *m = MCACHE(id);
151 if (--m->refcnt != 0)
156 krb5_data_free(&id->data);
162 static krb5_error_code
163 mcc_destroy(krb5_context context,
166 krb5_mcache **n, *m = MCACHE(id);
170 krb5_abortx(context, "mcc_destroy: refcnt already 0");
173 /* if this is an active mcache, remove it from the linked
174 list, and free all data */
175 for(n = &mcc_head; n && *n; n = &(*n)->next) {
181 krb5_free_principal (context, m->primary_principal);
182 m->primary_principal = NULL;
188 krb5_free_creds_contents (context, &l->cred);
198 static krb5_error_code
199 mcc_store_cred(krb5_context context,
203 krb5_mcache *m = MCACHE(id);
210 l = malloc (sizeof(*l));
212 krb5_set_error_string (context, "malloc: out of memory");
213 return KRB5_CC_NOMEM;
217 memset (&l->cred, 0, sizeof(l->cred));
218 ret = krb5_copy_creds_contents (context, creds, &l->cred);
227 static krb5_error_code
228 mcc_get_principal(krb5_context context,
230 krb5_principal *principal)
232 krb5_mcache *m = MCACHE(id);
237 return krb5_copy_principal (context,
238 m->primary_principal,
242 static krb5_error_code
243 mcc_get_first (krb5_context context,
245 krb5_cc_cursor *cursor)
247 krb5_mcache *m = MCACHE(id);
256 static krb5_error_code
257 mcc_get_next (krb5_context context,
259 krb5_cc_cursor *cursor,
262 krb5_mcache *m = MCACHE(id);
271 return krb5_copy_creds_contents (context,
278 static krb5_error_code
279 mcc_end_get (krb5_context context,
281 krb5_cc_cursor *cursor)
286 static krb5_error_code
287 mcc_remove_cred(krb5_context context,
292 krb5_mcache *m = MCACHE(id);
294 for(q = &m->creds, p = *q; p; p = *q) {
295 if(krb5_compare_creds(context, which, mcreds, &p->cred)) {
297 krb5_free_creds_contents(context, &p->cred);
305 static krb5_error_code
306 mcc_set_flags(krb5_context context,
313 const krb5_cc_ops krb5_mcc_ops = {
322 NULL, /* mcc_retrieve */