xref: /netbsd-src/crypto/external/bsd/heimdal/dist/lib/krb5/mcache.c (revision afab4e300d3a9fb07dd8c80daf53d0feb3345706)
1*afab4e30Schristos /*	$NetBSD: mcache.c,v 1.3 2023/06/19 21:41:44 christos Exp $	*/
2ca1c9b0cSelric 
3ca1c9b0cSelric /*
4ca1c9b0cSelric  * Copyright (c) 1997-2004 Kungliga Tekniska Högskolan
5ca1c9b0cSelric  * (Royal Institute of Technology, Stockholm, Sweden).
6ca1c9b0cSelric  * All rights reserved.
7ca1c9b0cSelric  *
8ca1c9b0cSelric  * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
9ca1c9b0cSelric  *
10ca1c9b0cSelric  * Redistribution and use in source and binary forms, with or without
11ca1c9b0cSelric  * modification, are permitted provided that the following conditions
12ca1c9b0cSelric  * are met:
13ca1c9b0cSelric  *
14ca1c9b0cSelric  * 1. Redistributions of source code must retain the above copyright
15ca1c9b0cSelric  *    notice, this list of conditions and the following disclaimer.
16ca1c9b0cSelric  *
17ca1c9b0cSelric  * 2. Redistributions in binary form must reproduce the above copyright
18ca1c9b0cSelric  *    notice, this list of conditions and the following disclaimer in the
19ca1c9b0cSelric  *    documentation and/or other materials provided with the distribution.
20ca1c9b0cSelric  *
21ca1c9b0cSelric  * 3. Neither the name of the Institute nor the names of its contributors
22ca1c9b0cSelric  *    may be used to endorse or promote products derived from this software
23ca1c9b0cSelric  *    without specific prior written permission.
24ca1c9b0cSelric  *
25ca1c9b0cSelric  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26ca1c9b0cSelric  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27ca1c9b0cSelric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28ca1c9b0cSelric  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29ca1c9b0cSelric  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30ca1c9b0cSelric  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31ca1c9b0cSelric  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32ca1c9b0cSelric  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33ca1c9b0cSelric  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34ca1c9b0cSelric  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35ca1c9b0cSelric  * SUCH DAMAGE.
36ca1c9b0cSelric  */
37ca1c9b0cSelric 
38ca1c9b0cSelric #include "krb5_locl.h"
39ca1c9b0cSelric 
40ca1c9b0cSelric typedef struct krb5_mcache {
41ca1c9b0cSelric     char *name;
42ca1c9b0cSelric     unsigned int refcnt;
43ca1c9b0cSelric     int dead;
44ca1c9b0cSelric     krb5_principal primary_principal;
45ca1c9b0cSelric     struct link {
46ca1c9b0cSelric 	krb5_creds cred;
47ca1c9b0cSelric 	struct link *next;
48ca1c9b0cSelric     } *creds;
49ca1c9b0cSelric     struct krb5_mcache *next;
50ca1c9b0cSelric     time_t mtime;
51ca1c9b0cSelric     krb5_deltat kdc_offset;
52b9d004c6Schristos     HEIMDAL_MUTEX mutex;
53ca1c9b0cSelric } krb5_mcache;
54ca1c9b0cSelric 
55ca1c9b0cSelric static HEIMDAL_MUTEX mcc_mutex = HEIMDAL_MUTEX_INITIALIZER;
56ca1c9b0cSelric static struct krb5_mcache *mcc_head;
57ca1c9b0cSelric 
58ca1c9b0cSelric #define	MCACHE(X)	((krb5_mcache *)(X)->data.data)
59ca1c9b0cSelric 
60ca1c9b0cSelric #define MISDEAD(X)	((X)->dead)
61ca1c9b0cSelric 
62ca1c9b0cSelric static const char* KRB5_CALLCONV
mcc_get_name(krb5_context context,krb5_ccache id)63ca1c9b0cSelric mcc_get_name(krb5_context context,
64ca1c9b0cSelric 	     krb5_ccache id)
65ca1c9b0cSelric {
66ca1c9b0cSelric     return MCACHE(id)->name;
67ca1c9b0cSelric }
68ca1c9b0cSelric 
69ca1c9b0cSelric static krb5_mcache * KRB5_CALLCONV
mcc_alloc(const char * name)70ca1c9b0cSelric mcc_alloc(const char *name)
71ca1c9b0cSelric {
72ca1c9b0cSelric     krb5_mcache *m, *m_c;
73ca1c9b0cSelric     int ret = 0;
74ca1c9b0cSelric 
75ca1c9b0cSelric     ALLOC(m, 1);
76ca1c9b0cSelric     if(m == NULL)
77ca1c9b0cSelric 	return NULL;
78ca1c9b0cSelric     if(name == NULL)
79ca1c9b0cSelric 	ret = asprintf(&m->name, "%p", m);
80ca1c9b0cSelric     else
81ca1c9b0cSelric 	m->name = strdup(name);
82ca1c9b0cSelric     if(ret < 0 || m->name == NULL) {
83ca1c9b0cSelric 	free(m);
84ca1c9b0cSelric 	return NULL;
85ca1c9b0cSelric     }
86ca1c9b0cSelric     /* check for dups first */
87ca1c9b0cSelric     HEIMDAL_MUTEX_lock(&mcc_mutex);
88ca1c9b0cSelric     for (m_c = mcc_head; m_c != NULL; m_c = m_c->next)
89ca1c9b0cSelric 	if (strcmp(m->name, m_c->name) == 0)
90ca1c9b0cSelric 	    break;
91ca1c9b0cSelric     if (m_c) {
92ca1c9b0cSelric 	free(m->name);
93ca1c9b0cSelric 	free(m);
94ca1c9b0cSelric 	HEIMDAL_MUTEX_unlock(&mcc_mutex);
95ca1c9b0cSelric 	return NULL;
96ca1c9b0cSelric     }
97ca1c9b0cSelric 
98ca1c9b0cSelric     m->dead = 0;
99ca1c9b0cSelric     m->refcnt = 1;
100ca1c9b0cSelric     m->primary_principal = NULL;
101ca1c9b0cSelric     m->creds = NULL;
102ca1c9b0cSelric     m->mtime = time(NULL);
103ca1c9b0cSelric     m->kdc_offset = 0;
104ca1c9b0cSelric     m->next = mcc_head;
105b9d004c6Schristos     HEIMDAL_MUTEX_init(&(m->mutex));
106ca1c9b0cSelric     mcc_head = m;
107ca1c9b0cSelric     HEIMDAL_MUTEX_unlock(&mcc_mutex);
108ca1c9b0cSelric     return m;
109ca1c9b0cSelric }
110ca1c9b0cSelric 
111ca1c9b0cSelric static krb5_error_code KRB5_CALLCONV
mcc_resolve(krb5_context context,krb5_ccache * id,const char * res)112ca1c9b0cSelric mcc_resolve(krb5_context context, krb5_ccache *id, const char *res)
113ca1c9b0cSelric {
114ca1c9b0cSelric     krb5_mcache *m;
115ca1c9b0cSelric 
116ca1c9b0cSelric     HEIMDAL_MUTEX_lock(&mcc_mutex);
117ca1c9b0cSelric     for (m = mcc_head; m != NULL; m = m->next)
118ca1c9b0cSelric 	if (strcmp(m->name, res) == 0)
119ca1c9b0cSelric 	    break;
120ca1c9b0cSelric     HEIMDAL_MUTEX_unlock(&mcc_mutex);
121ca1c9b0cSelric 
122ca1c9b0cSelric     if (m != NULL) {
123b9d004c6Schristos     	HEIMDAL_MUTEX_lock(&(m->mutex));
124ca1c9b0cSelric     	m->refcnt++;
125b9d004c6Schristos     	HEIMDAL_MUTEX_unlock(&(m->mutex));
126ca1c9b0cSelric     	(*id)->data.data = m;
127ca1c9b0cSelric     	(*id)->data.length = sizeof(*m);
128ca1c9b0cSelric     	return 0;
129ca1c9b0cSelric     }
130ca1c9b0cSelric 
131ca1c9b0cSelric     m = mcc_alloc(res);
132ca1c9b0cSelric     if (m == NULL) {
133ca1c9b0cSelric 	krb5_set_error_message(context, KRB5_CC_NOMEM,
134ca1c9b0cSelric 			       N_("malloc: out of memory", ""));
135ca1c9b0cSelric 	return KRB5_CC_NOMEM;
136ca1c9b0cSelric     }
137ca1c9b0cSelric 
138ca1c9b0cSelric     (*id)->data.data = m;
139ca1c9b0cSelric     (*id)->data.length = sizeof(*m);
140ca1c9b0cSelric 
141ca1c9b0cSelric     return 0;
142ca1c9b0cSelric }
143ca1c9b0cSelric 
144ca1c9b0cSelric 
145ca1c9b0cSelric static krb5_error_code KRB5_CALLCONV
mcc_gen_new(krb5_context context,krb5_ccache * id)146ca1c9b0cSelric mcc_gen_new(krb5_context context, krb5_ccache *id)
147ca1c9b0cSelric {
148ca1c9b0cSelric     krb5_mcache *m;
149ca1c9b0cSelric 
150ca1c9b0cSelric     m = mcc_alloc(NULL);
151ca1c9b0cSelric 
152ca1c9b0cSelric     if (m == NULL) {
153ca1c9b0cSelric 	krb5_set_error_message(context, KRB5_CC_NOMEM,
154ca1c9b0cSelric 			       N_("malloc: out of memory", ""));
155ca1c9b0cSelric 	return KRB5_CC_NOMEM;
156ca1c9b0cSelric     }
157ca1c9b0cSelric 
158ca1c9b0cSelric     (*id)->data.data = m;
159ca1c9b0cSelric     (*id)->data.length = sizeof(*m);
160ca1c9b0cSelric 
161ca1c9b0cSelric     return 0;
162ca1c9b0cSelric }
163ca1c9b0cSelric 
164b9d004c6Schristos static void KRB5_CALLCONV
mcc_destroy_internal(krb5_context context,krb5_mcache * m)165b9d004c6Schristos mcc_destroy_internal(krb5_context context,
166b9d004c6Schristos 		     krb5_mcache *m)
167ca1c9b0cSelric {
168ca1c9b0cSelric     struct link *l;
169ca1c9b0cSelric 
170ca1c9b0cSelric     if (m->primary_principal != NULL) {
171ca1c9b0cSelric 	krb5_free_principal (context, m->primary_principal);
172ca1c9b0cSelric 	m->primary_principal = NULL;
173ca1c9b0cSelric     }
174ca1c9b0cSelric     m->dead = 1;
175ca1c9b0cSelric 
176ca1c9b0cSelric     l = m->creds;
177ca1c9b0cSelric     while (l != NULL) {
178ca1c9b0cSelric 	struct link *old;
179ca1c9b0cSelric 
180ca1c9b0cSelric 	krb5_free_cred_contents (context, &l->cred);
181ca1c9b0cSelric 	old = l;
182ca1c9b0cSelric 	l = l->next;
183ca1c9b0cSelric 	free (old);
184ca1c9b0cSelric     }
185b9d004c6Schristos 
186ca1c9b0cSelric     m->creds = NULL;
187b9d004c6Schristos     return;
188ca1c9b0cSelric }
189b9d004c6Schristos 
190b9d004c6Schristos static krb5_error_code KRB5_CALLCONV
mcc_initialize(krb5_context context,krb5_ccache id,krb5_principal primary_principal)191b9d004c6Schristos mcc_initialize(krb5_context context,
192b9d004c6Schristos 	       krb5_ccache id,
193b9d004c6Schristos 	       krb5_principal primary_principal)
194b9d004c6Schristos {
195b9d004c6Schristos     krb5_mcache *m = MCACHE(id);
196b9d004c6Schristos     krb5_error_code ret = 0;
197b9d004c6Schristos     HEIMDAL_MUTEX_lock(&(m->mutex));
198b9d004c6Schristos     heim_assert(m->refcnt != 0, "resurection released mcache");
199b9d004c6Schristos     /*
200b9d004c6Schristos      * It's important to destroy any existing
201b9d004c6Schristos      * creds here, that matches the baheviour
202b9d004c6Schristos      * of all other backends and also the
203b9d004c6Schristos      * MEMORY: backend in MIT.
204b9d004c6Schristos      */
205b9d004c6Schristos     mcc_destroy_internal(context, m);
206b9d004c6Schristos     m->dead = 0;
207b9d004c6Schristos     m->kdc_offset = 0;
208b9d004c6Schristos     m->mtime = time(NULL);
209b9d004c6Schristos     ret = krb5_copy_principal (context,
210b9d004c6Schristos 			       primary_principal,
211b9d004c6Schristos 			       &m->primary_principal);
212b9d004c6Schristos     HEIMDAL_MUTEX_unlock(&(m->mutex));
213b9d004c6Schristos     return ret;
214b9d004c6Schristos }
215b9d004c6Schristos 
216b9d004c6Schristos static int
mcc_close_internal(krb5_mcache * m)217b9d004c6Schristos mcc_close_internal(krb5_mcache *m)
218b9d004c6Schristos {
219b9d004c6Schristos     HEIMDAL_MUTEX_lock(&(m->mutex));
220b9d004c6Schristos     heim_assert(m->refcnt != 0, "closed dead cache mcache");
221b9d004c6Schristos     if (--m->refcnt != 0) {
222b9d004c6Schristos 	HEIMDAL_MUTEX_unlock(&(m->mutex));
223b9d004c6Schristos 	return 0;
224b9d004c6Schristos     }
225b9d004c6Schristos     if (MISDEAD(m)) {
226b9d004c6Schristos 	free (m->name);
227b9d004c6Schristos 	HEIMDAL_MUTEX_unlock(&(m->mutex));
228b9d004c6Schristos 	return 1;
229b9d004c6Schristos     }
230b9d004c6Schristos     HEIMDAL_MUTEX_unlock(&(m->mutex));
231b9d004c6Schristos     return 0;
232b9d004c6Schristos }
233b9d004c6Schristos 
234b9d004c6Schristos static krb5_error_code KRB5_CALLCONV
mcc_close(krb5_context context,krb5_ccache id)235b9d004c6Schristos mcc_close(krb5_context context,
236b9d004c6Schristos 	  krb5_ccache id)
237b9d004c6Schristos {
238b9d004c6Schristos     krb5_mcache *m = MCACHE(id);
239b9d004c6Schristos 
240b9d004c6Schristos     if (mcc_close_internal(MCACHE(id))) {
241b9d004c6Schristos 	HEIMDAL_MUTEX_destroy(&(m->mutex));
242b9d004c6Schristos 	krb5_data_free(&id->data);
243b9d004c6Schristos     }
244b9d004c6Schristos     return 0;
245b9d004c6Schristos }
246b9d004c6Schristos 
247b9d004c6Schristos static krb5_error_code KRB5_CALLCONV
mcc_destroy(krb5_context context,krb5_ccache id)248b9d004c6Schristos mcc_destroy(krb5_context context,
249b9d004c6Schristos 	    krb5_ccache id)
250b9d004c6Schristos {
251b9d004c6Schristos     krb5_mcache **n, *m = MCACHE(id);
252b9d004c6Schristos 
253*afab4e30Schristos     HEIMDAL_MUTEX_lock(&mcc_mutex);
254b9d004c6Schristos     HEIMDAL_MUTEX_lock(&(m->mutex));
255b9d004c6Schristos     if (m->refcnt == 0)
256b9d004c6Schristos     {
257b9d004c6Schristos     	HEIMDAL_MUTEX_unlock(&(m->mutex));
258*afab4e30Schristos 	HEIMDAL_MUTEX_unlock(&mcc_mutex);
259b9d004c6Schristos     	krb5_abortx(context, "mcc_destroy: refcnt already 0");
260b9d004c6Schristos     }
261b9d004c6Schristos 
262b9d004c6Schristos     if (!MISDEAD(m)) {
263b9d004c6Schristos 	/* if this is an active mcache, remove it from the linked
264b9d004c6Schristos            list, and free all data */
265b9d004c6Schristos 	for(n = &mcc_head; n && *n; n = &(*n)->next) {
266b9d004c6Schristos 	    if(m == *n) {
267b9d004c6Schristos 		*n = m->next;
268b9d004c6Schristos 		break;
269b9d004c6Schristos 	    }
270b9d004c6Schristos 	}
271b9d004c6Schristos 	mcc_destroy_internal(context, m);
272b9d004c6Schristos     }
273b9d004c6Schristos     HEIMDAL_MUTEX_unlock(&(m->mutex));
274*afab4e30Schristos     HEIMDAL_MUTEX_unlock(&mcc_mutex);
275ca1c9b0cSelric     return 0;
276ca1c9b0cSelric }
277ca1c9b0cSelric 
278ca1c9b0cSelric static krb5_error_code KRB5_CALLCONV
mcc_store_cred(krb5_context context,krb5_ccache id,krb5_creds * creds)279ca1c9b0cSelric mcc_store_cred(krb5_context context,
280ca1c9b0cSelric 	       krb5_ccache id,
281ca1c9b0cSelric 	       krb5_creds *creds)
282ca1c9b0cSelric {
283ca1c9b0cSelric     krb5_mcache *m = MCACHE(id);
284ca1c9b0cSelric     krb5_error_code ret;
285ca1c9b0cSelric     struct link *l;
286ca1c9b0cSelric 
287b9d004c6Schristos     HEIMDAL_MUTEX_lock(&(m->mutex));
288ca1c9b0cSelric     if (MISDEAD(m))
289b9d004c6Schristos     {
290b9d004c6Schristos     	HEIMDAL_MUTEX_unlock(&(m->mutex));
291ca1c9b0cSelric     	return ENOENT;
292b9d004c6Schristos     }
293ca1c9b0cSelric 
294ca1c9b0cSelric     l = malloc (sizeof(*l));
295ca1c9b0cSelric     if (l == NULL) {
296ca1c9b0cSelric     	krb5_set_error_message(context, KRB5_CC_NOMEM,
297ca1c9b0cSelric     			N_("malloc: out of memory", ""));
298b9d004c6Schristos     	HEIMDAL_MUTEX_unlock(&(m->mutex));
299ca1c9b0cSelric     	return KRB5_CC_NOMEM;
300ca1c9b0cSelric     }
301ca1c9b0cSelric     l->next = m->creds;
302ca1c9b0cSelric     m->creds = l;
303ca1c9b0cSelric     memset (&l->cred, 0, sizeof(l->cred));
304ca1c9b0cSelric     ret = krb5_copy_creds_contents (context, creds, &l->cred);
305ca1c9b0cSelric     if (ret) {
306ca1c9b0cSelric     	m->creds = l->next;
307ca1c9b0cSelric     	free (l);
308b9d004c6Schristos     	HEIMDAL_MUTEX_unlock(&(m->mutex));
309ca1c9b0cSelric     	return ret;
310ca1c9b0cSelric     }
311ca1c9b0cSelric     m->mtime = time(NULL);
312b9d004c6Schristos 	HEIMDAL_MUTEX_unlock(&(m->mutex));
313ca1c9b0cSelric     return 0;
314ca1c9b0cSelric }
315ca1c9b0cSelric 
316ca1c9b0cSelric static krb5_error_code KRB5_CALLCONV
mcc_get_principal(krb5_context context,krb5_ccache id,krb5_principal * principal)317ca1c9b0cSelric mcc_get_principal(krb5_context context,
318ca1c9b0cSelric 		  krb5_ccache id,
319ca1c9b0cSelric 		  krb5_principal *principal)
320ca1c9b0cSelric {
321ca1c9b0cSelric     krb5_mcache *m = MCACHE(id);
322b9d004c6Schristos     krb5_error_code ret = 0;
323ca1c9b0cSelric 
324b9d004c6Schristos     HEIMDAL_MUTEX_lock(&(m->mutex));
325b9d004c6Schristos     if (MISDEAD(m) || m->primary_principal == NULL) {
326b9d004c6Schristos 	HEIMDAL_MUTEX_unlock(&(m->mutex));
327ca1c9b0cSelric 	return ENOENT;
328b9d004c6Schristos     }
329b9d004c6Schristos     ret = krb5_copy_principal (context,
330ca1c9b0cSelric 			       m->primary_principal,
331ca1c9b0cSelric 			       principal);
332b9d004c6Schristos     HEIMDAL_MUTEX_unlock(&(m->mutex));
333b9d004c6Schristos     return ret;
334ca1c9b0cSelric }
335ca1c9b0cSelric 
336ca1c9b0cSelric static krb5_error_code KRB5_CALLCONV
mcc_get_first(krb5_context context,krb5_ccache id,krb5_cc_cursor * cursor)337ca1c9b0cSelric mcc_get_first (krb5_context context,
338ca1c9b0cSelric 		krb5_ccache id,
339ca1c9b0cSelric 		krb5_cc_cursor *cursor)
340ca1c9b0cSelric {
341ca1c9b0cSelric     krb5_mcache *m = MCACHE(id);
342ca1c9b0cSelric 
343b9d004c6Schristos     HEIMDAL_MUTEX_lock(&(m->mutex));
344b9d004c6Schristos     if (MISDEAD(m)) {
345b9d004c6Schristos 	HEIMDAL_MUTEX_unlock(&(m->mutex));
346ca1c9b0cSelric 	return ENOENT;
347b9d004c6Schristos     }
348ca1c9b0cSelric     *cursor = m->creds;
349b9d004c6Schristos 
350b9d004c6Schristos     HEIMDAL_MUTEX_unlock(&(m->mutex));
351ca1c9b0cSelric     return 0;
352ca1c9b0cSelric }
353ca1c9b0cSelric 
354ca1c9b0cSelric static krb5_error_code KRB5_CALLCONV
mcc_get_next(krb5_context context,krb5_ccache id,krb5_cc_cursor * cursor,krb5_creds * creds)355ca1c9b0cSelric mcc_get_next (krb5_context context,
356ca1c9b0cSelric 	      krb5_ccache id,
357ca1c9b0cSelric 	      krb5_cc_cursor *cursor,
358ca1c9b0cSelric 	      krb5_creds *creds)
359ca1c9b0cSelric {
360ca1c9b0cSelric     krb5_mcache *m = MCACHE(id);
361ca1c9b0cSelric     struct link *l;
362ca1c9b0cSelric 
363b9d004c6Schristos     HEIMDAL_MUTEX_lock(&(m->mutex));
364b9d004c6Schristos     if (MISDEAD(m)) {
365b9d004c6Schristos 	HEIMDAL_MUTEX_unlock(&(m->mutex));
366ca1c9b0cSelric 	return ENOENT;
367b9d004c6Schristos     }
368b9d004c6Schristos     HEIMDAL_MUTEX_unlock(&(m->mutex));
369ca1c9b0cSelric 
370ca1c9b0cSelric     l = *cursor;
371ca1c9b0cSelric     if (l != NULL) {
372ca1c9b0cSelric 	*cursor = l->next;
373ca1c9b0cSelric 	return krb5_copy_creds_contents (context,
374ca1c9b0cSelric 					 &l->cred,
375ca1c9b0cSelric 					 creds);
376ca1c9b0cSelric     } else
377ca1c9b0cSelric 	return KRB5_CC_END;
378ca1c9b0cSelric }
379ca1c9b0cSelric 
380ca1c9b0cSelric static krb5_error_code KRB5_CALLCONV
mcc_end_get(krb5_context context,krb5_ccache id,krb5_cc_cursor * cursor)381ca1c9b0cSelric mcc_end_get (krb5_context context,
382ca1c9b0cSelric 	     krb5_ccache id,
383ca1c9b0cSelric 	     krb5_cc_cursor *cursor)
384ca1c9b0cSelric {
385ca1c9b0cSelric     return 0;
386ca1c9b0cSelric }
387ca1c9b0cSelric 
388ca1c9b0cSelric static krb5_error_code KRB5_CALLCONV
mcc_remove_cred(krb5_context context,krb5_ccache id,krb5_flags which,krb5_creds * mcreds)389ca1c9b0cSelric mcc_remove_cred(krb5_context context,
390ca1c9b0cSelric 		 krb5_ccache id,
391ca1c9b0cSelric 		 krb5_flags which,
392ca1c9b0cSelric 		 krb5_creds *mcreds)
393ca1c9b0cSelric {
394ca1c9b0cSelric     krb5_mcache *m = MCACHE(id);
395ca1c9b0cSelric     struct link **q, *p;
396b9d004c6Schristos 
397b9d004c6Schristos     HEIMDAL_MUTEX_lock(&(m->mutex));
398b9d004c6Schristos 
399ca1c9b0cSelric     for(q = &m->creds, p = *q; p; p = *q) {
400ca1c9b0cSelric 	if(krb5_compare_creds(context, which, mcreds, &p->cred)) {
401ca1c9b0cSelric 	    *q = p->next;
402ca1c9b0cSelric 	    krb5_free_cred_contents(context, &p->cred);
403ca1c9b0cSelric 	    free(p);
404ca1c9b0cSelric 	    m->mtime = time(NULL);
405ca1c9b0cSelric 	} else
406ca1c9b0cSelric 	    q = &p->next;
407ca1c9b0cSelric     }
408b9d004c6Schristos     HEIMDAL_MUTEX_unlock(&(m->mutex));
409ca1c9b0cSelric     return 0;
410ca1c9b0cSelric }
411ca1c9b0cSelric 
412ca1c9b0cSelric static krb5_error_code KRB5_CALLCONV
mcc_set_flags(krb5_context context,krb5_ccache id,krb5_flags flags)413ca1c9b0cSelric mcc_set_flags(krb5_context context,
414ca1c9b0cSelric 	      krb5_ccache id,
415ca1c9b0cSelric 	      krb5_flags flags)
416ca1c9b0cSelric {
417ca1c9b0cSelric     return 0; /* XXX */
418ca1c9b0cSelric }
419ca1c9b0cSelric 
420ca1c9b0cSelric struct mcache_iter {
421ca1c9b0cSelric     krb5_mcache *cache;
422ca1c9b0cSelric };
423ca1c9b0cSelric 
424ca1c9b0cSelric static krb5_error_code KRB5_CALLCONV
mcc_get_cache_first(krb5_context context,krb5_cc_cursor * cursor)425ca1c9b0cSelric mcc_get_cache_first(krb5_context context, krb5_cc_cursor *cursor)
426ca1c9b0cSelric {
427ca1c9b0cSelric     struct mcache_iter *iter;
428ca1c9b0cSelric 
429ca1c9b0cSelric     iter = calloc(1, sizeof(*iter));
430b9d004c6Schristos     if (iter == NULL)
431b9d004c6Schristos 	return krb5_enomem(context);
432ca1c9b0cSelric 
433ca1c9b0cSelric     HEIMDAL_MUTEX_lock(&mcc_mutex);
434ca1c9b0cSelric     iter->cache = mcc_head;
435b9d004c6Schristos     if (iter->cache) {
436b9d004c6Schristos 	HEIMDAL_MUTEX_lock(&(iter->cache->mutex));
437ca1c9b0cSelric 	iter->cache->refcnt++;
438b9d004c6Schristos 	HEIMDAL_MUTEX_unlock(&(iter->cache->mutex));
439b9d004c6Schristos     }
440ca1c9b0cSelric     HEIMDAL_MUTEX_unlock(&mcc_mutex);
441ca1c9b0cSelric 
442ca1c9b0cSelric     *cursor = iter;
443ca1c9b0cSelric     return 0;
444ca1c9b0cSelric }
445ca1c9b0cSelric 
446ca1c9b0cSelric static krb5_error_code KRB5_CALLCONV
mcc_get_cache_next(krb5_context context,krb5_cc_cursor cursor,krb5_ccache * id)447ca1c9b0cSelric mcc_get_cache_next(krb5_context context, krb5_cc_cursor cursor, krb5_ccache *id)
448ca1c9b0cSelric {
449ca1c9b0cSelric     struct mcache_iter *iter = cursor;
450ca1c9b0cSelric     krb5_error_code ret;
451ca1c9b0cSelric     krb5_mcache *m;
452ca1c9b0cSelric 
453ca1c9b0cSelric     if (iter->cache == NULL)
454ca1c9b0cSelric 	return KRB5_CC_END;
455ca1c9b0cSelric 
456ca1c9b0cSelric     HEIMDAL_MUTEX_lock(&mcc_mutex);
457ca1c9b0cSelric     m = iter->cache;
458ca1c9b0cSelric     if (m->next)
459b9d004c6Schristos     {
460b9d004c6Schristos     	HEIMDAL_MUTEX_lock(&(m->next->mutex));
461ca1c9b0cSelric     	m->next->refcnt++;
462b9d004c6Schristos     	HEIMDAL_MUTEX_unlock(&(m->next->mutex));
463b9d004c6Schristos     }
464b9d004c6Schristos 
465ca1c9b0cSelric     iter->cache = m->next;
466ca1c9b0cSelric     HEIMDAL_MUTEX_unlock(&mcc_mutex);
467ca1c9b0cSelric 
468ca1c9b0cSelric     ret = _krb5_cc_allocate(context, &krb5_mcc_ops, id);
469ca1c9b0cSelric     if (ret)
470ca1c9b0cSelric 	return ret;
471ca1c9b0cSelric 
472ca1c9b0cSelric     (*id)->data.data = m;
473ca1c9b0cSelric     (*id)->data.length = sizeof(*m);
474ca1c9b0cSelric 
475ca1c9b0cSelric     return 0;
476ca1c9b0cSelric }
477ca1c9b0cSelric 
478ca1c9b0cSelric static krb5_error_code KRB5_CALLCONV
mcc_end_cache_get(krb5_context context,krb5_cc_cursor cursor)479ca1c9b0cSelric mcc_end_cache_get(krb5_context context, krb5_cc_cursor cursor)
480ca1c9b0cSelric {
481ca1c9b0cSelric     struct mcache_iter *iter = cursor;
482ca1c9b0cSelric 
483ca1c9b0cSelric     if (iter->cache)
484ca1c9b0cSelric     	mcc_close_internal(iter->cache);
485ca1c9b0cSelric     iter->cache = NULL;
486ca1c9b0cSelric     free(iter);
487ca1c9b0cSelric     return 0;
488ca1c9b0cSelric }
489ca1c9b0cSelric 
490ca1c9b0cSelric static krb5_error_code KRB5_CALLCONV
mcc_move(krb5_context context,krb5_ccache from,krb5_ccache to)491ca1c9b0cSelric mcc_move(krb5_context context, krb5_ccache from, krb5_ccache to)
492ca1c9b0cSelric {
493ca1c9b0cSelric     krb5_mcache *mfrom = MCACHE(from), *mto = MCACHE(to);
494ca1c9b0cSelric     struct link *creds;
495ca1c9b0cSelric     krb5_principal principal;
496ca1c9b0cSelric     krb5_mcache **n;
497ca1c9b0cSelric 
498ca1c9b0cSelric     HEIMDAL_MUTEX_lock(&mcc_mutex);
499ca1c9b0cSelric 
500ca1c9b0cSelric     /* drop the from cache from the linked list to avoid lookups */
501ca1c9b0cSelric     for(n = &mcc_head; n && *n; n = &(*n)->next) {
502ca1c9b0cSelric 	if(mfrom == *n) {
503ca1c9b0cSelric 	    *n = mfrom->next;
504ca1c9b0cSelric 	    break;
505ca1c9b0cSelric 	}
506ca1c9b0cSelric     }
507ca1c9b0cSelric 
508b9d004c6Schristos     HEIMDAL_MUTEX_lock(&(mfrom->mutex));
509b9d004c6Schristos     HEIMDAL_MUTEX_lock(&(mto->mutex));
510ca1c9b0cSelric     /* swap creds */
511ca1c9b0cSelric     creds = mto->creds;
512ca1c9b0cSelric     mto->creds = mfrom->creds;
513ca1c9b0cSelric     mfrom->creds = creds;
514ca1c9b0cSelric     /* swap principal */
515ca1c9b0cSelric     principal = mto->primary_principal;
516ca1c9b0cSelric     mto->primary_principal = mfrom->primary_principal;
517ca1c9b0cSelric     mfrom->primary_principal = principal;
518ca1c9b0cSelric 
519ca1c9b0cSelric     mto->mtime = mfrom->mtime = time(NULL);
520ca1c9b0cSelric 
521b9d004c6Schristos     HEIMDAL_MUTEX_unlock(&(mfrom->mutex));
522b9d004c6Schristos     HEIMDAL_MUTEX_unlock(&(mto->mutex));
523ca1c9b0cSelric     HEIMDAL_MUTEX_unlock(&mcc_mutex);
524ca1c9b0cSelric     mcc_destroy(context, from);
525ca1c9b0cSelric 
526ca1c9b0cSelric     return 0;
527ca1c9b0cSelric }
528ca1c9b0cSelric 
529ca1c9b0cSelric static krb5_error_code KRB5_CALLCONV
mcc_default_name(krb5_context context,char ** str)530ca1c9b0cSelric mcc_default_name(krb5_context context, char **str)
531ca1c9b0cSelric {
532ca1c9b0cSelric     *str = strdup("MEMORY:");
533b9d004c6Schristos     if (*str == NULL)
534b9d004c6Schristos 	return krb5_enomem(context);
535ca1c9b0cSelric     return 0;
536ca1c9b0cSelric }
537ca1c9b0cSelric 
538ca1c9b0cSelric static krb5_error_code KRB5_CALLCONV
mcc_lastchange(krb5_context context,krb5_ccache id,krb5_timestamp * mtime)539ca1c9b0cSelric mcc_lastchange(krb5_context context, krb5_ccache id, krb5_timestamp *mtime)
540ca1c9b0cSelric {
541b9d004c6Schristos     krb5_mcache *m = MCACHE(id);
542b9d004c6Schristos     HEIMDAL_MUTEX_lock(&(m->mutex));
543b9d004c6Schristos     *mtime = m->mtime;
544b9d004c6Schristos     HEIMDAL_MUTEX_unlock(&(m->mutex));
545ca1c9b0cSelric     return 0;
546ca1c9b0cSelric }
547ca1c9b0cSelric 
548ca1c9b0cSelric static krb5_error_code KRB5_CALLCONV
mcc_set_kdc_offset(krb5_context context,krb5_ccache id,krb5_deltat kdc_offset)549ca1c9b0cSelric mcc_set_kdc_offset(krb5_context context, krb5_ccache id, krb5_deltat kdc_offset)
550ca1c9b0cSelric {
551ca1c9b0cSelric     krb5_mcache *m = MCACHE(id);
552b9d004c6Schristos     HEIMDAL_MUTEX_lock(&(m->mutex));
553ca1c9b0cSelric     m->kdc_offset = kdc_offset;
554b9d004c6Schristos     HEIMDAL_MUTEX_unlock(&(m->mutex));
555ca1c9b0cSelric     return 0;
556ca1c9b0cSelric }
557ca1c9b0cSelric 
558ca1c9b0cSelric static krb5_error_code KRB5_CALLCONV
mcc_get_kdc_offset(krb5_context context,krb5_ccache id,krb5_deltat * kdc_offset)559ca1c9b0cSelric mcc_get_kdc_offset(krb5_context context, krb5_ccache id, krb5_deltat *kdc_offset)
560ca1c9b0cSelric {
561ca1c9b0cSelric     krb5_mcache *m = MCACHE(id);
562b9d004c6Schristos     HEIMDAL_MUTEX_lock(&(m->mutex));
563ca1c9b0cSelric     *kdc_offset = m->kdc_offset;
564b9d004c6Schristos     HEIMDAL_MUTEX_unlock(&(m->mutex));
565ca1c9b0cSelric     return 0;
566ca1c9b0cSelric }
567ca1c9b0cSelric 
568ca1c9b0cSelric 
569ca1c9b0cSelric /**
570ca1c9b0cSelric  * Variable containing the MEMORY based credential cache implemention.
571ca1c9b0cSelric  *
572ca1c9b0cSelric  * @ingroup krb5_ccache
573ca1c9b0cSelric  */
574ca1c9b0cSelric 
575ca1c9b0cSelric KRB5_LIB_VARIABLE const krb5_cc_ops krb5_mcc_ops = {
576ca1c9b0cSelric     KRB5_CC_OPS_VERSION,
577ca1c9b0cSelric     "MEMORY",
578ca1c9b0cSelric     mcc_get_name,
579ca1c9b0cSelric     mcc_resolve,
580ca1c9b0cSelric     mcc_gen_new,
581ca1c9b0cSelric     mcc_initialize,
582ca1c9b0cSelric     mcc_destroy,
583ca1c9b0cSelric     mcc_close,
584ca1c9b0cSelric     mcc_store_cred,
585ca1c9b0cSelric     NULL, /* mcc_retrieve */
586ca1c9b0cSelric     mcc_get_principal,
587ca1c9b0cSelric     mcc_get_first,
588ca1c9b0cSelric     mcc_get_next,
589ca1c9b0cSelric     mcc_end_get,
590ca1c9b0cSelric     mcc_remove_cred,
591ca1c9b0cSelric     mcc_set_flags,
592ca1c9b0cSelric     NULL,
593ca1c9b0cSelric     mcc_get_cache_first,
594ca1c9b0cSelric     mcc_get_cache_next,
595ca1c9b0cSelric     mcc_end_cache_get,
596ca1c9b0cSelric     mcc_move,
597ca1c9b0cSelric     mcc_default_name,
598ca1c9b0cSelric     NULL,
599ca1c9b0cSelric     mcc_lastchange,
600ca1c9b0cSelric     mcc_set_kdc_offset,
601ca1c9b0cSelric     mcc_get_kdc_offset
602ca1c9b0cSelric };
603