Fix user2user principal (again)
[heimdal.git] / kcm / cache.c
blobc63d2624ab09e76a0d2a4d39bd3d008b61efff0b
1 /*
2 * Copyright (c) 2005, PADL Software Pty Ltd.
3 * All rights reserved.
5 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of PADL Software nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY PADL SOFTWARE AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL PADL SOFTWARE OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
35 #include "kcm_locl.h"
37 HEIMDAL_MUTEX ccache_mutex = HEIMDAL_MUTEX_INITIALIZER;
38 kcm_ccache_data *ccache_head = NULL;
39 static unsigned int ccache_nextid = 0;
41 char *kcm_ccache_nextid(pid_t pid, uid_t uid, gid_t gid)
43 unsigned n;
44 char *name;
45 int ret;
47 HEIMDAL_MUTEX_lock(&ccache_mutex);
48 n = ++ccache_nextid;
49 HEIMDAL_MUTEX_unlock(&ccache_mutex);
51 ret = asprintf(&name, "%ld:%u", (long)uid, n);
52 if (ret == -1)
53 return NULL;
55 return name;
58 krb5_error_code
59 kcm_ccache_resolve(krb5_context context,
60 const char *name,
61 kcm_ccache *ccache)
63 kcm_ccache p;
64 krb5_error_code ret;
66 *ccache = NULL;
68 ret = KRB5_FCC_NOFILE;
70 HEIMDAL_MUTEX_lock(&ccache_mutex);
72 for (p = ccache_head; p != NULL; p = p->next) {
73 if ((p->flags & KCM_FLAGS_VALID) == 0)
74 continue;
75 if (strcmp(p->name, name) == 0) {
76 ret = 0;
77 break;
81 if (ret == 0) {
82 kcm_retain_ccache(context, p);
83 *ccache = p;
86 HEIMDAL_MUTEX_unlock(&ccache_mutex);
88 return ret;
91 krb5_error_code
92 kcm_ccache_resolve_by_uuid(krb5_context context,
93 kcmuuid_t uuid,
94 kcm_ccache *ccache)
96 kcm_ccache p;
97 krb5_error_code ret;
99 *ccache = NULL;
101 ret = KRB5_FCC_NOFILE;
103 HEIMDAL_MUTEX_lock(&ccache_mutex);
105 for (p = ccache_head; p != NULL; p = p->next) {
106 if ((p->flags & KCM_FLAGS_VALID) == 0)
107 continue;
108 if (memcmp(p->uuid, uuid, sizeof(*uuid)) == 0) {
109 ret = 0;
110 break;
114 if (ret == 0) {
115 kcm_retain_ccache(context, p);
116 *ccache = p;
119 HEIMDAL_MUTEX_unlock(&ccache_mutex);
121 return ret;
124 krb5_error_code
125 kcm_ccache_get_uuids(krb5_context context, kcm_client *client, kcm_operation opcode, krb5_storage *sp)
127 krb5_error_code ret;
128 kcm_ccache p;
130 ret = KRB5_FCC_NOFILE;
132 HEIMDAL_MUTEX_lock(&ccache_mutex);
134 for (p = ccache_head; p != NULL; p = p->next) {
135 if ((p->flags & KCM_FLAGS_VALID) == 0)
136 continue;
137 ret = kcm_access(context, client, opcode, p);
138 if (ret) {
139 ret = 0;
140 continue;
142 krb5_storage_write(sp, p->uuid, sizeof(p->uuid));
145 HEIMDAL_MUTEX_unlock(&ccache_mutex);
147 return ret;
151 krb5_error_code kcm_debug_ccache(krb5_context context)
153 kcm_ccache p;
155 for (p = ccache_head; p != NULL; p = p->next) {
156 char *cpn = NULL, *spn = NULL;
157 int ncreds = 0;
158 struct kcm_creds *k;
160 if ((p->flags & KCM_FLAGS_VALID) == 0) {
161 kcm_log(7, "cache %08x: empty slot");
162 continue;
165 KCM_ASSERT_VALID(p);
167 for (k = p->creds; k != NULL; k = k->next)
168 ncreds++;
170 if (p->client != NULL)
171 krb5_unparse_name(context, p->client, &cpn);
172 if (p->server != NULL)
173 krb5_unparse_name(context, p->server, &spn);
175 kcm_log(7, "cache %08x: name %s refcnt %d flags %04x mode %04o "
176 "uid %d gid %d client %s server %s ncreds %d",
177 p, p->name, p->refcnt, p->flags, p->mode, p->uid, p->gid,
178 (cpn == NULL) ? "<none>" : cpn,
179 (spn == NULL) ? "<none>" : spn,
180 ncreds);
182 if (cpn != NULL)
183 free(cpn);
184 if (spn != NULL)
185 free(spn);
188 return 0;
191 static void
192 kcm_free_ccache_data_internal(krb5_context context,
193 kcm_ccache_data *cache)
195 KCM_ASSERT_VALID(cache);
197 if (cache->name != NULL) {
198 free(cache->name);
199 cache->name = NULL;
202 if (cache->flags & KCM_FLAGS_USE_KEYTAB) {
203 krb5_kt_close(context, cache->key.keytab);
204 cache->key.keytab = NULL;
205 } else if (cache->flags & KCM_FLAGS_USE_CACHED_KEY) {
206 krb5_free_keyblock_contents(context, &cache->key.keyblock);
207 krb5_keyblock_zero(&cache->key.keyblock);
210 cache->flags = 0;
211 cache->mode = 0;
212 cache->uid = -1;
213 cache->gid = -1;
214 cache->session = -1;
216 kcm_zero_ccache_data_internal(context, cache);
218 cache->tkt_life = 0;
219 cache->renew_life = 0;
220 cache->kdc_offset = 0;
222 cache->next = NULL;
223 cache->refcnt = 0;
225 HEIMDAL_MUTEX_unlock(&cache->mutex);
226 HEIMDAL_MUTEX_destroy(&cache->mutex);
230 krb5_error_code
231 kcm_ccache_destroy(krb5_context context, const char *name)
233 kcm_ccache *p, ccache;
234 krb5_error_code ret;
236 ret = KRB5_FCC_NOFILE;
238 HEIMDAL_MUTEX_lock(&ccache_mutex);
239 for (p = &ccache_head; *p != NULL; p = &(*p)->next) {
240 if (((*p)->flags & KCM_FLAGS_VALID) == 0)
241 continue;
242 if (strcmp((*p)->name, name) == 0) {
243 ret = 0;
244 break;
247 if (ret)
248 goto out;
250 if ((*p)->refcnt != 1) {
251 ret = EAGAIN;
252 goto out;
255 ccache = *p;
256 *p = (*p)->next;
257 kcm_free_ccache_data_internal(context, ccache);
258 free(ccache);
260 out:
261 HEIMDAL_MUTEX_unlock(&ccache_mutex);
263 return ret;
266 static krb5_error_code
267 kcm_ccache_alloc(krb5_context context,
268 const char *name,
269 kcm_ccache *ccache)
271 kcm_ccache slot = NULL, p;
272 krb5_error_code ret;
273 int new_slot = 0;
275 *ccache = NULL;
277 /* First, check for duplicates */
278 HEIMDAL_MUTEX_lock(&ccache_mutex);
279 ret = 0;
280 for (p = ccache_head; p != NULL; p = p->next) {
281 if (p->flags & KCM_FLAGS_VALID) {
282 if (strcmp(p->name, name) == 0) {
283 ret = KRB5_CC_WRITE;
284 break;
286 } else if (slot == NULL)
287 slot = p;
290 if (ret)
291 goto out;
294 * Create an enpty slot for us.
296 if (slot == NULL) {
297 slot = (kcm_ccache_data *)malloc(sizeof(*slot));
298 if (slot == NULL) {
299 ret = KRB5_CC_NOMEM;
300 goto out;
302 slot->next = ccache_head;
303 HEIMDAL_MUTEX_init(&slot->mutex);
304 new_slot = 1;
307 RAND_bytes(slot->uuid, sizeof(slot->uuid));
309 slot->name = strdup(name);
310 if (slot->name == NULL) {
311 ret = KRB5_CC_NOMEM;
312 goto out;
315 slot->refcnt = 1;
316 slot->flags = KCM_FLAGS_VALID;
317 slot->mode = S_IRUSR | S_IWUSR;
318 slot->uid = -1;
319 slot->gid = -1;
320 slot->client = NULL;
321 slot->server = NULL;
322 slot->creds = NULL;
323 slot->key.keytab = NULL;
324 slot->tkt_life = 0;
325 slot->renew_life = 0;
326 slot->kdc_offset = 0;
328 if (new_slot)
329 ccache_head = slot;
331 *ccache = slot;
333 HEIMDAL_MUTEX_unlock(&ccache_mutex);
334 return 0;
336 out:
337 HEIMDAL_MUTEX_unlock(&ccache_mutex);
338 if (new_slot && slot != NULL) {
339 HEIMDAL_MUTEX_destroy(&slot->mutex);
340 free(slot);
342 return ret;
345 krb5_error_code
346 kcm_ccache_remove_creds_internal(krb5_context context,
347 kcm_ccache ccache)
349 struct kcm_creds *k;
351 k = ccache->creds;
352 while (k != NULL) {
353 struct kcm_creds *old;
355 krb5_free_cred_contents(context, &k->cred);
356 old = k;
357 k = k->next;
358 free(old);
360 ccache->creds = NULL;
362 return 0;
365 krb5_error_code
366 kcm_ccache_remove_creds(krb5_context context,
367 kcm_ccache ccache)
369 krb5_error_code ret;
371 KCM_ASSERT_VALID(ccache);
373 HEIMDAL_MUTEX_lock(&ccache->mutex);
374 ret = kcm_ccache_remove_creds_internal(context, ccache);
375 HEIMDAL_MUTEX_unlock(&ccache->mutex);
377 return ret;
380 krb5_error_code
381 kcm_zero_ccache_data_internal(krb5_context context,
382 kcm_ccache_data *cache)
384 if (cache->client != NULL) {
385 krb5_free_principal(context, cache->client);
386 cache->client = NULL;
389 if (cache->server != NULL) {
390 krb5_free_principal(context, cache->server);
391 cache->server = NULL;
394 kcm_ccache_remove_creds_internal(context, cache);
396 return 0;
399 krb5_error_code
400 kcm_zero_ccache_data(krb5_context context,
401 kcm_ccache cache)
403 krb5_error_code ret;
405 KCM_ASSERT_VALID(cache);
407 HEIMDAL_MUTEX_lock(&cache->mutex);
408 ret = kcm_zero_ccache_data_internal(context, cache);
409 HEIMDAL_MUTEX_unlock(&cache->mutex);
411 return ret;
414 krb5_error_code
415 kcm_retain_ccache(krb5_context context,
416 kcm_ccache ccache)
418 KCM_ASSERT_VALID(ccache);
420 HEIMDAL_MUTEX_lock(&ccache->mutex);
421 ccache->refcnt++;
422 HEIMDAL_MUTEX_unlock(&ccache->mutex);
424 return 0;
427 krb5_error_code
428 kcm_release_ccache(krb5_context context, kcm_ccache c)
430 krb5_error_code ret = 0;
432 KCM_ASSERT_VALID(c);
434 HEIMDAL_MUTEX_lock(&c->mutex);
435 if (c->refcnt == 1) {
436 kcm_free_ccache_data_internal(context, c);
437 free(c);
438 } else {
439 c->refcnt--;
440 HEIMDAL_MUTEX_unlock(&c->mutex);
443 return ret;
446 krb5_error_code
447 kcm_ccache_gen_new(krb5_context context,
448 pid_t pid,
449 uid_t uid,
450 gid_t gid,
451 kcm_ccache *ccache)
453 krb5_error_code ret;
454 char *name;
456 name = kcm_ccache_nextid(pid, uid, gid);
457 if (name == NULL) {
458 return KRB5_CC_NOMEM;
461 ret = kcm_ccache_new(context, name, ccache);
463 free(name);
464 return ret;
467 krb5_error_code
468 kcm_ccache_new(krb5_context context,
469 const char *name,
470 kcm_ccache *ccache)
472 krb5_error_code ret;
474 ret = kcm_ccache_alloc(context, name, ccache);
475 if (ret == 0) {
477 * one reference is held by the linked list,
478 * one by the caller
480 kcm_retain_ccache(context, *ccache);
483 return ret;
486 krb5_error_code
487 kcm_ccache_destroy_if_empty(krb5_context context,
488 kcm_ccache ccache)
490 krb5_error_code ret;
492 KCM_ASSERT_VALID(ccache);
494 if (ccache->creds == NULL) {
495 ret = kcm_ccache_destroy(context, ccache->name);
496 } else
497 ret = 0;
499 return ret;
502 krb5_error_code
503 kcm_ccache_store_cred(krb5_context context,
504 kcm_ccache ccache,
505 krb5_creds *creds,
506 int copy)
508 krb5_error_code ret;
509 krb5_creds *tmp;
511 KCM_ASSERT_VALID(ccache);
513 HEIMDAL_MUTEX_lock(&ccache->mutex);
514 ret = kcm_ccache_store_cred_internal(context, ccache, creds, copy, &tmp);
515 HEIMDAL_MUTEX_unlock(&ccache->mutex);
517 return ret;
520 struct kcm_creds *
521 kcm_ccache_find_cred_uuid(krb5_context context,
522 kcm_ccache ccache,
523 kcmuuid_t uuid)
525 struct kcm_creds *c;
527 for (c = ccache->creds; c != NULL; c = c->next)
528 if (memcmp(c->uuid, uuid, sizeof(c->uuid)) == 0)
529 return c;
531 return NULL;
536 krb5_error_code
537 kcm_ccache_store_cred_internal(krb5_context context,
538 kcm_ccache ccache,
539 krb5_creds *creds,
540 int copy,
541 krb5_creds **credp)
543 struct kcm_creds **c;
544 krb5_error_code ret;
546 for (c = &ccache->creds; *c != NULL; c = &(*c)->next)
549 *c = (struct kcm_creds *)calloc(1, sizeof(**c));
550 if (*c == NULL)
551 return KRB5_CC_NOMEM;
553 RAND_bytes((*c)->uuid, sizeof((*c)->uuid));
555 *credp = &(*c)->cred;
557 if (copy) {
558 ret = krb5_copy_creds_contents(context, creds, *credp);
559 if (ret) {
560 free(*c);
561 *c = NULL;
563 } else {
564 **credp = *creds;
565 ret = 0;
568 return ret;
571 krb5_error_code
572 kcm_ccache_remove_cred_internal(krb5_context context,
573 kcm_ccache ccache,
574 krb5_flags whichfields,
575 const krb5_creds *mcreds)
577 krb5_error_code ret;
578 struct kcm_creds **c;
580 ret = KRB5_CC_NOTFOUND;
582 for (c = &ccache->creds; *c != NULL; c = &(*c)->next) {
583 if (krb5_compare_creds(context, whichfields, mcreds, &(*c)->cred)) {
584 struct kcm_creds *cred = *c;
586 *c = cred->next;
587 krb5_free_cred_contents(context, &cred->cred);
588 free(cred);
589 ret = 0;
590 if (*c == NULL)
591 break;
595 return ret;
598 krb5_error_code
599 kcm_ccache_remove_cred(krb5_context context,
600 kcm_ccache ccache,
601 krb5_flags whichfields,
602 const krb5_creds *mcreds)
604 krb5_error_code ret;
606 KCM_ASSERT_VALID(ccache);
608 HEIMDAL_MUTEX_lock(&ccache->mutex);
609 ret = kcm_ccache_remove_cred_internal(context, ccache, whichfields, mcreds);
610 HEIMDAL_MUTEX_unlock(&ccache->mutex);
612 return ret;
615 krb5_error_code
616 kcm_ccache_retrieve_cred_internal(krb5_context context,
617 kcm_ccache ccache,
618 krb5_flags whichfields,
619 const krb5_creds *mcreds,
620 krb5_creds **creds)
622 krb5_boolean match;
623 struct kcm_creds *c;
624 krb5_error_code ret;
626 memset(creds, 0, sizeof(*creds));
628 ret = KRB5_CC_END;
630 match = FALSE;
631 for (c = ccache->creds; c != NULL; c = c->next) {
632 match = krb5_compare_creds(context, whichfields, mcreds, &c->cred);
633 if (match)
634 break;
637 if (match) {
638 ret = 0;
639 *creds = &c->cred;
642 return ret;
645 krb5_error_code
646 kcm_ccache_retrieve_cred(krb5_context context,
647 kcm_ccache ccache,
648 krb5_flags whichfields,
649 const krb5_creds *mcreds,
650 krb5_creds **credp)
652 krb5_error_code ret;
654 KCM_ASSERT_VALID(ccache);
656 HEIMDAL_MUTEX_lock(&ccache->mutex);
657 ret = kcm_ccache_retrieve_cred_internal(context, ccache,
658 whichfields, mcreds, credp);
659 HEIMDAL_MUTEX_unlock(&ccache->mutex);
661 return ret;
664 char *
665 kcm_ccache_first_name(kcm_client *client)
667 kcm_ccache p;
668 char *name = NULL;
670 HEIMDAL_MUTEX_lock(&ccache_mutex);
672 for (p = ccache_head; p != NULL; p = p->next) {
673 if (kcm_is_same_session(client, p->uid, p->session))
674 break;
676 if (p)
677 name = strdup(p->name);
678 HEIMDAL_MUTEX_unlock(&ccache_mutex);
679 return name;