Debian Codegraph @

lib/krb5/mcache.c

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