add [libdefaults]fcache_strict_checking to gate the strict checking, defaults to on
[heimdal.git] / lib / krb5 / fcache.c
blob1003df1c63743ffb28d290d27976f0e6eee4e749
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 KRB5_LIB_FUNCTION int KRB5_LIB_CALL
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;
115 KRB5_LIB_FUNCTION int KRB5_LIB_CALL
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;
227 size_t wr = sizeof(buf);
228 if (wr > pos)
229 wr = (size_t)pos;
230 tmp = write(fd, buf, wr);
232 if (tmp < 0)
233 return errno;
234 pos -= tmp;
236 #ifdef _MSC_VER
237 _commit (fd);
238 #else
239 fsync (fd);
240 #endif
241 return 0;
245 * Erase `filename' if it exists, trying to remove the contents if
246 * it's `safe'. We always try to remove the file, it it exists. It's
247 * only overwritten if it's a regular file (not a symlink and not a
248 * hardlink)
251 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
252 _krb5_erase_file(krb5_context context, const char *filename)
254 int fd;
255 struct stat sb1, sb2;
256 int ret;
258 ret = lstat (filename, &sb1);
259 if (ret < 0)
260 return errno;
262 fd = open(filename, O_RDWR | O_BINARY);
263 if(fd < 0) {
264 if(errno == ENOENT)
265 return 0;
266 else
267 return errno;
269 rk_cloexec(fd);
270 ret = _krb5_xlock(context, fd, 1, filename);
271 if (ret) {
272 close(fd);
273 return ret;
275 if (unlink(filename) < 0) {
276 _krb5_xunlock(context, fd);
277 close (fd);
278 return errno;
280 ret = fstat(fd, &sb2);
281 if (ret < 0) {
282 _krb5_xunlock(context, fd);
283 close (fd);
284 return errno;
287 /* check if someone was playing with symlinks */
289 if (sb1.st_dev != sb2.st_dev || sb1.st_ino != sb2.st_ino) {
290 _krb5_xunlock(context, fd);
291 close(fd);
292 return EPERM;
295 /* there are still hard links to this file */
297 if (sb2.st_nlink != 0) {
298 _krb5_xunlock(context, fd);
299 close(fd);
300 return 0;
303 ret = scrub_file(fd);
304 if (ret) {
305 _krb5_xunlock(context, fd);
306 close(fd);
307 return ret;
309 ret = _krb5_xunlock(context, fd);
310 close(fd);
311 return ret;
314 static krb5_error_code KRB5_CALLCONV
315 fcc_gen_new(krb5_context context, krb5_ccache *id)
317 char *file = NULL, *exp_file = NULL;
318 krb5_error_code ret;
319 krb5_fcache *f;
320 int fd;
322 f = malloc(sizeof(*f));
323 if(f == NULL) {
324 krb5_set_error_message(context, KRB5_CC_NOMEM,
325 N_("malloc: out of memory", ""));
326 return KRB5_CC_NOMEM;
328 ret = asprintf(&file, "%sXXXXXX", KRB5_DEFAULT_CCFILE_ROOT);
329 if(ret < 0 || file == NULL) {
330 free(f);
331 krb5_set_error_message(context, KRB5_CC_NOMEM,
332 N_("malloc: out of memory", ""));
333 return KRB5_CC_NOMEM;
335 ret = _krb5_expand_path_tokens(context, file, &exp_file);
336 free(file);
337 if (ret) {
338 free(f);
339 return ret;
342 file = exp_file;
344 fd = mkstemp(exp_file);
345 if(fd < 0) {
346 ret = (krb5_error_code)errno;
347 krb5_set_error_message(context, ret, N_("mkstemp %s failed", ""), exp_file);
348 free(f);
349 free(exp_file);
350 return ret;
352 close(fd);
353 f->filename = exp_file;
354 f->version = 0;
355 (*id)->data.data = f;
356 (*id)->data.length = sizeof(*f);
357 return 0;
360 static void
361 storage_set_flags(krb5_context context, krb5_storage *sp, int vno)
363 int flags = 0;
364 switch(vno) {
365 case KRB5_FCC_FVNO_1:
366 flags |= KRB5_STORAGE_PRINCIPAL_WRONG_NUM_COMPONENTS;
367 flags |= KRB5_STORAGE_PRINCIPAL_NO_NAME_TYPE;
368 flags |= KRB5_STORAGE_HOST_BYTEORDER;
369 break;
370 case KRB5_FCC_FVNO_2:
371 flags |= KRB5_STORAGE_HOST_BYTEORDER;
372 break;
373 case KRB5_FCC_FVNO_3:
374 flags |= KRB5_STORAGE_KEYBLOCK_KEYTYPE_TWICE;
375 break;
376 case KRB5_FCC_FVNO_4:
377 break;
378 default:
379 krb5_abortx(context,
380 "storage_set_flags called with bad vno (%x)", vno);
382 krb5_storage_set_flags(sp, flags);
385 static krb5_error_code KRB5_CALLCONV
386 fcc_open(krb5_context context,
387 krb5_ccache id,
388 const char *operation,
389 int *fd_ret,
390 int flags,
391 mode_t mode)
393 krb5_boolean exclusive = ((flags | O_WRONLY) == flags ||
394 (flags | O_RDWR) == flags);
395 krb5_error_code ret;
396 const char *filename;
397 struct stat sb1, sb2;
398 int strict_checking;;
399 int fd;
401 if (FCACHE(id) == NULL)
402 return krb5_einval(context, 2);
404 filename = FILENAME(id);
406 strict_checking = (flags & O_CREAT) == 0 &&
407 (context->flags & KRB5_CTX_F_FCACHE_STRICT_CHECKING) != 0;
409 if (strict_checking) {
410 ret = lstat(filename, &sb1);
411 if (ret < 0) {
412 krb5_set_error_message(context, ret, N_("%s lstat(%s)", "file, error"),
413 operation, filename);
414 return errno;
419 fd = open(filename, flags, mode);
420 if(fd < 0) {
421 char buf[128];
422 ret = errno;
423 rk_strerror_r(ret, buf, sizeof(buf));
424 krb5_set_error_message(context, ret, N_("%s open(%s): %s", "file, error"),
425 operation, filename, buf);
426 return ret;
428 rk_cloexec(fd);
430 if (strict_checking) {
432 ret = fstat(fd, &sb2);
433 if (ret < 0) {
434 krb5_clear_error_message(context);
435 return errno;
438 if (!S_ISREG(sb2.st_mode)) {
439 krb5_set_error_message(context, EPERM, N_("Refuses to open non files caches: FILE:%s", ""), filename);
440 close(fd);
441 return EPERM;
444 if (sb1.st_dev != sb2.st_dev || sb1.st_ino != sb2.st_ino) {
445 krb5_set_error_message(context, EPERM, N_("Refuses to open symlinks for caches FILE:%s", ""), filename);
446 close(fd);
447 return EPERM;
449 if (sb2.st_nlink != 1) {
450 krb5_set_error_message(context, EPERM, N_("Refuses to open hardlinks for caches FILE:%s", ""), filename);
451 close(fd);
452 return EPERM;
454 if ((sb2.st_mode & 077) != 0) {
455 krb5_set_error_message(context, EPERM,
456 N_("Refuses to open group/other readable files FILE:%s", ""), filename);
457 close(fd);
458 return EPERM;
463 if((ret = fcc_lock(context, id, fd, exclusive)) != 0) {
464 close(fd);
465 return ret;
467 *fd_ret = fd;
468 return 0;
471 static krb5_error_code KRB5_CALLCONV
472 fcc_initialize(krb5_context context,
473 krb5_ccache id,
474 krb5_principal primary_principal)
476 krb5_fcache *f = FCACHE(id);
477 int ret = 0;
478 int fd;
480 if (f == NULL)
481 return krb5_einval(context, 2);
483 unlink (f->filename);
485 ret = fcc_open(context, id, "initialize", &fd, O_RDWR | O_CREAT | O_EXCL | O_BINARY | O_CLOEXEC | O_NOFOLLOW, 0600);
486 if(ret)
487 return ret;
489 krb5_storage *sp;
490 sp = krb5_storage_emem();
491 krb5_storage_set_eof_code(sp, KRB5_CC_END);
492 if(context->fcache_vno != 0)
493 f->version = context->fcache_vno;
494 else
495 f->version = KRB5_FCC_FVNO_4;
496 ret |= krb5_store_int8(sp, 5);
497 ret |= krb5_store_int8(sp, f->version);
498 storage_set_flags(context, sp, f->version);
499 if(f->version == KRB5_FCC_FVNO_4 && ret == 0) {
500 /* V4 stuff */
501 if (context->kdc_sec_offset) {
502 ret |= krb5_store_int16 (sp, 12); /* length */
503 ret |= krb5_store_int16 (sp, FCC_TAG_DELTATIME); /* Tag */
504 ret |= krb5_store_int16 (sp, 8); /* length of data */
505 ret |= krb5_store_int32 (sp, context->kdc_sec_offset);
506 ret |= krb5_store_int32 (sp, context->kdc_usec_offset);
507 } else {
508 ret |= krb5_store_int16 (sp, 0);
511 ret |= krb5_store_principal(sp, primary_principal);
513 ret |= write_storage(context, sp, fd);
515 krb5_storage_free(sp);
517 fcc_unlock(context, fd);
518 if (close(fd) < 0)
519 if (ret == 0) {
520 char buf[128];
521 ret = errno;
522 rk_strerror_r(ret, buf, sizeof(buf));
523 krb5_set_error_message (context, ret, N_("close %s: %s", ""),
524 FILENAME(id), buf);
526 return ret;
529 static krb5_error_code KRB5_CALLCONV
530 fcc_close(krb5_context context,
531 krb5_ccache id)
533 if (FCACHE(id) == NULL)
534 return krb5_einval(context, 2);
536 free (FILENAME(id));
537 krb5_data_free(&id->data);
538 return 0;
541 static krb5_error_code KRB5_CALLCONV
542 fcc_destroy(krb5_context context,
543 krb5_ccache id)
545 if (FCACHE(id) == NULL)
546 return krb5_einval(context, 2);
548 _krb5_erase_file(context, FILENAME(id));
549 return 0;
552 static krb5_error_code KRB5_CALLCONV
553 fcc_store_cred(krb5_context context,
554 krb5_ccache id,
555 krb5_creds *creds)
557 int ret;
558 int fd;
560 ret = fcc_open(context, id, "store", &fd, O_WRONLY | O_APPEND | O_BINARY | O_CLOEXEC | O_NOFOLLOW, 0);
561 if(ret)
562 return ret;
564 krb5_storage *sp;
566 sp = krb5_storage_emem();
567 krb5_storage_set_eof_code(sp, KRB5_CC_END);
568 storage_set_flags(context, sp, FCACHE(id)->version);
569 if (!krb5_config_get_bool_default(context, NULL, TRUE,
570 "libdefaults",
571 "fcc-mit-ticketflags",
572 NULL))
573 krb5_storage_set_flags(sp, KRB5_STORAGE_CREDS_FLAGS_WRONG_BITORDER);
574 ret = krb5_store_creds(sp, creds);
575 if (ret == 0)
576 ret = write_storage(context, sp, fd);
577 krb5_storage_free(sp);
579 fcc_unlock(context, fd);
580 if (close(fd) < 0) {
581 if (ret == 0) {
582 char buf[128];
583 rk_strerror_r(ret, buf, sizeof(buf));
584 ret = errno;
585 krb5_set_error_message (context, ret, N_("close %s: %s", ""),
586 FILENAME(id), buf);
589 return ret;
592 static krb5_error_code
593 init_fcc(krb5_context context,
594 krb5_ccache id,
595 const char *operation,
596 krb5_storage **ret_sp,
597 int *ret_fd,
598 krb5_deltat *kdc_offset)
600 int fd;
601 int8_t pvno, tag;
602 krb5_storage *sp;
603 krb5_error_code ret;
605 if (kdc_offset)
606 *kdc_offset = 0;
608 ret = fcc_open(context, id, operation, &fd, O_RDONLY | O_BINARY | O_CLOEXEC | O_NOFOLLOW, 0);
609 if(ret)
610 return ret;
612 sp = krb5_storage_from_fd(fd);
613 if(sp == NULL) {
614 krb5_clear_error_message(context);
615 ret = ENOMEM;
616 goto out;
618 krb5_storage_set_eof_code(sp, KRB5_CC_END);
619 ret = krb5_ret_int8(sp, &pvno);
620 if(ret != 0) {
621 if(ret == KRB5_CC_END) {
622 ret = ENOENT;
623 krb5_set_error_message(context, ret,
624 N_("Empty credential cache file: %s", ""),
625 FILENAME(id));
626 } else
627 krb5_set_error_message(context, ret, N_("Error reading pvno "
628 "in cache file: %s", ""),
629 FILENAME(id));
630 goto out;
632 if(pvno != 5) {
633 ret = KRB5_CCACHE_BADVNO;
634 krb5_set_error_message(context, ret, N_("Bad version number in credential "
635 "cache file: %s", ""),
636 FILENAME(id));
637 goto out;
639 ret = krb5_ret_int8(sp, &tag); /* should not be host byte order */
640 if(ret != 0) {
641 ret = KRB5_CC_FORMAT;
642 krb5_set_error_message(context, ret, "Error reading tag in "
643 "cache file: %s", FILENAME(id));
644 goto out;
646 FCACHE(id)->version = tag;
647 storage_set_flags(context, sp, FCACHE(id)->version);
648 switch (tag) {
649 case KRB5_FCC_FVNO_4: {
650 int16_t length;
652 ret = krb5_ret_int16 (sp, &length);
653 if(ret) {
654 ret = KRB5_CC_FORMAT;
655 krb5_set_error_message(context, ret,
656 N_("Error reading tag length in "
657 "cache file: %s", ""), FILENAME(id));
658 goto out;
660 while(length > 0) {
661 int16_t dtag, data_len;
662 int i;
663 int8_t dummy;
665 ret = krb5_ret_int16 (sp, &dtag);
666 if(ret) {
667 ret = KRB5_CC_FORMAT;
668 krb5_set_error_message(context, ret, N_("Error reading dtag in "
669 "cache file: %s", ""),
670 FILENAME(id));
671 goto out;
673 ret = krb5_ret_int16 (sp, &data_len);
674 if(ret) {
675 ret = KRB5_CC_FORMAT;
676 krb5_set_error_message(context, ret,
677 N_("Error reading dlength "
678 "in cache file: %s",""),
679 FILENAME(id));
680 goto out;
682 switch (dtag) {
683 case FCC_TAG_DELTATIME : {
684 int32_t offset;
686 ret = krb5_ret_int32 (sp, &offset);
687 ret |= krb5_ret_int32 (sp, &context->kdc_usec_offset);
688 if(ret) {
689 ret = KRB5_CC_FORMAT;
690 krb5_set_error_message(context, ret,
691 N_("Error reading kdc_sec in "
692 "cache file: %s", ""),
693 FILENAME(id));
694 goto out;
696 context->kdc_sec_offset = offset;
697 if (kdc_offset)
698 *kdc_offset = offset;
699 break;
701 default :
702 for (i = 0; i < data_len; ++i) {
703 ret = krb5_ret_int8 (sp, &dummy);
704 if(ret) {
705 ret = KRB5_CC_FORMAT;
706 krb5_set_error_message(context, ret,
707 N_("Error reading unknown "
708 "tag in cache file: %s", ""),
709 FILENAME(id));
710 goto out;
713 break;
715 length -= 4 + data_len;
717 break;
719 case KRB5_FCC_FVNO_3:
720 case KRB5_FCC_FVNO_2:
721 case KRB5_FCC_FVNO_1:
722 break;
723 default :
724 ret = KRB5_CCACHE_BADVNO;
725 krb5_set_error_message(context, ret,
726 N_("Unknown version number (%d) in "
727 "credential cache file: %s", ""),
728 (int)tag, FILENAME(id));
729 goto out;
731 *ret_sp = sp;
732 *ret_fd = fd;
734 return 0;
735 out:
736 if(sp != NULL)
737 krb5_storage_free(sp);
738 fcc_unlock(context, fd);
739 close(fd);
740 return ret;
743 static krb5_error_code KRB5_CALLCONV
744 fcc_get_principal(krb5_context context,
745 krb5_ccache id,
746 krb5_principal *principal)
748 krb5_error_code ret;
749 int fd;
750 krb5_storage *sp;
752 ret = init_fcc (context, id, "get-pricipal", &sp, &fd, NULL);
753 if (ret)
754 return ret;
755 ret = krb5_ret_principal(sp, principal);
756 if (ret)
757 krb5_clear_error_message(context);
758 krb5_storage_free(sp);
759 fcc_unlock(context, fd);
760 close(fd);
761 return ret;
764 static krb5_error_code KRB5_CALLCONV
765 fcc_end_get (krb5_context context,
766 krb5_ccache id,
767 krb5_cc_cursor *cursor);
769 static krb5_error_code KRB5_CALLCONV
770 fcc_get_first (krb5_context context,
771 krb5_ccache id,
772 krb5_cc_cursor *cursor)
774 krb5_error_code ret;
775 krb5_principal principal;
777 if (FCACHE(id) == NULL)
778 return krb5_einval(context, 2);
780 *cursor = malloc(sizeof(struct fcc_cursor));
781 if (*cursor == NULL) {
782 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
783 return ENOMEM;
785 memset(*cursor, 0, sizeof(struct fcc_cursor));
787 ret = init_fcc(context, id, "get-frist", &FCC_CURSOR(*cursor)->sp,
788 &FCC_CURSOR(*cursor)->fd, NULL);
789 if (ret) {
790 free(*cursor);
791 *cursor = NULL;
792 return ret;
794 ret = krb5_ret_principal (FCC_CURSOR(*cursor)->sp, &principal);
795 if(ret) {
796 krb5_clear_error_message(context);
797 fcc_end_get(context, id, cursor);
798 return ret;
800 krb5_free_principal (context, principal);
801 fcc_unlock(context, FCC_CURSOR(*cursor)->fd);
802 return 0;
805 static krb5_error_code KRB5_CALLCONV
806 fcc_get_next (krb5_context context,
807 krb5_ccache id,
808 krb5_cc_cursor *cursor,
809 krb5_creds *creds)
811 krb5_error_code ret;
813 if (FCACHE(id) == NULL)
814 return krb5_einval(context, 2);
816 if (FCC_CURSOR(*cursor) == NULL)
817 return krb5_einval(context, 3);
819 if((ret = fcc_lock(context, id, FCC_CURSOR(*cursor)->fd, FALSE)) != 0)
820 return ret;
822 ret = krb5_ret_creds(FCC_CURSOR(*cursor)->sp, creds);
823 if (ret)
824 krb5_clear_error_message(context);
826 fcc_unlock(context, FCC_CURSOR(*cursor)->fd);
827 return ret;
830 static krb5_error_code KRB5_CALLCONV
831 fcc_end_get (krb5_context context,
832 krb5_ccache id,
833 krb5_cc_cursor *cursor)
836 if (FCACHE(id) == NULL)
837 return krb5_einval(context, 2);
839 if (FCC_CURSOR(*cursor) == NULL)
840 return krb5_einval(context, 3);
842 krb5_storage_free(FCC_CURSOR(*cursor)->sp);
843 close (FCC_CURSOR(*cursor)->fd);
844 free(*cursor);
845 *cursor = NULL;
846 return 0;
849 static krb5_error_code KRB5_CALLCONV
850 fcc_remove_cred(krb5_context context,
851 krb5_ccache id,
852 krb5_flags which,
853 krb5_creds *cred)
855 krb5_error_code ret;
856 krb5_ccache copy, newfile;
857 char *newname = NULL;
858 int fd;
860 if (FCACHE(id) == NULL)
861 return krb5_einval(context, 2);
863 ret = krb5_cc_new_unique(context, krb5_cc_type_memory, NULL, &copy);
864 if (ret)
865 return ret;
867 ret = krb5_cc_copy_cache(context, id, copy);
868 if (ret) {
869 krb5_cc_destroy(context, copy);
870 return ret;
873 ret = krb5_cc_remove_cred(context, copy, which, cred);
874 if (ret) {
875 krb5_cc_destroy(context, copy);
876 return ret;
879 ret = asprintf(&newname, "FILE:%s.XXXXXX", FILENAME(id));
880 if (ret < 0 || newname == NULL) {
881 krb5_cc_destroy(context, copy);
882 return ENOMEM;
885 fd = mkstemp(&newname[5]);
886 if (fd < 0) {
887 ret = errno;
888 krb5_cc_destroy(context, copy);
889 return ret;
891 close(fd);
893 ret = krb5_cc_resolve(context, newname, &newfile);
894 if (ret) {
895 unlink(&newname[5]);
896 free(newname);
897 krb5_cc_destroy(context, copy);
898 return ret;
901 ret = krb5_cc_copy_cache(context, copy, newfile);
902 krb5_cc_destroy(context, copy);
903 if (ret) {
904 free(newname);
905 krb5_cc_destroy(context, newfile);
906 return ret;
909 ret = rk_rename(&newname[5], FILENAME(id));
910 if (ret)
911 ret = errno;
912 free(newname);
913 krb5_cc_close(context, newfile);
915 return ret;
918 static krb5_error_code KRB5_CALLCONV
919 fcc_set_flags(krb5_context context,
920 krb5_ccache id,
921 krb5_flags flags)
923 if (FCACHE(id) == NULL)
924 return krb5_einval(context, 2);
926 return 0; /* XXX */
929 static int KRB5_CALLCONV
930 fcc_get_version(krb5_context context,
931 krb5_ccache id)
933 if (FCACHE(id) == NULL)
934 return -1;
936 return FCACHE(id)->version;
939 struct fcache_iter {
940 int first;
943 static krb5_error_code KRB5_CALLCONV
944 fcc_get_cache_first(krb5_context context, krb5_cc_cursor *cursor)
946 struct fcache_iter *iter;
948 iter = calloc(1, sizeof(*iter));
949 if (iter == NULL) {
950 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
951 return ENOMEM;
953 iter->first = 1;
954 *cursor = iter;
955 return 0;
958 static krb5_error_code KRB5_CALLCONV
959 fcc_get_cache_next(krb5_context context, krb5_cc_cursor cursor, krb5_ccache *id)
961 struct fcache_iter *iter = cursor;
962 krb5_error_code ret;
963 const char *fn;
964 char *expandedfn = NULL;
966 if (iter == NULL)
967 return krb5_einval(context, 2);
969 if (!iter->first) {
970 krb5_clear_error_message(context);
971 return KRB5_CC_END;
973 iter->first = 0;
976 * Can't call krb5_cc_default_name here since it refers back to
977 * krb5_cc_cache_match() which will call back into this function.
979 * Just use the default value if its set, otherwise, use the
980 * default hardcoded value.
982 fn = context->default_cc_name;
983 if (fn == NULL || strncasecmp(fn, "FILE:", 5) != 0) {
984 ret = _krb5_expand_default_cc_name(context,
985 KRB5_DEFAULT_CCNAME_FILE,
986 &expandedfn);
987 if (ret)
988 return ret;
989 fn = expandedfn;
991 /* check if file exists, don't return a non existant "next" */
992 if (strncasecmp(fn, "FILE:", 5) == 0) {
993 struct stat sb;
994 ret = stat(fn + 5, &sb);
995 if (ret) {
996 ret = KRB5_CC_END;
997 goto out;
1000 ret = krb5_cc_resolve(context, fn, id);
1001 out:
1002 if (expandedfn)
1003 free(expandedfn);
1005 return ret;
1008 static krb5_error_code KRB5_CALLCONV
1009 fcc_end_cache_get(krb5_context context, krb5_cc_cursor cursor)
1011 struct fcache_iter *iter = cursor;
1013 if (iter == NULL)
1014 return krb5_einval(context, 2);
1016 free(iter);
1017 return 0;
1020 static krb5_error_code KRB5_CALLCONV
1021 fcc_move(krb5_context context, krb5_ccache from, krb5_ccache to)
1023 krb5_error_code ret = 0;
1025 ret = rk_rename(FILENAME(from), FILENAME(to));
1027 if (ret && errno != EXDEV) {
1028 char buf[128];
1029 ret = errno;
1030 rk_strerror_r(ret, buf, sizeof(buf));
1031 krb5_set_error_message(context, ret,
1032 N_("Rename of file from %s "
1033 "to %s failed: %s", ""),
1034 FILENAME(from), FILENAME(to), buf);
1035 return ret;
1036 } else if (ret && errno == EXDEV) {
1037 /* make a copy and delete the orignal */
1038 krb5_ssize_t sz1, sz2;
1039 int fd1, fd2;
1040 char buf[BUFSIZ];
1042 ret = fcc_open(context, from, "move/from", &fd1, O_RDONLY | O_BINARY | O_CLOEXEC | O_NOFOLLOW, 0);
1043 if(ret)
1044 return ret;
1046 unlink(FILENAME(to));
1048 ret = fcc_open(context, to, "move/to", &fd2,
1049 O_WRONLY | O_CREAT | O_EXCL | O_BINARY | O_CLOEXEC | O_NOFOLLOW, 0600);
1050 if(ret)
1051 goto out1;
1053 while((sz1 = read(fd1, buf, sizeof(buf))) > 0) {
1054 sz2 = write(fd2, buf, sz1);
1055 if (sz1 != sz2) {
1056 ret = EIO;
1057 krb5_set_error_message(context, ret,
1058 N_("Failed to write data from one file "
1059 "credential cache to the other", ""));
1060 goto out2;
1063 if (sz1 < 0) {
1064 ret = EIO;
1065 krb5_set_error_message(context, ret,
1066 N_("Failed to read data from one file "
1067 "credential cache to the other", ""));
1068 goto out2;
1070 out2:
1071 fcc_unlock(context, fd2);
1072 close(fd2);
1074 out1:
1075 fcc_unlock(context, fd1);
1076 close(fd1);
1078 _krb5_erase_file(context, FILENAME(from));
1080 if (ret) {
1081 _krb5_erase_file(context, FILENAME(to));
1082 return ret;
1086 /* make sure ->version is uptodate */
1088 krb5_storage *sp;
1089 int fd;
1090 if ((ret = init_fcc (context, to, "move", &sp, &fd, NULL)) == 0) {
1091 if (sp)
1092 krb5_storage_free(sp);
1093 fcc_unlock(context, fd);
1094 close(fd);
1098 fcc_close(context, from);
1100 return ret;
1103 static krb5_error_code KRB5_CALLCONV
1104 fcc_get_default_name(krb5_context context, char **str)
1106 return _krb5_expand_default_cc_name(context,
1107 KRB5_DEFAULT_CCNAME_FILE,
1108 str);
1111 static krb5_error_code KRB5_CALLCONV
1112 fcc_lastchange(krb5_context context, krb5_ccache id, krb5_timestamp *mtime)
1114 krb5_error_code ret;
1115 struct stat sb;
1116 int fd;
1118 ret = fcc_open(context, id, "lastchange", &fd, O_RDONLY | O_BINARY | O_CLOEXEC | O_NOFOLLOW, 0);
1119 if(ret)
1120 return ret;
1121 ret = fstat(fd, &sb);
1122 close(fd);
1123 if (ret) {
1124 ret = errno;
1125 krb5_set_error_message(context, ret, N_("Failed to stat cache file", ""));
1126 return ret;
1128 *mtime = sb.st_mtime;
1129 return 0;
1132 static krb5_error_code KRB5_CALLCONV
1133 fcc_set_kdc_offset(krb5_context context, krb5_ccache id, krb5_deltat kdc_offset)
1135 return 0;
1138 static krb5_error_code KRB5_CALLCONV
1139 fcc_get_kdc_offset(krb5_context context, krb5_ccache id, krb5_deltat *kdc_offset)
1141 krb5_error_code ret;
1142 krb5_storage *sp = NULL;
1143 int fd;
1144 ret = init_fcc(context, id, "get-kdc-offset", &sp, &fd, kdc_offset);
1145 if (sp)
1146 krb5_storage_free(sp);
1147 fcc_unlock(context, fd);
1148 close(fd);
1150 return ret;
1155 * Variable containing the FILE based credential cache implemention.
1157 * @ingroup krb5_ccache
1160 KRB5_LIB_VARIABLE const krb5_cc_ops krb5_fcc_ops = {
1161 KRB5_CC_OPS_VERSION,
1162 "FILE",
1163 fcc_get_name,
1164 fcc_resolve,
1165 fcc_gen_new,
1166 fcc_initialize,
1167 fcc_destroy,
1168 fcc_close,
1169 fcc_store_cred,
1170 NULL, /* fcc_retrieve */
1171 fcc_get_principal,
1172 fcc_get_first,
1173 fcc_get_next,
1174 fcc_end_get,
1175 fcc_remove_cred,
1176 fcc_set_flags,
1177 fcc_get_version,
1178 fcc_get_cache_first,
1179 fcc_get_cache_next,
1180 fcc_end_cache_get,
1181 fcc_move,
1182 fcc_get_default_name,
1183 NULL,
1184 fcc_lastchange,
1185 fcc_set_kdc_offset,
1186 fcc_get_kdc_offset