s4:heimdal: import lorikeet-heimdal-201001120029 (commit a5e675fed7c5db8a7370b77ed0bf...
[Samba/ekacnet.git] / source4 / heimdal / lib / krb5 / fcache.c
blob67c4c744449b80f0f5effef682ca4b2d271bd07b
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*
62 fcc_get_name(krb5_context context,
63 krb5_ccache id)
65 return FILENAME(id);
68 int
69 _krb5_xlock(krb5_context context, int fd, krb5_boolean exclusive,
70 const char *filename)
72 int ret;
73 #ifdef HAVE_FCNTL
74 struct flock l;
76 l.l_start = 0;
77 l.l_len = 0;
78 l.l_type = exclusive ? F_WRLCK : F_RDLCK;
79 l.l_whence = SEEK_SET;
80 ret = fcntl(fd, F_SETLKW, &l);
81 #else
82 ret = flock(fd, exclusive ? LOCK_EX : LOCK_SH);
83 #endif
84 if(ret < 0)
85 ret = errno;
86 if(ret == EACCES) /* fcntl can return EACCES instead of EAGAIN */
87 ret = EAGAIN;
89 switch (ret) {
90 case 0:
91 break;
92 case EINVAL: /* filesystem doesn't support locking, let the user have it */
93 ret = 0;
94 break;
95 case EAGAIN:
96 krb5_set_error_message(context, ret,
97 N_("timed out locking cache file %s", "file"),
98 filename);
99 break;
100 default: {
101 char buf[128];
102 rk_strerror_r(ret, buf, sizeof(buf));
103 krb5_set_error_message(context, ret,
104 N_("error locking cache file %s: %s",
105 "file, error"), filename, buf);
106 break;
109 return ret;
113 _krb5_xunlock(krb5_context context, int fd)
115 int ret;
116 #ifdef HAVE_FCNTL
117 struct flock l;
118 l.l_start = 0;
119 l.l_len = 0;
120 l.l_type = F_UNLCK;
121 l.l_whence = SEEK_SET;
122 ret = fcntl(fd, F_SETLKW, &l);
123 #else
124 ret = flock(fd, LOCK_UN);
125 #endif
126 if (ret < 0)
127 ret = errno;
128 switch (ret) {
129 case 0:
130 break;
131 case EINVAL: /* filesystem doesn't support locking, let the user have it */
132 ret = 0;
133 break;
134 default: {
135 char buf[128];
136 rk_strerror_r(ret, buf, sizeof(buf));
137 krb5_set_error_message(context, ret,
138 N_("Failed to unlock file: %s", ""), buf);
139 break;
142 return ret;
145 static krb5_error_code
146 write_storage(krb5_context context, krb5_storage *sp, int fd)
148 krb5_error_code ret;
149 krb5_data data;
150 ssize_t sret;
152 ret = krb5_storage_to_data(sp, &data);
153 if (ret) {
154 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
155 return ret;
157 sret = write(fd, data.data, data.length);
158 ret = (sret != data.length);
159 krb5_data_free(&data);
160 if (ret) {
161 ret = errno;
162 krb5_set_error_message(context, ret,
163 N_("Failed to write FILE credential data", ""));
164 return ret;
166 return 0;
170 static krb5_error_code
171 fcc_lock(krb5_context context, krb5_ccache id,
172 int fd, krb5_boolean exclusive)
174 return _krb5_xlock(context, fd, exclusive, fcc_get_name(context, id));
177 static krb5_error_code
178 fcc_unlock(krb5_context context, int fd)
180 return _krb5_xunlock(context, fd);
183 static krb5_error_code
184 fcc_resolve(krb5_context context, krb5_ccache *id, const char *res)
186 krb5_fcache *f;
187 f = malloc(sizeof(*f));
188 if(f == NULL) {
189 krb5_set_error_message(context, KRB5_CC_NOMEM,
190 N_("malloc: out of memory", ""));
191 return KRB5_CC_NOMEM;
193 f->filename = strdup(res);
194 if(f->filename == NULL){
195 free(f);
196 krb5_set_error_message(context, KRB5_CC_NOMEM,
197 N_("malloc: out of memory", ""));
198 return KRB5_CC_NOMEM;
200 f->version = 0;
201 (*id)->data.data = f;
202 (*id)->data.length = sizeof(*f);
203 return 0;
207 * Try to scrub the contents of `filename' safely.
210 static int
211 scrub_file (int fd)
213 off_t pos;
214 char buf[128];
216 pos = lseek(fd, 0, SEEK_END);
217 if (pos < 0)
218 return errno;
219 if (lseek(fd, 0, SEEK_SET) < 0)
220 return errno;
221 memset(buf, 0, sizeof(buf));
222 while(pos > 0) {
223 ssize_t tmp = write(fd, buf, min(sizeof(buf), pos));
225 if (tmp < 0)
226 return errno;
227 pos -= tmp;
229 #ifdef _MSC_VER
230 _commit (fd);
231 #else
232 fsync (fd);
233 #endif
234 return 0;
238 * Erase `filename' if it exists, trying to remove the contents if
239 * it's `safe'. We always try to remove the file, it it exists. It's
240 * only overwritten if it's a regular file (not a symlink and not a
241 * hardlink)
244 krb5_error_code
245 _krb5_erase_file(krb5_context context, const char *filename)
247 int fd;
248 struct stat sb1, sb2;
249 int ret;
251 ret = lstat (filename, &sb1);
252 if (ret < 0)
253 return errno;
255 fd = open(filename, O_RDWR | O_BINARY);
256 if(fd < 0) {
257 if(errno == ENOENT)
258 return 0;
259 else
260 return errno;
262 rk_cloexec(fd);
263 ret = _krb5_xlock(context, fd, 1, filename);
264 if (ret) {
265 close(fd);
266 return ret;
268 if (unlink(filename) < 0) {
269 _krb5_xunlock(context, fd);
270 close (fd);
271 return errno;
273 ret = fstat (fd, &sb2);
274 if (ret < 0) {
275 _krb5_xunlock(context, fd);
276 close (fd);
277 return errno;
280 /* check if someone was playing with symlinks */
282 if (sb1.st_dev != sb2.st_dev || sb1.st_ino != sb2.st_ino) {
283 _krb5_xunlock(context, fd);
284 close (fd);
285 return EPERM;
288 /* there are still hard links to this file */
290 if (sb2.st_nlink != 0) {
291 _krb5_xunlock(context, fd);
292 close (fd);
293 return 0;
296 ret = scrub_file (fd);
297 if (ret) {
298 _krb5_xunlock(context, fd);
299 close(fd);
300 return ret;
302 ret = _krb5_xunlock(context, fd);
303 close (fd);
304 return ret;
307 static krb5_error_code
308 fcc_gen_new(krb5_context context, krb5_ccache *id)
310 krb5_fcache *f;
311 int fd;
312 char *file;
314 f = malloc(sizeof(*f));
315 if(f == NULL) {
316 krb5_set_error_message(context, KRB5_CC_NOMEM,
317 N_("malloc: out of memory", ""));
318 return KRB5_CC_NOMEM;
320 asprintf (&file, "%sXXXXXX", KRB5_DEFAULT_CCFILE_ROOT);
321 if(file == NULL) {
322 free(f);
323 krb5_set_error_message(context, KRB5_CC_NOMEM,
324 N_("malloc: out of memory", ""));
325 return KRB5_CC_NOMEM;
327 #ifdef KRB5_USE_PATH_TOKENS
329 char * exp_file = NULL;
330 krb5_error_code ec;
332 ec = _krb5_expand_path_tokens(context, file, &exp_file);
334 if (ec == 0) {
335 free(file);
336 file = exp_file;
337 } else {
338 free(file);
339 return ec;
342 #endif
343 fd = mkstemp(file);
344 if(fd < 0) {
345 int ret = errno;
346 krb5_set_error_message(context, ret, N_("mkstemp %s failed", ""), file);
347 free(f);
348 free(file);
349 return ret;
351 close(fd);
352 f->filename = file;
353 f->version = 0;
354 (*id)->data.data = f;
355 (*id)->data.length = sizeof(*f);
356 return 0;
359 static void
360 storage_set_flags(krb5_context context, krb5_storage *sp, int vno)
362 int flags = 0;
363 switch(vno) {
364 case KRB5_FCC_FVNO_1:
365 flags |= KRB5_STORAGE_PRINCIPAL_WRONG_NUM_COMPONENTS;
366 flags |= KRB5_STORAGE_PRINCIPAL_NO_NAME_TYPE;
367 flags |= KRB5_STORAGE_HOST_BYTEORDER;
368 break;
369 case KRB5_FCC_FVNO_2:
370 flags |= KRB5_STORAGE_HOST_BYTEORDER;
371 break;
372 case KRB5_FCC_FVNO_3:
373 flags |= KRB5_STORAGE_KEYBLOCK_KEYTYPE_TWICE;
374 break;
375 case KRB5_FCC_FVNO_4:
376 break;
377 default:
378 krb5_abortx(context,
379 "storage_set_flags called with bad vno (%x)", vno);
381 krb5_storage_set_flags(sp, flags);
384 static krb5_error_code
385 fcc_open(krb5_context context,
386 krb5_ccache id,
387 int *fd_ret,
388 int flags,
389 mode_t mode)
391 krb5_boolean exclusive = ((flags | O_WRONLY) == flags ||
392 (flags | O_RDWR) == flags);
393 krb5_error_code ret;
394 const char *filename = FILENAME(id);
395 int fd;
396 fd = open(filename, flags, mode);
397 if(fd < 0) {
398 char buf[128];
399 ret = errno;
400 rk_strerror_r(ret, buf, sizeof(buf));
401 krb5_set_error_message(context, ret, N_("open(%s): %s", "file, error"),
402 filename, buf);
403 return ret;
405 rk_cloexec(fd);
407 if((ret = fcc_lock(context, id, fd, exclusive)) != 0) {
408 close(fd);
409 return ret;
411 *fd_ret = fd;
412 return 0;
415 static krb5_error_code
416 fcc_initialize(krb5_context context,
417 krb5_ccache id,
418 krb5_principal primary_principal)
420 krb5_fcache *f = FCACHE(id);
421 int ret = 0;
422 int fd;
423 char *filename = f->filename;
425 unlink (filename);
427 ret = fcc_open(context, id, &fd, O_RDWR | O_CREAT | O_EXCL | O_BINARY | O_CLOEXEC, 0600);
428 if(ret)
429 return ret;
431 krb5_storage *sp;
432 sp = krb5_storage_emem();
433 krb5_storage_set_eof_code(sp, KRB5_CC_END);
434 if(context->fcache_vno != 0)
435 f->version = context->fcache_vno;
436 else
437 f->version = KRB5_FCC_FVNO_4;
438 ret |= krb5_store_int8(sp, 5);
439 ret |= krb5_store_int8(sp, f->version);
440 storage_set_flags(context, sp, f->version);
441 if(f->version == KRB5_FCC_FVNO_4 && ret == 0) {
442 /* V4 stuff */
443 if (context->kdc_sec_offset) {
444 ret |= krb5_store_int16 (sp, 12); /* length */
445 ret |= krb5_store_int16 (sp, FCC_TAG_DELTATIME); /* Tag */
446 ret |= krb5_store_int16 (sp, 8); /* length of data */
447 ret |= krb5_store_int32 (sp, context->kdc_sec_offset);
448 ret |= krb5_store_int32 (sp, context->kdc_usec_offset);
449 } else {
450 ret |= krb5_store_int16 (sp, 0);
453 ret |= krb5_store_principal(sp, primary_principal);
455 ret |= write_storage(context, sp, fd);
457 krb5_storage_free(sp);
459 fcc_unlock(context, fd);
460 if (close(fd) < 0)
461 if (ret == 0) {
462 char buf[128];
463 ret = errno;
464 rk_strerror_r(ret, buf, sizeof(buf));
465 krb5_set_error_message (context, ret, N_("close %s: %s", ""),
466 FILENAME(id), buf);
468 return ret;
471 static krb5_error_code
472 fcc_close(krb5_context context,
473 krb5_ccache id)
475 free (FILENAME(id));
476 krb5_data_free(&id->data);
477 return 0;
480 static krb5_error_code
481 fcc_destroy(krb5_context context,
482 krb5_ccache id)
484 _krb5_erase_file(context, FILENAME(id));
485 return 0;
488 static krb5_error_code
489 fcc_store_cred(krb5_context context,
490 krb5_ccache id,
491 krb5_creds *creds)
493 int ret;
494 int fd;
496 ret = fcc_open(context, id, &fd, O_WRONLY | O_APPEND | O_BINARY | O_CLOEXEC, 0);
497 if(ret)
498 return ret;
500 krb5_storage *sp;
502 sp = krb5_storage_emem();
503 krb5_storage_set_eof_code(sp, KRB5_CC_END);
504 storage_set_flags(context, sp, FCACHE(id)->version);
505 if (!krb5_config_get_bool_default(context, NULL, TRUE,
506 "libdefaults",
507 "fcc-mit-ticketflags",
508 NULL))
509 krb5_storage_set_flags(sp, KRB5_STORAGE_CREDS_FLAGS_WRONG_BITORDER);
510 ret = krb5_store_creds(sp, creds);
511 if (ret == 0)
512 ret = write_storage(context, sp, fd);
513 krb5_storage_free(sp);
515 fcc_unlock(context, fd);
516 if (close(fd) < 0) {
517 if (ret == 0) {
518 char buf[128];
519 rk_strerror_r(ret, buf, sizeof(buf));
520 ret = errno;
521 krb5_set_error_message (context, ret, N_("close %s: %s", ""),
522 FILENAME(id), buf);
525 return ret;
528 static krb5_error_code
529 init_fcc (krb5_context context,
530 krb5_ccache id,
531 krb5_storage **ret_sp,
532 int *ret_fd,
533 krb5_deltat *kdc_offset)
535 int fd;
536 int8_t pvno, tag;
537 krb5_storage *sp;
538 krb5_error_code ret;
540 if (kdc_offset)
541 *kdc_offset = 0;
543 ret = fcc_open(context, id, &fd, O_RDONLY | O_BINARY | O_CLOEXEC, 0);
544 if(ret)
545 return ret;
547 sp = krb5_storage_from_fd(fd);
548 if(sp == NULL) {
549 krb5_clear_error_message(context);
550 ret = ENOMEM;
551 goto out;
553 krb5_storage_set_eof_code(sp, KRB5_CC_END);
554 ret = krb5_ret_int8(sp, &pvno);
555 if(ret != 0) {
556 if(ret == KRB5_CC_END) {
557 ret = ENOENT;
558 krb5_set_error_message(context, ret,
559 N_("Empty credential cache file: %s", ""),
560 FILENAME(id));
561 } else
562 krb5_set_error_message(context, ret, N_("Error reading pvno "
563 "in cache file: %s", ""),
564 FILENAME(id));
565 goto out;
567 if(pvno != 5) {
568 ret = KRB5_CCACHE_BADVNO;
569 krb5_set_error_message(context, ret, N_("Bad version number in credential "
570 "cache file: %s", ""),
571 FILENAME(id));
572 goto out;
574 ret = krb5_ret_int8(sp, &tag); /* should not be host byte order */
575 if(ret != 0) {
576 ret = KRB5_CC_FORMAT;
577 krb5_set_error_message(context, ret, "Error reading tag in "
578 "cache file: %s", FILENAME(id));
579 goto out;
581 FCACHE(id)->version = tag;
582 storage_set_flags(context, sp, FCACHE(id)->version);
583 switch (tag) {
584 case KRB5_FCC_FVNO_4: {
585 int16_t length;
587 ret = krb5_ret_int16 (sp, &length);
588 if(ret) {
589 ret = KRB5_CC_FORMAT;
590 krb5_set_error_message(context, ret,
591 N_("Error reading tag length in "
592 "cache file: %s", ""), FILENAME(id));
593 goto out;
595 while(length > 0) {
596 int16_t dtag, data_len;
597 int i;
598 int8_t dummy;
600 ret = krb5_ret_int16 (sp, &dtag);
601 if(ret) {
602 ret = KRB5_CC_FORMAT;
603 krb5_set_error_message(context, ret, N_("Error reading dtag in "
604 "cache file: %s", ""),
605 FILENAME(id));
606 goto out;
608 ret = krb5_ret_int16 (sp, &data_len);
609 if(ret) {
610 ret = KRB5_CC_FORMAT;
611 krb5_set_error_message(context, ret,
612 N_("Error reading dlength "
613 "in cache file: %s",""),
614 FILENAME(id));
615 goto out;
617 switch (dtag) {
618 case FCC_TAG_DELTATIME : {
619 int32_t offset;
621 ret = krb5_ret_int32 (sp, &offset);
622 ret |= krb5_ret_int32 (sp, &context->kdc_usec_offset);
623 if(ret) {
624 ret = KRB5_CC_FORMAT;
625 krb5_set_error_message(context, ret,
626 N_("Error reading kdc_sec in "
627 "cache file: %s", ""),
628 FILENAME(id));
629 goto out;
631 context->kdc_sec_offset = offset;
632 if (kdc_offset)
633 *kdc_offset = offset;
634 break;
636 default :
637 for (i = 0; i < data_len; ++i) {
638 ret = krb5_ret_int8 (sp, &dummy);
639 if(ret) {
640 ret = KRB5_CC_FORMAT;
641 krb5_set_error_message(context, ret,
642 N_("Error reading unknown "
643 "tag in cache file: %s", ""),
644 FILENAME(id));
645 goto out;
648 break;
650 length -= 4 + data_len;
652 break;
654 case KRB5_FCC_FVNO_3:
655 case KRB5_FCC_FVNO_2:
656 case KRB5_FCC_FVNO_1:
657 break;
658 default :
659 ret = KRB5_CCACHE_BADVNO;
660 krb5_set_error_message(context, ret,
661 N_("Unknown version number (%d) in "
662 "credential cache file: %s", ""),
663 (int)tag, FILENAME(id));
664 goto out;
666 *ret_sp = sp;
667 *ret_fd = fd;
669 return 0;
670 out:
671 if(sp != NULL)
672 krb5_storage_free(sp);
673 fcc_unlock(context, fd);
674 close(fd);
675 return ret;
678 static krb5_error_code
679 fcc_get_principal(krb5_context context,
680 krb5_ccache id,
681 krb5_principal *principal)
683 krb5_error_code ret;
684 int fd;
685 krb5_storage *sp;
687 ret = init_fcc (context, id, &sp, &fd, NULL);
688 if (ret)
689 return ret;
690 ret = krb5_ret_principal(sp, principal);
691 if (ret)
692 krb5_clear_error_message(context);
693 krb5_storage_free(sp);
694 fcc_unlock(context, fd);
695 close(fd);
696 return ret;
699 static krb5_error_code
700 fcc_end_get (krb5_context context,
701 krb5_ccache id,
702 krb5_cc_cursor *cursor);
704 static krb5_error_code
705 fcc_get_first (krb5_context context,
706 krb5_ccache id,
707 krb5_cc_cursor *cursor)
709 krb5_error_code ret;
710 krb5_principal principal;
712 *cursor = malloc(sizeof(struct fcc_cursor));
713 if (*cursor == NULL) {
714 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
715 return ENOMEM;
717 memset(*cursor, 0, sizeof(struct fcc_cursor));
719 ret = init_fcc (context, id, &FCC_CURSOR(*cursor)->sp,
720 &FCC_CURSOR(*cursor)->fd, NULL);
721 if (ret) {
722 free(*cursor);
723 *cursor = NULL;
724 return ret;
726 ret = krb5_ret_principal (FCC_CURSOR(*cursor)->sp, &principal);
727 if(ret) {
728 krb5_clear_error_message(context);
729 fcc_end_get(context, id, cursor);
730 return ret;
732 krb5_free_principal (context, principal);
733 fcc_unlock(context, FCC_CURSOR(*cursor)->fd);
734 return 0;
737 static krb5_error_code
738 fcc_get_next (krb5_context context,
739 krb5_ccache id,
740 krb5_cc_cursor *cursor,
741 krb5_creds *creds)
743 krb5_error_code ret;
744 if((ret = fcc_lock(context, id, FCC_CURSOR(*cursor)->fd, FALSE)) != 0)
745 return ret;
747 ret = krb5_ret_creds(FCC_CURSOR(*cursor)->sp, creds);
748 if (ret)
749 krb5_clear_error_message(context);
751 fcc_unlock(context, FCC_CURSOR(*cursor)->fd);
752 return ret;
755 static krb5_error_code
756 fcc_end_get (krb5_context context,
757 krb5_ccache id,
758 krb5_cc_cursor *cursor)
760 krb5_storage_free(FCC_CURSOR(*cursor)->sp);
761 close (FCC_CURSOR(*cursor)->fd);
762 free(*cursor);
763 *cursor = NULL;
764 return 0;
767 static krb5_error_code
768 fcc_remove_cred(krb5_context context,
769 krb5_ccache id,
770 krb5_flags which,
771 krb5_creds *cred)
773 krb5_error_code ret;
774 krb5_ccache copy, newfile;
775 char *newname;
776 int fd;
778 ret = krb5_cc_new_unique(context, krb5_cc_type_memory, NULL, &copy);
779 if (ret)
780 return ret;
782 ret = krb5_cc_copy_cache(context, id, copy);
783 if (ret) {
784 krb5_cc_destroy(context, copy);
785 return ret;
788 ret = krb5_cc_remove_cred(context, copy, which, cred);
789 if (ret) {
790 krb5_cc_destroy(context, copy);
791 return ret;
794 asprintf(&newname, "FILE:%s.XXXXXX", FILENAME(id));
795 if (newname == NULL) {
796 krb5_cc_destroy(context, copy);
797 return ret;
800 fd = mkstemp(&newname[5]);
801 if (fd < 0) {
802 ret = errno;
803 krb5_cc_destroy(context, copy);
804 return ret;
806 close(fd);
808 ret = krb5_cc_resolve(context, newname, &newfile);
809 if (ret) {
810 unlink(&newname[5]);
811 free(newname);
812 krb5_cc_destroy(context, copy);
813 return ret;
816 ret = krb5_cc_copy_cache(context, copy, newfile);
817 krb5_cc_destroy(context, copy);
818 if (ret) {
819 free(newname);
820 krb5_cc_destroy(context, newfile);
821 return ret;
824 ret = rename(&newname[5], FILENAME(id));
825 if (ret)
826 ret = errno;
827 free(newname);
828 krb5_cc_close(context, newfile);
830 return ret;
833 static krb5_error_code
834 fcc_set_flags(krb5_context context,
835 krb5_ccache id,
836 krb5_flags flags)
838 return 0; /* XXX */
841 static int
842 fcc_get_version(krb5_context context,
843 krb5_ccache id)
845 return FCACHE(id)->version;
848 struct fcache_iter {
849 int first;
852 static krb5_error_code
853 fcc_get_cache_first(krb5_context context, krb5_cc_cursor *cursor)
855 struct fcache_iter *iter;
857 iter = calloc(1, sizeof(*iter));
858 if (iter == NULL) {
859 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
860 return ENOMEM;
862 iter->first = 1;
863 *cursor = iter;
864 return 0;
867 static krb5_error_code
868 fcc_get_cache_next(krb5_context context, krb5_cc_cursor cursor, krb5_ccache *id)
870 struct fcache_iter *iter = cursor;
871 krb5_error_code ret;
872 const char *fn;
873 char *expandedfn = NULL;
875 if (!iter->first) {
876 krb5_clear_error_message(context);
877 return KRB5_CC_END;
879 iter->first = 0;
881 fn = krb5_cc_default_name(context);
882 if (fn == NULL || strncasecmp(fn, "FILE:", 5) != 0) {
883 ret = _krb5_expand_default_cc_name(context,
884 KRB5_DEFAULT_CCNAME_FILE,
885 &expandedfn);
886 if (ret)
887 return ret;
888 fn = expandedfn;
890 /* check if file exists, don't return a non existant "next" */
891 if (strncasecmp(fn, "FILE:", 5) == 0) {
892 struct stat sb;
893 ret = stat(fn + 5, &sb);
894 if (ret) {
895 ret = KRB5_CC_END;
896 goto out;
899 ret = krb5_cc_resolve(context, fn, id);
900 out:
901 if (expandedfn)
902 free(expandedfn);
904 return ret;
907 static krb5_error_code
908 fcc_end_cache_get(krb5_context context, krb5_cc_cursor cursor)
910 struct fcache_iter *iter = cursor;
911 free(iter);
912 return 0;
915 static krb5_error_code
916 fcc_move(krb5_context context, krb5_ccache from, krb5_ccache to)
918 krb5_error_code ret = 0;
920 ret = rename(FILENAME(from), FILENAME(to));
921 #ifdef RENAME_DOES_NOT_UNLINK
922 if (ret && (errno == EEXIST || errno == EACCES)) {
923 ret = unlink(FILENAME(to));
924 if (ret == 0) {
925 ret = rename(FILENAME(from), FILENAME(to));
928 #endif
930 if (ret && errno != EXDEV) {
931 char buf[128];
932 ret = errno;
933 rk_strerror_r(ret, buf, sizeof(buf));
934 krb5_set_error_message(context, ret,
935 N_("Rename of file from %s "
936 "to %s failed: %s", ""),
937 FILENAME(from), FILENAME(to), buf);
938 return ret;
939 } else if (ret && errno == EXDEV) {
940 /* make a copy and delete the orignal */
941 krb5_ssize_t sz1, sz2;
942 int fd1, fd2;
943 char buf[BUFSIZ];
945 ret = fcc_open(context, from, &fd1, O_RDONLY | O_BINARY | O_CLOEXEC, 0);
946 if(ret)
947 return ret;
949 unlink(FILENAME(to));
951 ret = fcc_open(context, to, &fd2,
952 O_WRONLY | O_CREAT | O_EXCL | O_BINARY | O_CLOEXEC, 0600);
953 if(ret)
954 goto out1;
956 while((sz1 = read(fd1, buf, sizeof(buf))) > 0) {
957 sz2 = write(fd2, buf, sz1);
958 if (sz1 != sz2) {
959 ret = EIO;
960 krb5_set_error_message(context, ret,
961 N_("Failed to write data from one file "
962 "credential cache to the other", ""));
963 goto out2;
966 if (sz1 < 0) {
967 ret = EIO;
968 krb5_set_error_message(context, ret,
969 N_("Failed to read data from one file "
970 "credential cache to the other", ""));
971 goto out2;
973 out2:
974 fcc_unlock(context, fd2);
975 close(fd2);
977 out1:
978 fcc_unlock(context, fd1);
979 close(fd1);
981 _krb5_erase_file(context, FILENAME(from));
983 if (ret) {
984 _krb5_erase_file(context, FILENAME(to));
985 return ret;
989 /* make sure ->version is uptodate */
991 krb5_storage *sp;
992 int fd;
993 ret = init_fcc (context, to, &sp, &fd, NULL);
994 if (sp)
995 krb5_storage_free(sp);
996 fcc_unlock(context, fd);
997 close(fd);
1000 fcc_close(context, from);
1002 return ret;
1005 static krb5_error_code
1006 fcc_get_default_name(krb5_context context, char **str)
1008 return _krb5_expand_default_cc_name(context,
1009 KRB5_DEFAULT_CCNAME_FILE,
1010 str);
1013 static krb5_error_code
1014 fcc_lastchange(krb5_context context, krb5_ccache id, krb5_timestamp *mtime)
1016 krb5_error_code ret;
1017 struct stat sb;
1018 int fd;
1020 ret = fcc_open(context, id, &fd, O_RDONLY | O_BINARY | O_CLOEXEC, 0);
1021 if(ret)
1022 return ret;
1023 ret = fstat(fd, &sb);
1024 close(fd);
1025 if (ret) {
1026 ret = errno;
1027 krb5_set_error_message(context, ret, N_("Failed to stat cache file", ""));
1028 return ret;
1030 *mtime = sb.st_mtime;
1031 return 0;
1034 static krb5_error_code
1035 fcc_set_kdc_offset(krb5_context context, krb5_ccache id, krb5_deltat kdc_offset)
1037 return 0;
1040 static krb5_error_code
1041 fcc_get_kdc_offset(krb5_context context, krb5_ccache id, krb5_deltat *kdc_offset)
1043 krb5_error_code ret;
1044 krb5_storage *sp;
1045 int fd;
1046 ret = init_fcc(context, id, &sp, &fd, kdc_offset);
1047 if (sp)
1048 krb5_storage_free(sp);
1049 fcc_unlock(context, fd);
1050 close(fd);
1052 return ret;
1057 * Variable containing the FILE based credential cache implemention.
1059 * @ingroup krb5_ccache
1062 KRB5_LIB_VARIABLE const krb5_cc_ops krb5_fcc_ops = {
1063 KRB5_CC_OPS_VERSION,
1064 "FILE",
1065 fcc_get_name,
1066 fcc_resolve,
1067 fcc_gen_new,
1068 fcc_initialize,
1069 fcc_destroy,
1070 fcc_close,
1071 fcc_store_cred,
1072 NULL, /* fcc_retrieve */
1073 fcc_get_principal,
1074 fcc_get_first,
1075 fcc_get_next,
1076 fcc_end_get,
1077 fcc_remove_cred,
1078 fcc_set_flags,
1079 fcc_get_version,
1080 fcc_get_cache_first,
1081 fcc_get_cache_next,
1082 fcc_end_cache_get,
1083 fcc_move,
1084 fcc_get_default_name,
1085 NULL,
1086 fcc_lastchange,
1087 fcc_set_kdc_offset,
1088 fcc_get_kdc_offset