Quiet a warning in test_plugin.c
[heimdal.git] / lib / krb5 / fcache.c
blob9beaa1c6436329cf851919b7c190b7fcf14dad3d
1 /*
2 * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
36 #include "krb5_locl.h"
38 typedef struct krb5_fcache{
39 char *filename;
40 int version;
41 }krb5_fcache;
43 struct fcc_cursor {
44 int fd;
45 krb5_storage *sp;
48 #define KRB5_FCC_FVNO_1 1
49 #define KRB5_FCC_FVNO_2 2
50 #define KRB5_FCC_FVNO_3 3
51 #define KRB5_FCC_FVNO_4 4
53 #define FCC_TAG_DELTATIME 1
55 #define FCACHE(X) ((krb5_fcache*)(X)->data.data)
57 #define FILENAME(X) (FCACHE(X)->filename)
59 #define FCC_CURSOR(C) ((struct fcc_cursor*)(C))
61 static const char* KRB5_CALLCONV
62 fcc_get_name(krb5_context context,
63 krb5_ccache id)
65 if (FCACHE(id) == NULL)
66 return NULL;
68 return FILENAME(id);
71 int
72 _krb5_xlock(krb5_context context, int fd, krb5_boolean exclusive,
73 const char *filename)
75 int ret;
76 #ifdef HAVE_FCNTL
77 struct flock l;
79 l.l_start = 0;
80 l.l_len = 0;
81 l.l_type = exclusive ? F_WRLCK : F_RDLCK;
82 l.l_whence = SEEK_SET;
83 ret = fcntl(fd, F_SETLKW, &l);
84 #else
85 ret = flock(fd, exclusive ? LOCK_EX : LOCK_SH);
86 #endif
87 if(ret < 0)
88 ret = errno;
89 if(ret == EACCES) /* fcntl can return EACCES instead of EAGAIN */
90 ret = EAGAIN;
92 switch (ret) {
93 case 0:
94 break;
95 case EINVAL: /* filesystem doesn't support locking, let the user have it */
96 ret = 0;
97 break;
98 case EAGAIN:
99 krb5_set_error_message(context, ret,
100 N_("timed out locking cache file %s", "file"),
101 filename);
102 break;
103 default: {
104 char buf[128];
105 rk_strerror_r(ret, buf, sizeof(buf));
106 krb5_set_error_message(context, ret,
107 N_("error locking cache file %s: %s",
108 "file, error"), filename, buf);
109 break;
112 return ret;
116 _krb5_xunlock(krb5_context context, int fd)
118 int ret;
119 #ifdef HAVE_FCNTL
120 struct flock l;
121 l.l_start = 0;
122 l.l_len = 0;
123 l.l_type = F_UNLCK;
124 l.l_whence = SEEK_SET;
125 ret = fcntl(fd, F_SETLKW, &l);
126 #else
127 ret = flock(fd, LOCK_UN);
128 #endif
129 if (ret < 0)
130 ret = errno;
131 switch (ret) {
132 case 0:
133 break;
134 case EINVAL: /* filesystem doesn't support locking, let the user have it */
135 ret = 0;
136 break;
137 default: {
138 char buf[128];
139 rk_strerror_r(ret, buf, sizeof(buf));
140 krb5_set_error_message(context, ret,
141 N_("Failed to unlock file: %s", ""), buf);
142 break;
145 return ret;
148 static krb5_error_code
149 write_storage(krb5_context context, krb5_storage *sp, int fd)
151 krb5_error_code ret;
152 krb5_data data;
153 ssize_t sret;
155 ret = krb5_storage_to_data(sp, &data);
156 if (ret) {
157 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
158 return ret;
160 sret = write(fd, data.data, data.length);
161 ret = (sret != (ssize_t)data.length);
162 krb5_data_free(&data);
163 if (ret) {
164 ret = errno;
165 krb5_set_error_message(context, ret,
166 N_("Failed to write FILE credential data", ""));
167 return ret;
169 return 0;
173 static krb5_error_code KRB5_CALLCONV
174 fcc_lock(krb5_context context, krb5_ccache id,
175 int fd, krb5_boolean exclusive)
177 return _krb5_xlock(context, fd, exclusive, fcc_get_name(context, id));
180 static krb5_error_code KRB5_CALLCONV
181 fcc_unlock(krb5_context context, int fd)
183 return _krb5_xunlock(context, fd);
186 static krb5_error_code KRB5_CALLCONV
187 fcc_resolve(krb5_context context, krb5_ccache *id, const char *res)
189 krb5_fcache *f;
190 f = malloc(sizeof(*f));
191 if(f == NULL) {
192 krb5_set_error_message(context, KRB5_CC_NOMEM,
193 N_("malloc: out of memory", ""));
194 return KRB5_CC_NOMEM;
196 f->filename = strdup(res);
197 if(f->filename == NULL){
198 free(f);
199 krb5_set_error_message(context, KRB5_CC_NOMEM,
200 N_("malloc: out of memory", ""));
201 return KRB5_CC_NOMEM;
203 f->version = 0;
204 (*id)->data.data = f;
205 (*id)->data.length = sizeof(*f);
206 return 0;
210 * Try to scrub the contents of `filename' safely.
213 static int
214 scrub_file (int fd)
216 off_t pos;
217 char buf[128];
219 pos = lseek(fd, 0, SEEK_END);
220 if (pos < 0)
221 return errno;
222 if (lseek(fd, 0, SEEK_SET) < 0)
223 return errno;
224 memset(buf, 0, sizeof(buf));
225 while(pos > 0) {
226 ssize_t tmp = write(fd, buf, min((off_t)sizeof(buf), pos));
228 if (tmp < 0)
229 return errno;
230 pos -= tmp;
232 #ifdef _MSC_VER
233 _commit (fd);
234 #else
235 fsync (fd);
236 #endif
237 return 0;
241 * Erase `filename' if it exists, trying to remove the contents if
242 * it's `safe'. We always try to remove the file, it it exists. It's
243 * only overwritten if it's a regular file (not a symlink and not a
244 * hardlink)
247 krb5_error_code
248 _krb5_erase_file(krb5_context context, const char *filename)
250 int fd;
251 struct stat sb1, sb2;
252 int ret;
254 ret = lstat (filename, &sb1);
255 if (ret < 0)
256 return errno;
258 fd = open(filename, O_RDWR | O_BINARY);
259 if(fd < 0) {
260 if(errno == ENOENT)
261 return 0;
262 else
263 return errno;
265 rk_cloexec(fd);
266 ret = _krb5_xlock(context, fd, 1, filename);
267 if (ret) {
268 close(fd);
269 return ret;
271 if (unlink(filename) < 0) {
272 _krb5_xunlock(context, fd);
273 close (fd);
274 return errno;
276 ret = fstat (fd, &sb2);
277 if (ret < 0) {
278 _krb5_xunlock(context, fd);
279 close (fd);
280 return errno;
283 /* check if someone was playing with symlinks */
285 if (sb1.st_dev != sb2.st_dev || sb1.st_ino != sb2.st_ino) {
286 _krb5_xunlock(context, fd);
287 close (fd);
288 return EPERM;
291 /* there are still hard links to this file */
293 if (sb2.st_nlink != 0) {
294 _krb5_xunlock(context, fd);
295 close (fd);
296 return 0;
299 ret = scrub_file (fd);
300 if (ret) {
301 _krb5_xunlock(context, fd);
302 close(fd);
303 return ret;
305 ret = _krb5_xunlock(context, fd);
306 close (fd);
307 return ret;
310 static krb5_error_code KRB5_CALLCONV
311 fcc_gen_new(krb5_context context, krb5_ccache *id)
313 char *file = NULL, *exp_file = NULL;
314 krb5_error_code ret;
315 krb5_fcache *f;
316 int fd;
318 f = malloc(sizeof(*f));
319 if(f == NULL) {
320 krb5_set_error_message(context, KRB5_CC_NOMEM,
321 N_("malloc: out of memory", ""));
322 return KRB5_CC_NOMEM;
324 ret = asprintf (&file, "%sXXXXXX", KRB5_DEFAULT_CCFILE_ROOT);
325 if(ret < 0 || file == NULL) {
326 free(f);
327 krb5_set_error_message(context, KRB5_CC_NOMEM,
328 N_("malloc: out of memory", ""));
329 return KRB5_CC_NOMEM;
331 ret = _krb5_expand_path_tokens(context, file, &exp_file);
332 free(file);
333 if (ret)
334 return ret;
336 file = exp_file;
338 fd = mkstemp(exp_file);
339 if(fd < 0) {
340 int xret = errno;
341 krb5_set_error_message(context, xret, N_("mkstemp %s failed", ""), exp_file);
342 free(f);
343 free(exp_file);
344 return xret;
346 close(fd);
347 f->filename = exp_file;
348 f->version = 0;
349 (*id)->data.data = f;
350 (*id)->data.length = sizeof(*f);
351 return 0;
354 static void
355 storage_set_flags(krb5_context context, krb5_storage *sp, int vno)
357 int flags = 0;
358 switch(vno) {
359 case KRB5_FCC_FVNO_1:
360 flags |= KRB5_STORAGE_PRINCIPAL_WRONG_NUM_COMPONENTS;
361 flags |= KRB5_STORAGE_PRINCIPAL_NO_NAME_TYPE;
362 flags |= KRB5_STORAGE_HOST_BYTEORDER;
363 break;
364 case KRB5_FCC_FVNO_2:
365 flags |= KRB5_STORAGE_HOST_BYTEORDER;
366 break;
367 case KRB5_FCC_FVNO_3:
368 flags |= KRB5_STORAGE_KEYBLOCK_KEYTYPE_TWICE;
369 break;
370 case KRB5_FCC_FVNO_4:
371 break;
372 default:
373 krb5_abortx(context,
374 "storage_set_flags called with bad vno (%x)", vno);
376 krb5_storage_set_flags(sp, flags);
379 static krb5_error_code KRB5_CALLCONV
380 fcc_open(krb5_context context,
381 krb5_ccache id,
382 int *fd_ret,
383 int flags,
384 mode_t mode)
386 krb5_boolean exclusive = ((flags | O_WRONLY) == flags ||
387 (flags | O_RDWR) == flags);
388 krb5_error_code ret;
389 const char *filename;
390 int fd;
392 if (FCACHE(id) == NULL)
393 return krb5_einval(context, 2);
395 filename = FILENAME(id);
397 fd = open(filename, flags, mode);
398 if(fd < 0) {
399 char buf[128];
400 ret = errno;
401 rk_strerror_r(ret, buf, sizeof(buf));
402 krb5_set_error_message(context, ret, N_("open(%s): %s", "file, error"),
403 filename, buf);
404 return ret;
406 rk_cloexec(fd);
408 if((ret = fcc_lock(context, id, fd, exclusive)) != 0) {
409 close(fd);
410 return ret;
412 *fd_ret = fd;
413 return 0;
416 static krb5_error_code KRB5_CALLCONV
417 fcc_initialize(krb5_context context,
418 krb5_ccache id,
419 krb5_principal primary_principal)
421 krb5_fcache *f = FCACHE(id);
422 int ret = 0;
423 int fd;
425 if (f == NULL)
426 return krb5_einval(context, 2);
428 unlink (f->filename);
430 ret = fcc_open(context, id, &fd, O_RDWR | O_CREAT | O_EXCL | O_BINARY | O_CLOEXEC, 0600);
431 if(ret)
432 return ret;
434 krb5_storage *sp;
435 sp = krb5_storage_emem();
436 krb5_storage_set_eof_code(sp, KRB5_CC_END);
437 if(context->fcache_vno != 0)
438 f->version = context->fcache_vno;
439 else
440 f->version = KRB5_FCC_FVNO_4;
441 ret |= krb5_store_int8(sp, 5);
442 ret |= krb5_store_int8(sp, f->version);
443 storage_set_flags(context, sp, f->version);
444 if(f->version == KRB5_FCC_FVNO_4 && ret == 0) {
445 /* V4 stuff */
446 if (context->kdc_sec_offset) {
447 ret |= krb5_store_int16 (sp, 12); /* length */
448 ret |= krb5_store_int16 (sp, FCC_TAG_DELTATIME); /* Tag */
449 ret |= krb5_store_int16 (sp, 8); /* length of data */
450 ret |= krb5_store_int32 (sp, context->kdc_sec_offset);
451 ret |= krb5_store_int32 (sp, context->kdc_usec_offset);
452 } else {
453 ret |= krb5_store_int16 (sp, 0);
456 ret |= krb5_store_principal(sp, primary_principal);
458 ret |= write_storage(context, sp, fd);
460 krb5_storage_free(sp);
462 fcc_unlock(context, fd);
463 if (close(fd) < 0)
464 if (ret == 0) {
465 char buf[128];
466 ret = errno;
467 rk_strerror_r(ret, buf, sizeof(buf));
468 krb5_set_error_message (context, ret, N_("close %s: %s", ""),
469 FILENAME(id), buf);
471 return ret;
474 static krb5_error_code KRB5_CALLCONV
475 fcc_close(krb5_context context,
476 krb5_ccache id)
478 if (FCACHE(id) == NULL)
479 return krb5_einval(context, 2);
481 free (FILENAME(id));
482 krb5_data_free(&id->data);
483 return 0;
486 static krb5_error_code KRB5_CALLCONV
487 fcc_destroy(krb5_context context,
488 krb5_ccache id)
490 if (FCACHE(id) == NULL)
491 return krb5_einval(context, 2);
493 _krb5_erase_file(context, FILENAME(id));
494 return 0;
497 static krb5_error_code KRB5_CALLCONV
498 fcc_store_cred(krb5_context context,
499 krb5_ccache id,
500 krb5_creds *creds)
502 int ret;
503 int fd;
505 ret = fcc_open(context, id, &fd, O_WRONLY | O_APPEND | O_BINARY | O_CLOEXEC, 0);
506 if(ret)
507 return ret;
509 krb5_storage *sp;
511 sp = krb5_storage_emem();
512 krb5_storage_set_eof_code(sp, KRB5_CC_END);
513 storage_set_flags(context, sp, FCACHE(id)->version);
514 if (!krb5_config_get_bool_default(context, NULL, TRUE,
515 "libdefaults",
516 "fcc-mit-ticketflags",
517 NULL))
518 krb5_storage_set_flags(sp, KRB5_STORAGE_CREDS_FLAGS_WRONG_BITORDER);
519 ret = krb5_store_creds(sp, creds);
520 if (ret == 0)
521 ret = write_storage(context, sp, fd);
522 krb5_storage_free(sp);
524 fcc_unlock(context, fd);
525 if (close(fd) < 0) {
526 if (ret == 0) {
527 char buf[128];
528 rk_strerror_r(ret, buf, sizeof(buf));
529 ret = errno;
530 krb5_set_error_message (context, ret, N_("close %s: %s", ""),
531 FILENAME(id), buf);
534 return ret;
537 static krb5_error_code
538 init_fcc (krb5_context context,
539 krb5_ccache id,
540 krb5_storage **ret_sp,
541 int *ret_fd,
542 krb5_deltat *kdc_offset)
544 int fd;
545 int8_t pvno, tag;
546 krb5_storage *sp;
547 krb5_error_code ret;
549 if (kdc_offset)
550 *kdc_offset = 0;
552 ret = fcc_open(context, id, &fd, O_RDONLY | O_BINARY | O_CLOEXEC, 0);
553 if(ret)
554 return ret;
556 sp = krb5_storage_from_fd(fd);
557 if(sp == NULL) {
558 ret = krb5_enomem(context);
559 goto out;
561 krb5_storage_set_eof_code(sp, KRB5_CC_END);
562 ret = krb5_ret_int8(sp, &pvno);
563 if(ret != 0) {
564 if(ret == KRB5_CC_END) {
565 ret = ENOENT;
566 krb5_set_error_message(context, ret,
567 N_("Empty credential cache file: %s", ""),
568 FILENAME(id));
569 } else
570 krb5_set_error_message(context, ret, N_("Error reading pvno "
571 "in cache file: %s", ""),
572 FILENAME(id));
573 goto out;
575 if(pvno != 5) {
576 ret = KRB5_CCACHE_BADVNO;
577 krb5_set_error_message(context, ret, N_("Bad version number in credential "
578 "cache file: %s", ""),
579 FILENAME(id));
580 goto out;
582 ret = krb5_ret_int8(sp, &tag); /* should not be host byte order */
583 if(ret != 0) {
584 ret = KRB5_CC_FORMAT;
585 krb5_set_error_message(context, ret, "Error reading tag in "
586 "cache file: %s", FILENAME(id));
587 goto out;
589 FCACHE(id)->version = tag;
590 storage_set_flags(context, sp, FCACHE(id)->version);
591 switch (tag) {
592 case KRB5_FCC_FVNO_4: {
593 int16_t length;
595 ret = krb5_ret_int16 (sp, &length);
596 if(ret) {
597 ret = KRB5_CC_FORMAT;
598 krb5_set_error_message(context, ret,
599 N_("Error reading tag length in "
600 "cache file: %s", ""), FILENAME(id));
601 goto out;
603 while(length > 0) {
604 int16_t dtag, data_len;
605 int i;
606 int8_t dummy;
608 ret = krb5_ret_int16 (sp, &dtag);
609 if(ret) {
610 ret = KRB5_CC_FORMAT;
611 krb5_set_error_message(context, ret, N_("Error reading dtag in "
612 "cache file: %s", ""),
613 FILENAME(id));
614 goto out;
616 ret = krb5_ret_int16 (sp, &data_len);
617 if(ret) {
618 ret = KRB5_CC_FORMAT;
619 krb5_set_error_message(context, ret,
620 N_("Error reading dlength "
621 "in cache file: %s",""),
622 FILENAME(id));
623 goto out;
625 switch (dtag) {
626 case FCC_TAG_DELTATIME : {
627 int32_t offset;
629 ret = krb5_ret_int32 (sp, &offset);
630 ret |= krb5_ret_int32 (sp, &context->kdc_usec_offset);
631 if(ret) {
632 ret = KRB5_CC_FORMAT;
633 krb5_set_error_message(context, ret,
634 N_("Error reading kdc_sec in "
635 "cache file: %s", ""),
636 FILENAME(id));
637 goto out;
639 context->kdc_sec_offset = offset;
640 if (kdc_offset)
641 *kdc_offset = offset;
642 break;
644 default :
645 for (i = 0; i < data_len; ++i) {
646 ret = krb5_ret_int8 (sp, &dummy);
647 if(ret) {
648 ret = KRB5_CC_FORMAT;
649 krb5_set_error_message(context, ret,
650 N_("Error reading unknown "
651 "tag in cache file: %s", ""),
652 FILENAME(id));
653 goto out;
656 break;
658 length -= 4 + data_len;
660 break;
662 case KRB5_FCC_FVNO_3:
663 case KRB5_FCC_FVNO_2:
664 case KRB5_FCC_FVNO_1:
665 break;
666 default :
667 ret = KRB5_CCACHE_BADVNO;
668 krb5_set_error_message(context, ret,
669 N_("Unknown version number (%d) in "
670 "credential cache file: %s", ""),
671 (int)tag, FILENAME(id));
672 goto out;
674 *ret_sp = sp;
675 *ret_fd = fd;
677 return 0;
678 out:
679 if(sp != NULL)
680 krb5_storage_free(sp);
681 fcc_unlock(context, fd);
682 close(fd);
683 return ret;
686 static krb5_error_code KRB5_CALLCONV
687 fcc_get_principal(krb5_context context,
688 krb5_ccache id,
689 krb5_principal *principal)
691 krb5_error_code ret;
692 int fd;
693 krb5_storage *sp;
695 ret = init_fcc (context, id, &sp, &fd, NULL);
696 if (ret)
697 return ret;
698 ret = krb5_ret_principal(sp, principal);
699 if (ret)
700 krb5_clear_error_message(context);
701 krb5_storage_free(sp);
702 fcc_unlock(context, fd);
703 close(fd);
704 return ret;
707 static krb5_error_code KRB5_CALLCONV
708 fcc_end_get (krb5_context context,
709 krb5_ccache id,
710 krb5_cc_cursor *cursor);
712 static krb5_error_code KRB5_CALLCONV
713 fcc_get_first (krb5_context context,
714 krb5_ccache id,
715 krb5_cc_cursor *cursor)
717 krb5_error_code ret;
718 krb5_principal principal;
720 if (FCACHE(id) == NULL)
721 return krb5_einval(context, 2);
723 *cursor = malloc(sizeof(struct fcc_cursor));
724 if (*cursor == NULL)
725 return krb5_enomem(context);
726 memset(*cursor, 0, sizeof(struct fcc_cursor));
728 ret = init_fcc (context, id, &FCC_CURSOR(*cursor)->sp,
729 &FCC_CURSOR(*cursor)->fd, NULL);
730 if (ret) {
731 free(*cursor);
732 *cursor = NULL;
733 return ret;
735 ret = krb5_ret_principal (FCC_CURSOR(*cursor)->sp, &principal);
736 if(ret) {
737 krb5_clear_error_message(context);
738 fcc_end_get(context, id, cursor);
739 return ret;
741 krb5_free_principal (context, principal);
742 fcc_unlock(context, FCC_CURSOR(*cursor)->fd);
743 return 0;
746 static krb5_error_code KRB5_CALLCONV
747 fcc_get_next (krb5_context context,
748 krb5_ccache id,
749 krb5_cc_cursor *cursor,
750 krb5_creds *creds)
752 krb5_error_code ret;
754 if (FCACHE(id) == NULL)
755 return krb5_einval(context, 2);
757 if (FCC_CURSOR(*cursor) == NULL)
758 return krb5_einval(context, 3);
760 if((ret = fcc_lock(context, id, FCC_CURSOR(*cursor)->fd, FALSE)) != 0)
761 return ret;
763 ret = krb5_ret_creds(FCC_CURSOR(*cursor)->sp, creds);
764 if (ret)
765 krb5_clear_error_message(context);
767 fcc_unlock(context, FCC_CURSOR(*cursor)->fd);
768 return ret;
771 static krb5_error_code KRB5_CALLCONV
772 fcc_end_get (krb5_context context,
773 krb5_ccache id,
774 krb5_cc_cursor *cursor)
777 if (FCACHE(id) == NULL)
778 return krb5_einval(context, 2);
780 if (FCC_CURSOR(*cursor) == NULL)
781 return krb5_einval(context, 3);
783 krb5_storage_free(FCC_CURSOR(*cursor)->sp);
784 close (FCC_CURSOR(*cursor)->fd);
785 free(*cursor);
786 *cursor = NULL;
787 return 0;
790 static krb5_error_code KRB5_CALLCONV
791 fcc_remove_cred(krb5_context context,
792 krb5_ccache id,
793 krb5_flags which,
794 krb5_creds *cred)
796 krb5_error_code ret;
797 krb5_ccache copy, newfile;
798 char *newname = NULL;
799 int fd;
801 if (FCACHE(id) == NULL)
802 return krb5_einval(context, 2);
804 ret = krb5_cc_new_unique(context, krb5_cc_type_memory, NULL, &copy);
805 if (ret)
806 return ret;
808 ret = krb5_cc_copy_cache(context, id, copy);
809 if (ret) {
810 krb5_cc_destroy(context, copy);
811 return ret;
814 ret = krb5_cc_remove_cred(context, copy, which, cred);
815 if (ret) {
816 krb5_cc_destroy(context, copy);
817 return ret;
820 ret = asprintf(&newname, "FILE:%s.XXXXXX", FILENAME(id));
821 if (ret < 0 || newname == NULL) {
822 krb5_cc_destroy(context, copy);
823 return krb5_enomem(context);
826 fd = mkstemp(&newname[5]);
827 if (fd < 0) {
828 ret = errno;
829 krb5_cc_destroy(context, copy);
830 return ret;
832 close(fd);
834 ret = krb5_cc_resolve(context, newname, &newfile);
835 if (ret) {
836 unlink(&newname[5]);
837 free(newname);
838 krb5_cc_destroy(context, copy);
839 return ret;
842 ret = krb5_cc_copy_cache(context, copy, newfile);
843 krb5_cc_destroy(context, copy);
844 if (ret) {
845 free(newname);
846 krb5_cc_destroy(context, newfile);
847 return ret;
850 ret = rk_rename(&newname[5], FILENAME(id));
851 if (ret)
852 ret = errno;
853 free(newname);
854 krb5_cc_close(context, newfile);
856 return ret;
859 static krb5_error_code KRB5_CALLCONV
860 fcc_set_flags(krb5_context context,
861 krb5_ccache id,
862 krb5_flags flags)
864 if (FCACHE(id) == NULL)
865 return krb5_einval(context, 2);
867 return 0; /* XXX */
870 static int KRB5_CALLCONV
871 fcc_get_version(krb5_context context,
872 krb5_ccache id)
874 if (FCACHE(id) == NULL)
875 return -1;
877 return FCACHE(id)->version;
880 struct fcache_iter {
881 int first;
884 static krb5_error_code KRB5_CALLCONV
885 fcc_get_cache_first(krb5_context context, krb5_cc_cursor *cursor)
887 struct fcache_iter *iter;
889 iter = calloc(1, sizeof(*iter));
890 if (iter == NULL)
891 return krb5_enomem(context);
892 iter->first = 1;
893 *cursor = iter;
894 return 0;
897 static krb5_error_code KRB5_CALLCONV
898 fcc_get_cache_next(krb5_context context, krb5_cc_cursor cursor, krb5_ccache *id)
900 struct fcache_iter *iter = cursor;
901 krb5_error_code ret;
902 const char *fn;
903 char *expandedfn = NULL;
905 if (iter == NULL)
906 return krb5_einval(context, 2);
908 if (!iter->first) {
909 krb5_clear_error_message(context);
910 return KRB5_CC_END;
912 iter->first = 0;
914 fn = krb5_cc_default_name(context);
915 if (fn == NULL || strncasecmp(fn, "FILE:", 5) != 0) {
916 ret = _krb5_expand_default_cc_name(context,
917 KRB5_DEFAULT_CCNAME_FILE,
918 &expandedfn);
919 if (ret)
920 return ret;
921 fn = expandedfn;
923 /* check if file exists, don't return a non existant "next" */
924 if (strncasecmp(fn, "FILE:", 5) == 0) {
925 struct stat sb;
926 ret = stat(fn + 5, &sb);
927 if (ret) {
928 ret = KRB5_CC_END;
929 goto out;
932 ret = krb5_cc_resolve(context, fn, id);
933 out:
934 if (expandedfn)
935 free(expandedfn);
937 return ret;
940 static krb5_error_code KRB5_CALLCONV
941 fcc_end_cache_get(krb5_context context, krb5_cc_cursor cursor)
943 struct fcache_iter *iter = cursor;
945 if (iter == NULL)
946 return krb5_einval(context, 2);
948 free(iter);
949 return 0;
952 static krb5_error_code KRB5_CALLCONV
953 fcc_move(krb5_context context, krb5_ccache from, krb5_ccache to)
955 krb5_error_code ret = 0;
957 ret = rk_rename(FILENAME(from), FILENAME(to));
959 if (ret && errno != EXDEV) {
960 char buf[128];
961 ret = errno;
962 rk_strerror_r(ret, buf, sizeof(buf));
963 krb5_set_error_message(context, ret,
964 N_("Rename of file from %s "
965 "to %s failed: %s", ""),
966 FILENAME(from), FILENAME(to), buf);
967 return ret;
968 } else if (ret && errno == EXDEV) {
969 /* make a copy and delete the orignal */
970 krb5_ssize_t sz1, sz2;
971 int fd1, fd2;
972 char buf[BUFSIZ];
974 ret = fcc_open(context, from, &fd1, O_RDONLY | O_BINARY | O_CLOEXEC, 0);
975 if(ret)
976 return ret;
978 unlink(FILENAME(to));
980 ret = fcc_open(context, to, &fd2,
981 O_WRONLY | O_CREAT | O_EXCL | O_BINARY | O_CLOEXEC, 0600);
982 if(ret)
983 goto out1;
985 while((sz1 = read(fd1, buf, sizeof(buf))) > 0) {
986 sz2 = write(fd2, buf, sz1);
987 if (sz1 != sz2) {
988 ret = EIO;
989 krb5_set_error_message(context, ret,
990 N_("Failed to write data from one file "
991 "credential cache to the other", ""));
992 goto out2;
995 if (sz1 < 0) {
996 ret = EIO;
997 krb5_set_error_message(context, ret,
998 N_("Failed to read data from one file "
999 "credential cache to the other", ""));
1000 goto out2;
1002 out2:
1003 fcc_unlock(context, fd2);
1004 close(fd2);
1006 out1:
1007 fcc_unlock(context, fd1);
1008 close(fd1);
1010 _krb5_erase_file(context, FILENAME(from));
1012 if (ret) {
1013 _krb5_erase_file(context, FILENAME(to));
1014 return ret;
1018 /* make sure ->version is uptodate */
1020 krb5_storage *sp;
1021 int fd;
1022 if ((ret = init_fcc (context, to, &sp, &fd, NULL)) == 0) {
1023 if (sp)
1024 krb5_storage_free(sp);
1025 fcc_unlock(context, fd);
1026 close(fd);
1030 fcc_close(context, from);
1032 return ret;
1035 static krb5_error_code KRB5_CALLCONV
1036 fcc_get_default_name(krb5_context context, char **str)
1038 return _krb5_expand_default_cc_name(context,
1039 KRB5_DEFAULT_CCNAME_FILE,
1040 str);
1043 static krb5_error_code KRB5_CALLCONV
1044 fcc_lastchange(krb5_context context, krb5_ccache id, krb5_timestamp *mtime)
1046 krb5_error_code ret;
1047 struct stat sb;
1048 int fd;
1050 ret = fcc_open(context, id, &fd, O_RDONLY | O_BINARY | O_CLOEXEC, 0);
1051 if(ret)
1052 return ret;
1053 ret = fstat(fd, &sb);
1054 close(fd);
1055 if (ret) {
1056 ret = errno;
1057 krb5_set_error_message(context, ret, N_("Failed to stat cache file", ""));
1058 return ret;
1060 *mtime = sb.st_mtime;
1061 return 0;
1064 static krb5_error_code KRB5_CALLCONV
1065 fcc_set_kdc_offset(krb5_context context, krb5_ccache id, krb5_deltat kdc_offset)
1067 return 0;
1070 static krb5_error_code KRB5_CALLCONV
1071 fcc_get_kdc_offset(krb5_context context, krb5_ccache id, krb5_deltat *kdc_offset)
1073 krb5_error_code ret;
1074 krb5_storage *sp = NULL;
1075 int fd;
1076 ret = init_fcc(context, id, &sp, &fd, kdc_offset);
1077 if (sp)
1078 krb5_storage_free(sp);
1079 fcc_unlock(context, fd);
1080 close(fd);
1082 return ret;
1087 * Variable containing the FILE based credential cache implemention.
1089 * @ingroup krb5_ccache
1092 KRB5_LIB_VARIABLE const krb5_cc_ops krb5_fcc_ops = {
1093 KRB5_CC_OPS_VERSION,
1094 "FILE",
1095 fcc_get_name,
1096 fcc_resolve,
1097 fcc_gen_new,
1098 fcc_initialize,
1099 fcc_destroy,
1100 fcc_close,
1101 fcc_store_cred,
1102 NULL, /* fcc_retrieve */
1103 fcc_get_principal,
1104 fcc_get_first,
1105 fcc_get_next,
1106 fcc_end_get,
1107 fcc_remove_cred,
1108 fcc_set_flags,
1109 fcc_get_version,
1110 fcc_get_cache_first,
1111 fcc_get_cache_next,
1112 fcc_end_cache_get,
1113 fcc_move,
1114 fcc_get_default_name,
1115 NULL,
1116 fcc_lastchange,
1117 fcc_set_kdc_offset,
1118 fcc_get_kdc_offset