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