More strict fcache rules
[heimdal.git] / lib / krb5 / fcache.c
blob27fc5d2851967228ebfd2b5da300c4a61c0a4778
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 fd;
400 if (FCACHE(id) == NULL)
401 return krb5_einval(context, 2);
403 filename = FILENAME(id);
405 if ((flags & O_CREAT) == 0) {
406 ret = lstat(filename, &sb1);
407 if (ret < 0) {
408 krb5_set_error_message(context, ret, N_("%s lstat(%s)", "file, error"),
409 operation, filename);
410 return errno;
415 fd = open(filename, flags, mode);
416 if(fd < 0) {
417 char buf[128];
418 ret = errno;
419 rk_strerror_r(ret, buf, sizeof(buf));
420 krb5_set_error_message(context, ret, N_("%s open(%s): %s", "file, error"),
421 operation, filename, buf);
422 return ret;
424 rk_cloexec(fd);
426 if ((flags & O_CREAT) == 0) {
428 ret = fstat(fd, &sb2);
429 if (ret < 0) {
430 krb5_clear_error_message(context);
431 return errno;
434 if (!S_ISREG(sb2.st_mode)) {
435 krb5_set_error_message(context, EPERM, N_("Refuses to open non files caches: FILE:%s", ""), filename);
436 close(fd);
437 return EPERM;
440 if (sb1.st_dev != sb2.st_dev || sb1.st_ino != sb2.st_ino) {
441 krb5_set_error_message(context, EPERM, N_("Refuses to open symlinks for caches FILE:%s", ""), filename);
442 close(fd);
443 return EPERM;
445 if (sb2.st_nlink != 1) {
446 krb5_set_error_message(context, EPERM, N_("Refuses to open hardlinks for caches FILE:%s", ""), filename);
447 close(fd);
448 return EPERM;
450 if ((sb2.st_mode & 077) != 0) {
451 krb5_set_error_message(context, EPERM,
452 N_("Refuses to open group/other readable files FILE:%s", ""), filename);
453 close(fd);
454 return EPERM;
459 if((ret = fcc_lock(context, id, fd, exclusive)) != 0) {
460 close(fd);
461 return ret;
463 *fd_ret = fd;
464 return 0;
467 static krb5_error_code KRB5_CALLCONV
468 fcc_initialize(krb5_context context,
469 krb5_ccache id,
470 krb5_principal primary_principal)
472 krb5_fcache *f = FCACHE(id);
473 int ret = 0;
474 int fd;
476 if (f == NULL)
477 return krb5_einval(context, 2);
479 unlink (f->filename);
481 ret = fcc_open(context, id, "initialize", &fd, O_RDWR | O_CREAT | O_EXCL | O_BINARY | O_CLOEXEC | O_NOFOLLOW, 0600);
482 if(ret)
483 return ret;
485 krb5_storage *sp;
486 sp = krb5_storage_emem();
487 krb5_storage_set_eof_code(sp, KRB5_CC_END);
488 if(context->fcache_vno != 0)
489 f->version = context->fcache_vno;
490 else
491 f->version = KRB5_FCC_FVNO_4;
492 ret |= krb5_store_int8(sp, 5);
493 ret |= krb5_store_int8(sp, f->version);
494 storage_set_flags(context, sp, f->version);
495 if(f->version == KRB5_FCC_FVNO_4 && ret == 0) {
496 /* V4 stuff */
497 if (context->kdc_sec_offset) {
498 ret |= krb5_store_int16 (sp, 12); /* length */
499 ret |= krb5_store_int16 (sp, FCC_TAG_DELTATIME); /* Tag */
500 ret |= krb5_store_int16 (sp, 8); /* length of data */
501 ret |= krb5_store_int32 (sp, context->kdc_sec_offset);
502 ret |= krb5_store_int32 (sp, context->kdc_usec_offset);
503 } else {
504 ret |= krb5_store_int16 (sp, 0);
507 ret |= krb5_store_principal(sp, primary_principal);
509 ret |= write_storage(context, sp, fd);
511 krb5_storage_free(sp);
513 fcc_unlock(context, fd);
514 if (close(fd) < 0)
515 if (ret == 0) {
516 char buf[128];
517 ret = errno;
518 rk_strerror_r(ret, buf, sizeof(buf));
519 krb5_set_error_message (context, ret, N_("close %s: %s", ""),
520 FILENAME(id), buf);
522 return ret;
525 static krb5_error_code KRB5_CALLCONV
526 fcc_close(krb5_context context,
527 krb5_ccache id)
529 if (FCACHE(id) == NULL)
530 return krb5_einval(context, 2);
532 free (FILENAME(id));
533 krb5_data_free(&id->data);
534 return 0;
537 static krb5_error_code KRB5_CALLCONV
538 fcc_destroy(krb5_context context,
539 krb5_ccache id)
541 if (FCACHE(id) == NULL)
542 return krb5_einval(context, 2);
544 _krb5_erase_file(context, FILENAME(id));
545 return 0;
548 static krb5_error_code KRB5_CALLCONV
549 fcc_store_cred(krb5_context context,
550 krb5_ccache id,
551 krb5_creds *creds)
553 int ret;
554 int fd;
556 ret = fcc_open(context, id, "store", &fd, O_WRONLY | O_APPEND | O_BINARY | O_CLOEXEC | O_NOFOLLOW, 0);
557 if(ret)
558 return ret;
560 krb5_storage *sp;
562 sp = krb5_storage_emem();
563 krb5_storage_set_eof_code(sp, KRB5_CC_END);
564 storage_set_flags(context, sp, FCACHE(id)->version);
565 if (!krb5_config_get_bool_default(context, NULL, TRUE,
566 "libdefaults",
567 "fcc-mit-ticketflags",
568 NULL))
569 krb5_storage_set_flags(sp, KRB5_STORAGE_CREDS_FLAGS_WRONG_BITORDER);
570 ret = krb5_store_creds(sp, creds);
571 if (ret == 0)
572 ret = write_storage(context, sp, fd);
573 krb5_storage_free(sp);
575 fcc_unlock(context, fd);
576 if (close(fd) < 0) {
577 if (ret == 0) {
578 char buf[128];
579 rk_strerror_r(ret, buf, sizeof(buf));
580 ret = errno;
581 krb5_set_error_message (context, ret, N_("close %s: %s", ""),
582 FILENAME(id), buf);
585 return ret;
588 static krb5_error_code
589 init_fcc(krb5_context context,
590 krb5_ccache id,
591 const char *operation,
592 krb5_storage **ret_sp,
593 int *ret_fd,
594 krb5_deltat *kdc_offset)
596 int fd;
597 int8_t pvno, tag;
598 krb5_storage *sp;
599 krb5_error_code ret;
601 if (kdc_offset)
602 *kdc_offset = 0;
604 ret = fcc_open(context, id, operation, &fd, O_RDONLY | O_BINARY | O_CLOEXEC | O_NOFOLLOW, 0);
605 if(ret)
606 return ret;
608 sp = krb5_storage_from_fd(fd);
609 if(sp == NULL) {
610 krb5_clear_error_message(context);
611 ret = ENOMEM;
612 goto out;
614 krb5_storage_set_eof_code(sp, KRB5_CC_END);
615 ret = krb5_ret_int8(sp, &pvno);
616 if(ret != 0) {
617 if(ret == KRB5_CC_END) {
618 ret = ENOENT;
619 krb5_set_error_message(context, ret,
620 N_("Empty credential cache file: %s", ""),
621 FILENAME(id));
622 } else
623 krb5_set_error_message(context, ret, N_("Error reading pvno "
624 "in cache file: %s", ""),
625 FILENAME(id));
626 goto out;
628 if(pvno != 5) {
629 ret = KRB5_CCACHE_BADVNO;
630 krb5_set_error_message(context, ret, N_("Bad version number in credential "
631 "cache file: %s", ""),
632 FILENAME(id));
633 goto out;
635 ret = krb5_ret_int8(sp, &tag); /* should not be host byte order */
636 if(ret != 0) {
637 ret = KRB5_CC_FORMAT;
638 krb5_set_error_message(context, ret, "Error reading tag in "
639 "cache file: %s", FILENAME(id));
640 goto out;
642 FCACHE(id)->version = tag;
643 storage_set_flags(context, sp, FCACHE(id)->version);
644 switch (tag) {
645 case KRB5_FCC_FVNO_4: {
646 int16_t length;
648 ret = krb5_ret_int16 (sp, &length);
649 if(ret) {
650 ret = KRB5_CC_FORMAT;
651 krb5_set_error_message(context, ret,
652 N_("Error reading tag length in "
653 "cache file: %s", ""), FILENAME(id));
654 goto out;
656 while(length > 0) {
657 int16_t dtag, data_len;
658 int i;
659 int8_t dummy;
661 ret = krb5_ret_int16 (sp, &dtag);
662 if(ret) {
663 ret = KRB5_CC_FORMAT;
664 krb5_set_error_message(context, ret, N_("Error reading dtag in "
665 "cache file: %s", ""),
666 FILENAME(id));
667 goto out;
669 ret = krb5_ret_int16 (sp, &data_len);
670 if(ret) {
671 ret = KRB5_CC_FORMAT;
672 krb5_set_error_message(context, ret,
673 N_("Error reading dlength "
674 "in cache file: %s",""),
675 FILENAME(id));
676 goto out;
678 switch (dtag) {
679 case FCC_TAG_DELTATIME : {
680 int32_t offset;
682 ret = krb5_ret_int32 (sp, &offset);
683 ret |= krb5_ret_int32 (sp, &context->kdc_usec_offset);
684 if(ret) {
685 ret = KRB5_CC_FORMAT;
686 krb5_set_error_message(context, ret,
687 N_("Error reading kdc_sec in "
688 "cache file: %s", ""),
689 FILENAME(id));
690 goto out;
692 context->kdc_sec_offset = offset;
693 if (kdc_offset)
694 *kdc_offset = offset;
695 break;
697 default :
698 for (i = 0; i < data_len; ++i) {
699 ret = krb5_ret_int8 (sp, &dummy);
700 if(ret) {
701 ret = KRB5_CC_FORMAT;
702 krb5_set_error_message(context, ret,
703 N_("Error reading unknown "
704 "tag in cache file: %s", ""),
705 FILENAME(id));
706 goto out;
709 break;
711 length -= 4 + data_len;
713 break;
715 case KRB5_FCC_FVNO_3:
716 case KRB5_FCC_FVNO_2:
717 case KRB5_FCC_FVNO_1:
718 break;
719 default :
720 ret = KRB5_CCACHE_BADVNO;
721 krb5_set_error_message(context, ret,
722 N_("Unknown version number (%d) in "
723 "credential cache file: %s", ""),
724 (int)tag, FILENAME(id));
725 goto out;
727 *ret_sp = sp;
728 *ret_fd = fd;
730 return 0;
731 out:
732 if(sp != NULL)
733 krb5_storage_free(sp);
734 fcc_unlock(context, fd);
735 close(fd);
736 return ret;
739 static krb5_error_code KRB5_CALLCONV
740 fcc_get_principal(krb5_context context,
741 krb5_ccache id,
742 krb5_principal *principal)
744 krb5_error_code ret;
745 int fd;
746 krb5_storage *sp;
748 ret = init_fcc (context, id, "get-pricipal", &sp, &fd, NULL);
749 if (ret)
750 return ret;
751 ret = krb5_ret_principal(sp, principal);
752 if (ret)
753 krb5_clear_error_message(context);
754 krb5_storage_free(sp);
755 fcc_unlock(context, fd);
756 close(fd);
757 return ret;
760 static krb5_error_code KRB5_CALLCONV
761 fcc_end_get (krb5_context context,
762 krb5_ccache id,
763 krb5_cc_cursor *cursor);
765 static krb5_error_code KRB5_CALLCONV
766 fcc_get_first (krb5_context context,
767 krb5_ccache id,
768 krb5_cc_cursor *cursor)
770 krb5_error_code ret;
771 krb5_principal principal;
773 if (FCACHE(id) == NULL)
774 return krb5_einval(context, 2);
776 *cursor = malloc(sizeof(struct fcc_cursor));
777 if (*cursor == NULL) {
778 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
779 return ENOMEM;
781 memset(*cursor, 0, sizeof(struct fcc_cursor));
783 ret = init_fcc(context, id, "get-frist", &FCC_CURSOR(*cursor)->sp,
784 &FCC_CURSOR(*cursor)->fd, NULL);
785 if (ret) {
786 free(*cursor);
787 *cursor = NULL;
788 return ret;
790 ret = krb5_ret_principal (FCC_CURSOR(*cursor)->sp, &principal);
791 if(ret) {
792 krb5_clear_error_message(context);
793 fcc_end_get(context, id, cursor);
794 return ret;
796 krb5_free_principal (context, principal);
797 fcc_unlock(context, FCC_CURSOR(*cursor)->fd);
798 return 0;
801 static krb5_error_code KRB5_CALLCONV
802 fcc_get_next (krb5_context context,
803 krb5_ccache id,
804 krb5_cc_cursor *cursor,
805 krb5_creds *creds)
807 krb5_error_code ret;
809 if (FCACHE(id) == NULL)
810 return krb5_einval(context, 2);
812 if (FCC_CURSOR(*cursor) == NULL)
813 return krb5_einval(context, 3);
815 if((ret = fcc_lock(context, id, FCC_CURSOR(*cursor)->fd, FALSE)) != 0)
816 return ret;
818 ret = krb5_ret_creds(FCC_CURSOR(*cursor)->sp, creds);
819 if (ret)
820 krb5_clear_error_message(context);
822 fcc_unlock(context, FCC_CURSOR(*cursor)->fd);
823 return ret;
826 static krb5_error_code KRB5_CALLCONV
827 fcc_end_get (krb5_context context,
828 krb5_ccache id,
829 krb5_cc_cursor *cursor)
832 if (FCACHE(id) == NULL)
833 return krb5_einval(context, 2);
835 if (FCC_CURSOR(*cursor) == NULL)
836 return krb5_einval(context, 3);
838 krb5_storage_free(FCC_CURSOR(*cursor)->sp);
839 close (FCC_CURSOR(*cursor)->fd);
840 free(*cursor);
841 *cursor = NULL;
842 return 0;
845 static krb5_error_code KRB5_CALLCONV
846 fcc_remove_cred(krb5_context context,
847 krb5_ccache id,
848 krb5_flags which,
849 krb5_creds *cred)
851 krb5_error_code ret;
852 krb5_ccache copy, newfile;
853 char *newname = NULL;
854 int fd;
856 if (FCACHE(id) == NULL)
857 return krb5_einval(context, 2);
859 ret = krb5_cc_new_unique(context, krb5_cc_type_memory, NULL, &copy);
860 if (ret)
861 return ret;
863 ret = krb5_cc_copy_cache(context, id, copy);
864 if (ret) {
865 krb5_cc_destroy(context, copy);
866 return ret;
869 ret = krb5_cc_remove_cred(context, copy, which, cred);
870 if (ret) {
871 krb5_cc_destroy(context, copy);
872 return ret;
875 ret = asprintf(&newname, "FILE:%s.XXXXXX", FILENAME(id));
876 if (ret < 0 || newname == NULL) {
877 krb5_cc_destroy(context, copy);
878 return ENOMEM;
881 fd = mkstemp(&newname[5]);
882 if (fd < 0) {
883 ret = errno;
884 krb5_cc_destroy(context, copy);
885 return ret;
887 close(fd);
889 ret = krb5_cc_resolve(context, newname, &newfile);
890 if (ret) {
891 unlink(&newname[5]);
892 free(newname);
893 krb5_cc_destroy(context, copy);
894 return ret;
897 ret = krb5_cc_copy_cache(context, copy, newfile);
898 krb5_cc_destroy(context, copy);
899 if (ret) {
900 free(newname);
901 krb5_cc_destroy(context, newfile);
902 return ret;
905 ret = rk_rename(&newname[5], FILENAME(id));
906 if (ret)
907 ret = errno;
908 free(newname);
909 krb5_cc_close(context, newfile);
911 return ret;
914 static krb5_error_code KRB5_CALLCONV
915 fcc_set_flags(krb5_context context,
916 krb5_ccache id,
917 krb5_flags flags)
919 if (FCACHE(id) == NULL)
920 return krb5_einval(context, 2);
922 return 0; /* XXX */
925 static int KRB5_CALLCONV
926 fcc_get_version(krb5_context context,
927 krb5_ccache id)
929 if (FCACHE(id) == NULL)
930 return -1;
932 return FCACHE(id)->version;
935 struct fcache_iter {
936 int first;
939 static krb5_error_code KRB5_CALLCONV
940 fcc_get_cache_first(krb5_context context, krb5_cc_cursor *cursor)
942 struct fcache_iter *iter;
944 iter = calloc(1, sizeof(*iter));
945 if (iter == NULL) {
946 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
947 return ENOMEM;
949 iter->first = 1;
950 *cursor = iter;
951 return 0;
954 static krb5_error_code KRB5_CALLCONV
955 fcc_get_cache_next(krb5_context context, krb5_cc_cursor cursor, krb5_ccache *id)
957 struct fcache_iter *iter = cursor;
958 krb5_error_code ret;
959 const char *fn;
960 char *expandedfn = NULL;
962 if (iter == NULL)
963 return krb5_einval(context, 2);
965 if (!iter->first) {
966 krb5_clear_error_message(context);
967 return KRB5_CC_END;
969 iter->first = 0;
972 * Can't call krb5_cc_default_name here since it refers back to
973 * krb5_cc_cache_match() which will call back into this function.
975 * Just use the default value if its set, otherwise, use the
976 * default hardcoded value.
978 fn = context->default_cc_name;
979 if (fn == NULL || strncasecmp(fn, "FILE:", 5) != 0) {
980 ret = _krb5_expand_default_cc_name(context,
981 KRB5_DEFAULT_CCNAME_FILE,
982 &expandedfn);
983 if (ret)
984 return ret;
985 fn = expandedfn;
987 /* check if file exists, don't return a non existant "next" */
988 if (strncasecmp(fn, "FILE:", 5) == 0) {
989 struct stat sb;
990 ret = stat(fn + 5, &sb);
991 if (ret) {
992 ret = KRB5_CC_END;
993 goto out;
996 ret = krb5_cc_resolve(context, fn, id);
997 out:
998 if (expandedfn)
999 free(expandedfn);
1001 return ret;
1004 static krb5_error_code KRB5_CALLCONV
1005 fcc_end_cache_get(krb5_context context, krb5_cc_cursor cursor)
1007 struct fcache_iter *iter = cursor;
1009 if (iter == NULL)
1010 return krb5_einval(context, 2);
1012 free(iter);
1013 return 0;
1016 static krb5_error_code KRB5_CALLCONV
1017 fcc_move(krb5_context context, krb5_ccache from, krb5_ccache to)
1019 krb5_error_code ret = 0;
1021 ret = rk_rename(FILENAME(from), FILENAME(to));
1023 if (ret && errno != EXDEV) {
1024 char buf[128];
1025 ret = errno;
1026 rk_strerror_r(ret, buf, sizeof(buf));
1027 krb5_set_error_message(context, ret,
1028 N_("Rename of file from %s "
1029 "to %s failed: %s", ""),
1030 FILENAME(from), FILENAME(to), buf);
1031 return ret;
1032 } else if (ret && errno == EXDEV) {
1033 /* make a copy and delete the orignal */
1034 krb5_ssize_t sz1, sz2;
1035 int fd1, fd2;
1036 char buf[BUFSIZ];
1038 ret = fcc_open(context, from, "move/from", &fd1, O_RDONLY | O_BINARY | O_CLOEXEC | O_NOFOLLOW, 0);
1039 if(ret)
1040 return ret;
1042 unlink(FILENAME(to));
1044 ret = fcc_open(context, to, "move/to", &fd2,
1045 O_WRONLY | O_CREAT | O_EXCL | O_BINARY | O_CLOEXEC | O_NOFOLLOW, 0600);
1046 if(ret)
1047 goto out1;
1049 while((sz1 = read(fd1, buf, sizeof(buf))) > 0) {
1050 sz2 = write(fd2, buf, sz1);
1051 if (sz1 != sz2) {
1052 ret = EIO;
1053 krb5_set_error_message(context, ret,
1054 N_("Failed to write data from one file "
1055 "credential cache to the other", ""));
1056 goto out2;
1059 if (sz1 < 0) {
1060 ret = EIO;
1061 krb5_set_error_message(context, ret,
1062 N_("Failed to read data from one file "
1063 "credential cache to the other", ""));
1064 goto out2;
1066 out2:
1067 fcc_unlock(context, fd2);
1068 close(fd2);
1070 out1:
1071 fcc_unlock(context, fd1);
1072 close(fd1);
1074 _krb5_erase_file(context, FILENAME(from));
1076 if (ret) {
1077 _krb5_erase_file(context, FILENAME(to));
1078 return ret;
1082 /* make sure ->version is uptodate */
1084 krb5_storage *sp;
1085 int fd;
1086 if ((ret = init_fcc (context, to, "move", &sp, &fd, NULL)) == 0) {
1087 if (sp)
1088 krb5_storage_free(sp);
1089 fcc_unlock(context, fd);
1090 close(fd);
1094 fcc_close(context, from);
1096 return ret;
1099 static krb5_error_code KRB5_CALLCONV
1100 fcc_get_default_name(krb5_context context, char **str)
1102 return _krb5_expand_default_cc_name(context,
1103 KRB5_DEFAULT_CCNAME_FILE,
1104 str);
1107 static krb5_error_code KRB5_CALLCONV
1108 fcc_lastchange(krb5_context context, krb5_ccache id, krb5_timestamp *mtime)
1110 krb5_error_code ret;
1111 struct stat sb;
1112 int fd;
1114 ret = fcc_open(context, id, "lastchange", &fd, O_RDONLY | O_BINARY | O_CLOEXEC | O_NOFOLLOW, 0);
1115 if(ret)
1116 return ret;
1117 ret = fstat(fd, &sb);
1118 close(fd);
1119 if (ret) {
1120 ret = errno;
1121 krb5_set_error_message(context, ret, N_("Failed to stat cache file", ""));
1122 return ret;
1124 *mtime = sb.st_mtime;
1125 return 0;
1128 static krb5_error_code KRB5_CALLCONV
1129 fcc_set_kdc_offset(krb5_context context, krb5_ccache id, krb5_deltat kdc_offset)
1131 return 0;
1134 static krb5_error_code KRB5_CALLCONV
1135 fcc_get_kdc_offset(krb5_context context, krb5_ccache id, krb5_deltat *kdc_offset)
1137 krb5_error_code ret;
1138 krb5_storage *sp = NULL;
1139 int fd;
1140 ret = init_fcc(context, id, "get-kdc-offset", &sp, &fd, kdc_offset);
1141 if (sp)
1142 krb5_storage_free(sp);
1143 fcc_unlock(context, fd);
1144 close(fd);
1146 return ret;
1151 * Variable containing the FILE based credential cache implemention.
1153 * @ingroup krb5_ccache
1156 KRB5_LIB_VARIABLE const krb5_cc_ops krb5_fcc_ops = {
1157 KRB5_CC_OPS_VERSION,
1158 "FILE",
1159 fcc_get_name,
1160 fcc_resolve,
1161 fcc_gen_new,
1162 fcc_initialize,
1163 fcc_destroy,
1164 fcc_close,
1165 fcc_store_cred,
1166 NULL, /* fcc_retrieve */
1167 fcc_get_principal,
1168 fcc_get_first,
1169 fcc_get_next,
1170 fcc_end_get,
1171 fcc_remove_cred,
1172 fcc_set_flags,
1173 fcc_get_version,
1174 fcc_get_cache_first,
1175 fcc_get_cache_next,
1176 fcc_end_cache_get,
1177 fcc_move,
1178 fcc_get_default_name,
1179 NULL,
1180 fcc_lastchange,
1181 fcc_set_kdc_offset,
1182 fcc_get_kdc_offset