krb5: Red Hat gssproxy FILE ccache remove cred compatibility
[heimdal.git] / lib / krb5 / fcache.c
blob1c32389fac4a246703eb78154b4fbcb45ca1d0f0
1 /*
2 * Copyright (c) 1997 - 2017 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 char *res;
41 char *sub;
42 char *tmpfn;
43 int version;
44 }krb5_fcache;
46 struct fcc_cursor {
47 int fd;
48 off_t cred_start;
49 off_t cred_end;
50 krb5_storage *sp;
53 #define KRB5_FCC_FVNO_1 1
54 #define KRB5_FCC_FVNO_2 2
55 #define KRB5_FCC_FVNO_3 3
56 #define KRB5_FCC_FVNO_4 4
58 #define FCC_TAG_DELTATIME 1
60 #define FCACHE(X) ((krb5_fcache*)(X)->data.data)
62 #define FILENAME(X) (FCACHE(X)->filename)
63 #define TMPFILENAME(X) (FCACHE(X)->tmpfn)
64 #define RESFILENAME(X) (FCACHE(X)->res)
65 #define SUBFILENAME(X) (FCACHE(X)->sub)
67 #define FCC_CURSOR(C) ((struct fcc_cursor*)(C))
69 static krb5_error_code KRB5_CALLCONV
70 fcc_get_name_2(krb5_context context,
71 krb5_ccache id,
72 const char **name,
73 const char **colname,
74 const char **sub)
76 if (FCACHE(id) == NULL)
77 return KRB5_CC_NOTFOUND;
79 if (name)
80 *name = FILENAME(id);
81 if (colname)
82 *colname = FILENAME(id);
83 if (sub)
84 *sub = NULL;
85 return 0;
88 KRB5_LIB_FUNCTION int KRB5_LIB_CALL
89 _krb5_xlock(krb5_context context, int fd, krb5_boolean exclusive,
90 const char *filename)
92 int ret;
93 #ifdef HAVE_FCNTL
94 struct flock l;
96 l.l_start = 0;
97 l.l_len = 0;
98 l.l_type = exclusive ? F_WRLCK : F_RDLCK;
99 l.l_whence = SEEK_SET;
100 ret = fcntl(fd, F_SETLKW, &l);
101 #else
102 ret = flock(fd, exclusive ? LOCK_EX : LOCK_SH);
103 #endif
104 if(ret < 0)
105 ret = errno;
106 if(ret == EACCES) /* fcntl can return EACCES instead of EAGAIN */
107 ret = EAGAIN;
109 switch (ret) {
110 case 0:
111 break;
112 case EINVAL: /* filesystem doesn't support locking, let the user have it */
113 ret = 0;
114 break;
115 case EAGAIN:
116 krb5_set_error_message(context, ret,
117 N_("timed out locking cache file %s", "file"),
118 filename);
119 break;
120 default: {
121 char buf[128];
122 rk_strerror_r(ret, buf, sizeof(buf));
123 krb5_set_error_message(context, ret,
124 N_("error locking cache file %s: %s",
125 "file, error"), filename, buf);
126 break;
129 return ret;
132 KRB5_LIB_FUNCTION int KRB5_LIB_CALL
133 _krb5_xunlock(krb5_context context, int fd)
135 int ret;
136 #ifdef HAVE_FCNTL
137 struct flock l;
138 l.l_start = 0;
139 l.l_len = 0;
140 l.l_type = F_UNLCK;
141 l.l_whence = SEEK_SET;
142 ret = fcntl(fd, F_SETLKW, &l);
143 #else
144 ret = flock(fd, LOCK_UN);
145 #endif
146 if (ret < 0)
147 ret = errno;
148 switch (ret) {
149 case 0:
150 break;
151 case EINVAL: /* filesystem doesn't support locking, let the user have it */
152 ret = 0;
153 break;
154 default: {
155 char buf[128];
156 rk_strerror_r(ret, buf, sizeof(buf));
157 krb5_set_error_message(context, ret,
158 N_("Failed to unlock file: %s", ""), buf);
159 break;
162 return ret;
165 static krb5_error_code
166 write_storage(krb5_context context, krb5_storage *sp, int fd)
168 krb5_error_code ret;
169 krb5_data data;
170 ssize_t sret;
172 ret = krb5_storage_to_data(sp, &data);
173 if (ret) {
174 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
175 return ret;
177 sret = write(fd, data.data, data.length);
178 ret = (sret != (ssize_t)data.length);
179 krb5_data_free(&data);
180 if (ret) {
181 ret = errno;
182 krb5_set_error_message(context, ret,
183 N_("Failed to write FILE credential data", ""));
184 return ret;
186 return 0;
190 static krb5_error_code KRB5_CALLCONV
191 fcc_lock(krb5_context context, krb5_ccache id,
192 int fd, krb5_boolean exclusive)
194 krb5_error_code ret;
195 const char *name;
197 if (exclusive == FALSE)
198 return 0;
199 ret = fcc_get_name_2(context, id, &name, NULL, NULL);
200 if (ret == 0)
201 ret = _krb5_xlock(context, fd, exclusive, name);
202 return ret;
205 static krb5_error_code KRB5_CALLCONV
206 fcc_get_default_name(krb5_context, char **);
209 * This is the character used to separate the residual from the subsidiary name
210 * when both are given. It's tempting to use ':' just as we do in the ccache
211 * names, but we can't on Windows.
213 #define FILESUBSEP "+"
214 #define FILESUBSEPCHR ((FILESUBSEP)[0])
216 static krb5_error_code KRB5_CALLCONV
217 fcc_resolve_2(krb5_context context,
218 krb5_ccache *id,
219 const char *res,
220 const char *sub)
222 krb5_fcache *f;
223 char *freeme = NULL;
225 if (res == NULL && sub == NULL)
226 return krb5_einval(context, 3);
227 if (res == NULL) {
228 krb5_error_code ret;
230 if ((ret = fcc_get_default_name(context, &freeme)))
231 return ret;
232 res = freeme + sizeof("FILE:") - 1;
233 } else if (!sub && (sub = strchr(res, FILESUBSEPCHR))) {
234 if (sub[1] == '\0') {
235 sub = NULL;
236 } else {
237 /* `res' has a subsidiary component, so split on it */
238 if ((freeme = strndup(res, sub - res)) == NULL)
239 return krb5_enomem(context);
240 res = freeme;
241 sub++;
245 if ((f = calloc(1, sizeof(*f))) == NULL ||
246 (f->res = strdup(res)) == NULL ||
247 (f->sub = sub ? strdup(sub) : NULL) == (sub ? NULL : "") ||
248 asprintf(&f->filename, "%s%s%s",
249 res, sub ? FILESUBSEP : "", sub ? sub : "") == -1 ||
250 f->filename == NULL) {
251 if (f) {
252 free(f->filename);
253 free(f->res);
254 free(f->sub);
256 free(f);
257 free(freeme);
258 return krb5_enomem(context);
260 f->tmpfn = NULL;
261 f->version = 0;
262 (*id)->data.data = f;
263 (*id)->data.length = sizeof(*f);
265 free(freeme);
266 return 0;
270 * Try to scrub the contents of `filename' safely.
273 static int
274 scrub_file (int fd)
276 off_t pos;
277 char buf[128];
279 pos = lseek(fd, 0, SEEK_END);
280 if (pos < 0)
281 return errno;
282 if (lseek(fd, 0, SEEK_SET) < 0)
283 return errno;
284 memset(buf, 0, sizeof(buf));
285 while(pos > 0) {
286 ssize_t tmp;
287 size_t wr = sizeof(buf);
288 if (wr > pos)
289 wr = (size_t)pos;
290 tmp = write(fd, buf, wr);
292 if (tmp < 0)
293 return errno;
294 pos -= tmp;
296 #ifdef _MSC_VER
297 _commit (fd);
298 #else
299 fsync (fd);
300 #endif
301 return 0;
305 * Erase `filename' if it exists, trying to remove the contents if
306 * it's `safe'. We always try to remove the file, it it exists. It's
307 * only overwritten if it's a regular file (not a symlink and not a
308 * hardlink)
311 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
312 _krb5_erase_file(krb5_context context, const char *filename)
314 int fd;
315 struct stat sb1, sb2;
316 int ret;
318 ret = lstat (filename, &sb1);
319 if (ret < 0) {
320 if(errno == ENOENT)
321 return 0;
322 else
323 return errno;
326 fd = open(filename, O_RDWR | O_BINARY | O_CLOEXEC | O_NOFOLLOW);
327 if(fd < 0) {
328 if(errno == ENOENT)
329 return 0;
330 else
331 return errno;
333 rk_cloexec(fd);
334 ret = _krb5_xlock(context, fd, 1, filename);
335 if (ret) {
336 close(fd);
337 return ret;
339 if (unlink(filename) < 0) {
340 ret = errno;
341 close (fd);
342 krb5_set_error_message(context, errno,
343 N_("krb5_cc_destroy: unlinking \"%s\": %s", ""),
344 filename, strerror(ret));
345 return ret;
347 ret = fstat(fd, &sb2);
348 if (ret < 0) {
349 ret = errno;
350 close (fd);
351 return ret;
354 /* check if someone was playing with symlinks */
356 if (sb1.st_dev != sb2.st_dev || sb1.st_ino != sb2.st_ino) {
357 close(fd);
358 return EPERM;
361 /* there are still hard links to this file */
363 if (sb2.st_nlink != 0) {
364 close(fd);
365 return 0;
368 ret = scrub_file(fd);
369 close(fd);
370 return ret;
373 static krb5_error_code KRB5_CALLCONV
374 fcc_gen_new(krb5_context context, krb5_ccache *id)
376 char *file = NULL, *exp_file = NULL;
377 krb5_error_code ret;
378 krb5_fcache *f;
379 int fd;
381 f = calloc(1, sizeof(*f));
382 if(f == NULL) {
383 krb5_set_error_message(context, KRB5_CC_NOMEM,
384 N_("malloc: out of memory", ""));
385 return KRB5_CC_NOMEM;
387 f->tmpfn = NULL;
389 * XXX We should asprintf(&file, "%s:XXXXXX", KRB5_DEFAULT_CCNAME_FILE)
390 * instead so that new unique FILE ccaches can be found in the user's
391 * default collection.
392 * */
393 ret = asprintf(&file, "%sXXXXXX", KRB5_DEFAULT_CCFILE_ROOT);
394 if(ret < 0 || file == NULL) {
395 free(f);
396 krb5_set_error_message(context, KRB5_CC_NOMEM,
397 N_("malloc: out of memory", ""));
398 return KRB5_CC_NOMEM;
400 ret = _krb5_expand_path_tokens(context, file, 1, &exp_file);
401 free(file);
402 if (ret) {
403 free(f);
404 return ret;
407 file = exp_file;
409 fd = mkostemp(exp_file, O_CLOEXEC);
410 if(fd < 0) {
411 ret = (krb5_error_code)errno;
412 krb5_set_error_message(context, ret, N_("mkstemp %s failed", ""), exp_file);
413 free(f);
414 free(exp_file);
415 return ret;
417 close(fd);
418 f->filename = exp_file;
419 f->res = strdup(exp_file); /* XXX See above commentary about collection */
420 f->sub = NULL;
421 f->version = 0;
422 (*id)->data.data = f;
423 (*id)->data.length = sizeof(*f);
424 return 0;
427 static void
428 storage_set_flags(krb5_context context, krb5_storage *sp, int vno)
430 int flags = 0;
431 switch(vno) {
432 case KRB5_FCC_FVNO_1:
433 flags |= KRB5_STORAGE_PRINCIPAL_WRONG_NUM_COMPONENTS;
434 flags |= KRB5_STORAGE_PRINCIPAL_NO_NAME_TYPE;
435 flags |= KRB5_STORAGE_HOST_BYTEORDER;
436 break;
437 case KRB5_FCC_FVNO_2:
438 flags |= KRB5_STORAGE_HOST_BYTEORDER;
439 break;
440 case KRB5_FCC_FVNO_3:
441 flags |= KRB5_STORAGE_KEYBLOCK_KEYTYPE_TWICE;
442 break;
443 case KRB5_FCC_FVNO_4:
444 break;
445 default:
446 krb5_abortx(context,
447 "storage_set_flags called with bad vno (%x)", vno);
449 krb5_storage_set_flags(sp, flags);
452 static krb5_error_code KRB5_CALLCONV
453 fcc_open(krb5_context context,
454 krb5_ccache id,
455 const char *operation,
456 int *fd_ret,
457 int flags,
458 mode_t mode)
460 krb5_boolean exclusive = ((flags | O_WRONLY) == flags ||
461 (flags | O_RDWR) == flags);
462 krb5_error_code ret;
463 const char *filename;
464 struct stat sb1, sb2;
465 #ifndef _WIN32
466 struct stat sb3;
467 size_t tries = 3;
468 #endif
469 int strict_checking;
470 int fd;
472 flags |= O_BINARY | O_CLOEXEC | O_NOFOLLOW;
474 *fd_ret = -1;
476 if (FCACHE(id) == NULL)
477 return krb5_einval(context, 2);
479 if ((flags & O_EXCL)) {
481 * FIXME Instead of mkostemp()... we could instead try to use a .new
482 * file... with care. Or the O_TMPFILE / linkat() extensions. We need
483 * a roken / heimbase abstraction for that.
485 if (TMPFILENAME(id))
486 (void) unlink(TMPFILENAME(id));
487 free(TMPFILENAME(id));
488 TMPFILENAME(id) = NULL;
489 if (asprintf(&TMPFILENAME(id), "%s-XXXXXX", FILENAME(id)) < 0 ||
490 TMPFILENAME(id) == NULL)
491 return krb5_enomem(context);
492 if ((fd = mkostemp(TMPFILENAME(id), O_CLOEXEC)) == -1) {
493 krb5_set_error_message(context, ret = errno,
494 N_("Could not make temp ccache FILE:%s", ""),
495 TMPFILENAME(id));
496 free(TMPFILENAME(id));
497 TMPFILENAME(id) = NULL;
498 return ret;
500 goto out;
503 filename = TMPFILENAME(id) ? TMPFILENAME(id) : FILENAME(id);
504 strict_checking = (flags & O_CREAT) == 0 &&
505 (context->flags & KRB5_CTX_F_FCACHE_STRICT_CHECKING) != 0;
507 #ifndef WIN32
508 again:
509 #endif
510 memset(&sb1, 0, sizeof(sb1));
511 ret = lstat(filename, &sb1);
512 if (ret == 0) {
513 if (!S_ISREG(sb1.st_mode)) {
514 krb5_set_error_message(context, EPERM,
515 N_("Refuses to open symlinks for caches FILE:%s", ""), filename);
516 return EPERM;
518 } else if (errno != ENOENT || !(flags & O_CREAT)) {
519 krb5_set_error_message(context, errno, N_("%s lstat(%s)", "file, error"),
520 operation, filename);
521 return errno;
524 fd = open(filename, flags, mode);
525 if(fd < 0) {
526 char buf[128];
527 ret = errno;
528 rk_strerror_r(ret, buf, sizeof(buf));
529 krb5_set_error_message(context, ret, N_("%s open(%s): %s", "file, error"),
530 operation, filename, buf);
531 return ret;
533 rk_cloexec(fd);
535 ret = fstat(fd, &sb2);
536 if (ret < 0) {
537 krb5_clear_error_message(context);
538 close(fd);
539 return errno;
542 if (!S_ISREG(sb2.st_mode)) {
543 krb5_set_error_message(context, EPERM, N_("Refuses to open non files caches: FILE:%s", ""), filename);
544 close(fd);
545 return EPERM;
548 #ifndef _WIN32
549 if (sb1.st_dev && sb1.st_ino &&
550 (sb1.st_dev != sb2.st_dev || sb1.st_ino != sb2.st_ino)) {
552 * Perhaps we raced with a rename(). To complain about
553 * symlinks in that case would cause unnecessary concern, so
554 * we check for that possibility and loop. This has no
555 * TOCTOU problems because we redo the open(). We could also
556 * not do any of this checking if O_NOFOLLOW != 0...
558 close(fd);
559 ret = lstat(filename, &sb3);
560 if (ret || sb1.st_dev != sb2.st_dev ||
561 sb3.st_dev != sb2.st_dev || sb3.st_ino != sb2.st_ino) {
562 krb5_set_error_message(context, EPERM, N_("Refuses to open possible symlink for caches: FILE:%s", ""), filename);
563 return EPERM;
565 if (--tries == 0) {
566 krb5_set_error_message(context, EPERM, N_("Raced too many times with renames of FILE:%s", ""), filename);
567 return EPERM;
569 goto again;
571 #endif
574 * /tmp (or wherever default ccaches go) might not be on its own
575 * filesystem, or on a filesystem different /etc, say, and even if
576 * it were, suppose a user hard-links another's ccache to her
577 * default ccache, then runs a set-uid program that will user her
578 * default ccache (even if it ignores KRB5CCNAME)...
580 * Default ccache locations should really be on per-user non-tmp
581 * locations on tmpfs "run" directories. But we don't know here
582 * that this is the case. Thus: no hard-links, no symlinks.
584 if (sb2.st_nlink > 1) {
585 krb5_set_error_message(context, EPERM, N_("Refuses to open hardlinks for caches FILE:%s", ""), filename);
586 close(fd);
587 return EPERM;
590 if (strict_checking) {
591 #ifndef _WIN32
593 * XXX WIN32: Needs to have ACL checking code!
594 * st_mode comes out as 100666, and st_uid is no use.
597 * XXX Should probably add options to improve control over this
598 * check. We might want strict checking of everything except
599 * this.
601 if (sb2.st_uid != geteuid()) {
602 krb5_set_error_message(context, EPERM, N_("Refuses to open cache files not own by myself FILE:%s (owned by %d)", ""), filename, (int)sb2.st_uid);
603 close(fd);
604 return EPERM;
606 if ((sb2.st_mode & 077) != 0) {
607 krb5_set_error_message(context, EPERM,
608 N_("Refuses to open group/other readable files FILE:%s", ""), filename);
609 close(fd);
610 return EPERM;
612 #endif
615 out:
616 if((ret = fcc_lock(context, id, fd, exclusive)) != 0) {
617 close(fd);
618 return ret;
620 *fd_ret = fd;
621 return 0;
624 static krb5_error_code KRB5_CALLCONV
625 fcc_initialize(krb5_context context,
626 krb5_ccache id,
627 krb5_principal primary_principal)
629 krb5_fcache *f = FCACHE(id);
630 int ret = 0;
631 int fd;
633 if (f == NULL)
634 return krb5_einval(context, 2);
637 * fcc_open() will notice the O_EXCL and will make a temporary file that
638 * will later be renamed into place.
640 ret = fcc_open(context, id, "initialize", &fd, O_RDWR | O_CREAT | O_EXCL, 0600);
641 if(ret)
642 return ret;
644 krb5_storage *sp;
645 sp = krb5_storage_emem();
646 if (sp == NULL)
647 return krb5_enomem(context);
648 krb5_storage_set_eof_code(sp, KRB5_CC_END);
649 if(context->fcache_vno != 0)
650 f->version = context->fcache_vno;
651 else
652 f->version = KRB5_FCC_FVNO_4;
653 if (ret == 0)
654 ret = krb5_store_int8(sp, 5);
655 if (ret == 0)
656 ret = krb5_store_int8(sp, f->version);
657 storage_set_flags(context, sp, f->version);
658 if(f->version == KRB5_FCC_FVNO_4 && ret == 0) {
659 /* V4 stuff */
660 if (context->kdc_sec_offset) {
661 if (ret == 0)
662 ret = krb5_store_int16 (sp, 12); /* length */
663 if (ret == 0)
664 ret = krb5_store_int16 (sp, FCC_TAG_DELTATIME); /* Tag */
665 if (ret == 0)
666 ret = krb5_store_int16 (sp, 8); /* length of data */
667 if (ret == 0)
668 ret = krb5_store_int32 (sp, context->kdc_sec_offset);
669 if (ret == 0)
670 ret = krb5_store_int32 (sp, context->kdc_usec_offset);
671 } else {
672 if (ret == 0)
673 ret = krb5_store_int16 (sp, 0);
676 if (ret == 0)
677 ret = krb5_store_principal(sp, primary_principal);
679 if (ret == 0)
680 ret = write_storage(context, sp, fd);
682 krb5_storage_free(sp);
684 if (close(fd) < 0)
685 if (ret == 0) {
686 char buf[128];
687 ret = errno;
688 rk_strerror_r(ret, buf, sizeof(buf));
689 krb5_set_error_message(context, ret, N_("close %s: %s", ""),
690 FILENAME(id), buf);
692 return ret;
695 static krb5_error_code KRB5_CALLCONV
696 fcc_close(krb5_context context,
697 krb5_ccache id)
699 if (FCACHE(id) == NULL)
700 return krb5_einval(context, 2);
702 if (TMPFILENAME(id))
703 (void) unlink(TMPFILENAME(id));
704 free(TMPFILENAME(id));
705 free(RESFILENAME(id));
706 free(SUBFILENAME(id));
707 free(FILENAME(id));
708 krb5_data_free(&id->data);
709 return 0;
712 static krb5_error_code KRB5_CALLCONV
713 fcc_destroy(krb5_context context,
714 krb5_ccache id)
716 if (FCACHE(id) == NULL)
717 return krb5_einval(context, 2);
719 if (TMPFILENAME(id))
720 (void) _krb5_erase_file(context, TMPFILENAME(id));
721 return _krb5_erase_file(context, FILENAME(id));
724 static krb5_error_code KRB5_CALLCONV
725 fcc_store_cred(krb5_context context,
726 krb5_ccache id,
727 krb5_creds *creds)
729 int ret;
730 int fd;
732 ret = fcc_open(context, id, "store", &fd, O_WRONLY | O_APPEND, 0);
733 if(ret)
734 return ret;
736 krb5_storage *sp;
738 sp = krb5_storage_emem();
739 if (sp == NULL)
740 return krb5_enomem(context);
741 krb5_storage_set_eof_code(sp, KRB5_CC_END);
742 storage_set_flags(context, sp, FCACHE(id)->version);
743 ret = krb5_store_creds(sp, creds);
744 if (ret == 0)
745 ret = write_storage(context, sp, fd);
746 krb5_storage_free(sp);
748 if (close(fd) < 0) {
749 if (ret == 0) {
750 char buf[128];
751 ret = errno;
752 rk_strerror_r(ret, buf, sizeof(buf));
753 krb5_set_error_message(context, ret, N_("close %s: %s", ""),
754 FILENAME(id), buf);
757 if (ret == 0 && TMPFILENAME(id) &&
758 !krb5_is_config_principal(context, creds->server)) {
761 * Portability note: there's no need to have WIN32 or other code here
762 * for odd rename cases because rk_rename() is meant to handle that.
764 ret = rk_rename(TMPFILENAME(id), FILENAME(id));
765 if (ret == 0) {
766 free(TMPFILENAME(id));
767 TMPFILENAME(id) = NULL;
768 } else {
769 ret = errno;
772 return ret;
775 static krb5_error_code
776 init_fcc(krb5_context context,
777 krb5_ccache id,
778 const char *operation,
779 krb5_storage **ret_sp,
780 int *ret_fd,
781 krb5_deltat *kdc_offset)
783 int fd;
784 int8_t pvno, tag;
785 krb5_storage *sp;
786 krb5_error_code ret;
788 *ret_fd = -1;
789 *ret_sp = NULL;
790 if (kdc_offset)
791 *kdc_offset = 0;
793 ret = fcc_open(context, id, operation, &fd, O_RDONLY, 0);
794 if(ret)
795 return ret;
797 sp = krb5_storage_stdio_from_fd(fd, "r");
798 if(sp == NULL) {
799 krb5_clear_error_message(context);
800 ret = ENOMEM;
801 goto out;
803 krb5_storage_set_eof_code(sp, KRB5_CC_END);
804 ret = krb5_ret_int8(sp, &pvno);
805 if (ret != 0) {
806 if(ret == KRB5_CC_END) {
807 ret = ENOENT;
808 krb5_set_error_message(context, ret,
809 N_("Empty credential cache file: %s", ""),
810 FILENAME(id));
811 } else
812 krb5_set_error_message(context, ret, N_("Error reading pvno "
813 "in cache file: %s", ""),
814 FILENAME(id));
815 goto out;
817 if (pvno != 5) {
818 ret = KRB5_CCACHE_BADVNO;
819 krb5_set_error_message(context, ret, N_("Bad version number in credential "
820 "cache file: %s", ""),
821 FILENAME(id));
822 goto out;
824 ret = krb5_ret_int8(sp, &tag); /* should not be host byte order */
825 if (ret != 0) {
826 ret = KRB5_CC_FORMAT;
827 krb5_set_error_message(context, ret, "Error reading tag in "
828 "cache file: %s", FILENAME(id));
829 goto out;
831 FCACHE(id)->version = tag;
832 storage_set_flags(context, sp, FCACHE(id)->version);
833 switch (tag) {
834 case KRB5_FCC_FVNO_4: {
835 int16_t length;
837 ret = krb5_ret_int16 (sp, &length);
838 if(ret) {
839 ret = KRB5_CC_FORMAT;
840 krb5_set_error_message(context, ret,
841 N_("Error reading tag length in "
842 "cache file: %s", ""), FILENAME(id));
843 goto out;
845 while(length > 0) {
846 int16_t dtag, data_len;
847 int i;
848 int8_t dummy;
850 ret = krb5_ret_int16 (sp, &dtag);
851 if(ret) {
852 ret = KRB5_CC_FORMAT;
853 krb5_set_error_message(context, ret, N_("Error reading dtag in "
854 "cache file: %s", ""),
855 FILENAME(id));
856 goto out;
858 ret = krb5_ret_int16 (sp, &data_len);
859 if(ret) {
860 ret = KRB5_CC_FORMAT;
861 krb5_set_error_message(context, ret,
862 N_("Error reading dlength "
863 "in cache file: %s",""),
864 FILENAME(id));
865 goto out;
867 switch (dtag) {
868 case FCC_TAG_DELTATIME : {
869 int32_t offset;
871 ret = krb5_ret_int32 (sp, &offset);
872 ret |= krb5_ret_int32 (sp, &context->kdc_usec_offset);
873 if(ret) {
874 ret = KRB5_CC_FORMAT;
875 krb5_set_error_message(context, ret,
876 N_("Error reading kdc_sec in "
877 "cache file: %s", ""),
878 FILENAME(id));
879 goto out;
881 context->kdc_sec_offset = offset;
882 if (kdc_offset)
883 *kdc_offset = offset;
884 break;
886 default :
887 for (i = 0; i < data_len; ++i) {
888 ret = krb5_ret_int8 (sp, &dummy);
889 if(ret) {
890 ret = KRB5_CC_FORMAT;
891 krb5_set_error_message(context, ret,
892 N_("Error reading unknown "
893 "tag in cache file: %s", ""),
894 FILENAME(id));
895 goto out;
898 break;
900 length -= 4 + data_len;
902 break;
904 case KRB5_FCC_FVNO_3:
905 case KRB5_FCC_FVNO_2:
906 case KRB5_FCC_FVNO_1:
907 break;
908 default :
909 ret = KRB5_CCACHE_BADVNO;
910 krb5_set_error_message(context, ret,
911 N_("Unknown version number (%d) in "
912 "credential cache file: %s", ""),
913 (int)tag, FILENAME(id));
914 goto out;
916 *ret_sp = sp;
917 *ret_fd = fd;
919 return 0;
920 out:
921 if(sp != NULL)
922 krb5_storage_free(sp);
923 close(fd);
924 return ret;
927 static krb5_error_code KRB5_CALLCONV
928 fcc_get_principal(krb5_context context,
929 krb5_ccache id,
930 krb5_principal *principal)
932 krb5_error_code ret;
933 int fd;
934 krb5_storage *sp;
936 ret = init_fcc (context, id, "get-principal", &sp, &fd, NULL);
937 if (ret)
938 return ret;
939 ret = krb5_ret_principal(sp, principal);
940 if (ret)
941 krb5_clear_error_message(context);
942 krb5_storage_free(sp);
943 close(fd);
944 return ret;
947 static krb5_error_code KRB5_CALLCONV
948 fcc_end_get(krb5_context context,
949 krb5_ccache id,
950 krb5_cc_cursor *cursor);
952 static krb5_error_code KRB5_CALLCONV
953 fcc_get_first(krb5_context context,
954 krb5_ccache id,
955 krb5_cc_cursor *cursor)
957 krb5_error_code ret;
958 krb5_principal principal;
960 if (FCACHE(id) == NULL)
961 return krb5_einval(context, 2);
963 *cursor = calloc(1, sizeof(struct fcc_cursor));
964 if (*cursor == NULL) {
965 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
966 return ENOMEM;
969 ret = init_fcc(context, id, "get-first", &FCC_CURSOR(*cursor)->sp,
970 &FCC_CURSOR(*cursor)->fd, NULL);
971 if (ret) {
972 free(*cursor);
973 *cursor = NULL;
974 return ret;
976 ret = krb5_ret_principal (FCC_CURSOR(*cursor)->sp, &principal);
977 if(ret) {
978 krb5_clear_error_message(context);
979 fcc_end_get(context, id, cursor);
980 return ret;
982 krb5_free_principal (context, principal);
983 return 0;
987 * Return true if cred is a removed entry. We assume that any active entry
988 * with endtime=0 (such as a config entry or gssproxy encrypted credential)
989 * will also have authtime=0.
991 static inline krb5_boolean
992 cred_removed(krb5_creds *c)
994 return c->times.endtime == 0 && c->times.authtime != 0;
997 static krb5_error_code KRB5_CALLCONV
998 fcc_get_next (krb5_context context,
999 krb5_ccache id,
1000 krb5_cc_cursor *cursor,
1001 krb5_creds *creds)
1003 krb5_error_code ret;
1005 if (FCACHE(id) == NULL)
1006 return krb5_einval(context, 2);
1008 if (FCC_CURSOR(*cursor) == NULL)
1009 return krb5_einval(context, 3);
1011 while (1) {
1012 FCC_CURSOR(*cursor)->cred_start =
1013 krb5_storage_seek(FCC_CURSOR(*cursor)->sp, 0, SEEK_CUR);
1015 ret = krb5_ret_creds(FCC_CURSOR(*cursor)->sp, creds);
1017 FCC_CURSOR(*cursor)->cred_end =
1018 krb5_storage_seek(FCC_CURSOR(*cursor)->sp, 0, SEEK_CUR);
1020 if (ret) {
1021 krb5_clear_error_message(context);
1022 break;
1025 if (!cred_removed(creds))
1026 break;
1028 krb5_free_cred_contents(context, creds);
1031 return ret;
1034 static krb5_error_code KRB5_CALLCONV
1035 fcc_end_get (krb5_context context,
1036 krb5_ccache id,
1037 krb5_cc_cursor *cursor)
1040 if (FCACHE(id) == NULL)
1041 return krb5_einval(context, 2);
1043 if (FCC_CURSOR(*cursor) == NULL)
1044 return krb5_einval(context, 3);
1046 krb5_storage_free(FCC_CURSOR(*cursor)->sp);
1047 close (FCC_CURSOR(*cursor)->fd);
1048 free(*cursor);
1049 *cursor = NULL;
1050 return 0;
1053 static void KRB5_CALLCONV
1054 cred_delete(krb5_context context,
1055 krb5_ccache id,
1056 krb5_cc_cursor *cursor,
1057 krb5_creds *cred)
1059 krb5_error_code ret;
1060 krb5_storage *sp;
1061 krb5_data orig_cred_data;
1062 unsigned char *cred_data_in_file = NULL;
1063 off_t new_cred_sz;
1064 struct stat sb1, sb2;
1065 int fd = -1;
1066 ssize_t bytes;
1067 krb5_const_realm srealm = krb5_principal_get_realm(context, cred->server);
1069 /* This is best-effort code; if we lose track of errors here it's OK */
1071 heim_assert(FCC_CURSOR(*cursor)->cred_start < FCC_CURSOR(*cursor)->cred_end,
1072 "fcache internal error");
1074 krb5_data_zero(&orig_cred_data);
1076 sp = krb5_storage_emem();
1077 if (sp == NULL)
1078 return;
1079 krb5_storage_set_eof_code(sp, KRB5_CC_END);
1080 storage_set_flags(context, sp, FCACHE(id)->version);
1082 /* Get a copy of what the cred should look like in the file; see below */
1083 ret = krb5_store_creds(sp, cred);
1084 if (ret)
1085 goto out;
1087 ret = krb5_storage_to_data(sp, &orig_cred_data);
1088 if (ret)
1089 goto out;
1090 krb5_storage_free(sp);
1092 cred_data_in_file = malloc(orig_cred_data.length);
1093 if (cred_data_in_file == NULL)
1094 goto out;
1097 * Mark the cred expired; krb5_cc_retrieve_cred() callers should use
1098 * KRB5_TC_MATCH_TIMES, so this should be good enough...
1100 cred->times.endtime = 0;
1102 /* For compatibility with MIT d3b39a8bac6206b5ea78b0bf6a2958c1df0b0dd5 */
1103 cred->times.authtime = -1;
1105 /* ...except for config creds because we don't check their endtimes */
1106 if (srealm && strcmp(srealm, "X-CACHECONF:") == 0) {
1107 ret = krb5_principal_set_realm(context, cred->server, "X-RMED-CONF:");
1108 if (ret)
1109 goto out;
1112 sp = krb5_storage_emem();
1113 if (sp == NULL)
1114 goto out;
1115 krb5_storage_set_eof_code(sp, KRB5_CC_END);
1116 storage_set_flags(context, sp, FCACHE(id)->version);
1118 ret = krb5_store_creds(sp, cred);
1120 /* The new cred must be the same size as the old cred */
1121 new_cred_sz = krb5_storage_seek(sp, 0, SEEK_END);
1122 if (new_cred_sz != orig_cred_data.length || new_cred_sz !=
1123 (FCC_CURSOR(*cursor)->cred_end - FCC_CURSOR(*cursor)->cred_start)) {
1124 /* XXX This really can't happen. Assert like above? */
1125 krb5_set_error_message(context, EINVAL,
1126 N_("Credential deletion failed on ccache "
1127 "FILE:%s: new credential size did not "
1128 "match old credential size", ""),
1129 FILENAME(id));
1130 goto out;
1133 ret = fcc_open(context, id, "remove_cred", &fd, O_RDWR, 0);
1134 if (ret)
1135 goto out;
1138 * Check that we're updating the same file where we got the
1139 * cred's offset, else we'd be corrupting a new ccache.
1141 if (fstat(FCC_CURSOR(*cursor)->fd, &sb1) == -1 ||
1142 fstat(fd, &sb2) == -1)
1143 goto out;
1144 if (sb1.st_dev != sb2.st_dev || sb1.st_ino != sb2.st_ino)
1145 goto out;
1148 * Make sure what we overwrite is what we expected.
1150 * FIXME: We *really* need the ccache v4 tag for ccache ID. This
1151 * check that we're only overwriting something that looks exactly
1152 * like what we want to is probably good enough in practice, but
1153 * it's not guaranteed to work.
1155 if (lseek(fd, FCC_CURSOR(*cursor)->cred_start, SEEK_SET) == (off_t)-1)
1156 goto out;
1157 bytes = read(fd, cred_data_in_file, orig_cred_data.length);
1158 if (bytes != orig_cred_data.length)
1159 goto out;
1160 if (memcmp(orig_cred_data.data, cred_data_in_file, bytes) != 0)
1161 goto out;
1162 if (lseek(fd, FCC_CURSOR(*cursor)->cred_start, SEEK_SET) == (off_t)-1)
1163 goto out;
1164 ret = write_storage(context, sp, fd);
1165 out:
1166 if (fd > -1) {
1167 if (close(fd) < 0 && ret == 0) {
1168 krb5_set_error_message(context, errno, N_("close %s", ""),
1169 FILENAME(id));
1172 krb5_data_free(&orig_cred_data);
1173 free(cred_data_in_file);
1174 krb5_storage_free(sp);
1175 return;
1178 static krb5_error_code KRB5_CALLCONV
1179 fcc_remove_cred(krb5_context context,
1180 krb5_ccache id,
1181 krb5_flags which,
1182 krb5_creds *mcred)
1184 krb5_error_code ret, ret2;
1185 krb5_cc_cursor cursor;
1186 krb5_creds found_cred;
1188 if (FCACHE(id) == NULL)
1189 return krb5_einval(context, 2);
1191 ret = krb5_cc_start_seq_get(context, id, &cursor);
1192 if (ret)
1193 return ret;
1194 while ((ret = krb5_cc_next_cred(context, id, &cursor, &found_cred)) == 0) {
1195 if (!krb5_compare_creds(context, which, mcred, &found_cred)) {
1196 krb5_free_cred_contents(context, &found_cred);
1197 continue;
1199 cred_delete(context, id, &cursor, &found_cred);
1200 krb5_free_cred_contents(context, &found_cred);
1202 ret2 = krb5_cc_end_seq_get(context, id, &cursor);
1203 if (ret2) /* not expected to fail */
1204 return ret2;
1205 if (ret == KRB5_CC_END)
1206 return 0;
1207 return ret;
1210 static krb5_error_code KRB5_CALLCONV
1211 fcc_set_flags(krb5_context context,
1212 krb5_ccache id,
1213 krb5_flags flags)
1215 if (FCACHE(id) == NULL)
1216 return krb5_einval(context, 2);
1218 return 0; /* XXX */
1221 static int KRB5_CALLCONV
1222 fcc_get_version(krb5_context context,
1223 krb5_ccache id)
1225 if (FCACHE(id) == NULL)
1226 return -1;
1228 return FCACHE(id)->version;
1231 static const char *
1232 my_basename(const char *fn)
1234 const char *base, *p;
1236 if (strncmp(fn, "FILE:", sizeof("FILE:") - 1) == 0)
1237 fn += sizeof("FILE:") - 1;
1238 for (p = base = fn; *p; p++) {
1239 #ifdef WIN32
1240 if (*p == '/' || *p == '\\')
1241 base = p + 1;
1242 #else
1243 if (*p == '/')
1244 base = p + 1;
1245 #endif
1247 return base;
1250 /* We could use an rk_dirname()... */
1251 static char *
1252 my_dirname(const char *fn)
1254 size_t len, i;
1255 char *dname;
1257 if (strncmp(fn, "FILE:", sizeof("FILE:") - 1) == 0)
1258 fn += sizeof("FILE:") - 1;
1260 if ((dname = strdup(fn)) == NULL)
1261 return NULL;
1262 len = strlen(dname);
1263 for (i = 0; i < len; i++) {
1264 #ifdef WIN32
1265 if (dname[len - i] == '\\' ||
1266 dname[len - i] == '/') {
1267 dname[len - i] = '\0';
1268 break;
1270 #else
1271 if (dname[len - i] == '/') {
1272 dname[len - i] = '\0';
1273 break;
1275 #endif
1277 if (i < len)
1278 return dname;
1279 free(dname);
1280 return strdup(".");
1284 * This checks that a directory entry matches a required basename and has a
1285 * non-empty subsidiary component.
1287 static int
1288 matchbase(const char *fn, const char *base, size_t baselen)
1290 return strncmp(fn, base, baselen) == 0 &&
1291 (fn[baselen] == FILESUBSEPCHR && fn[baselen + 1] != '\0');
1295 * Check if `def_locs' contains `name' (which must be the default ccache name),
1296 * in which case the caller may look for subsidiaries of all of `def_locs'.
1298 * This is needed because the collection iterators don't take a base location
1299 * as an argument, so we can only search default locations, but only if the
1300 * current default ccache name is indeed a default (as opposed to from
1301 * KRB5CCNAME being set in the environment pointing to a non-default name).
1303 static krb5_error_code
1304 is_default_collection(krb5_context context, const char *name,
1305 const char * const *def_locs, int *res)
1307 krb5_error_code ret;
1308 const char *def_loc[2] = { KRB5_DEFAULT_CCNAME_FILE, NULL };
1309 const char *sep;
1310 size_t namelen;
1311 size_t i;
1313 *res = 0;
1314 if (name == NULL) {
1315 *res = 1;
1316 return 0;
1318 if ((sep = strchr(name, FILESUBSEPCHR)))
1319 namelen = (size_t)(sep - name);
1320 else
1321 namelen = strlen(name);
1322 if (def_locs == NULL)
1323 def_locs = def_loc;
1324 for (i = 0; !(*res) && def_locs[i]; i++) {
1325 char *e = NULL;
1327 if ((ret = _krb5_expand_default_cc_name(context, def_locs[i], &e)))
1328 return ret;
1329 *res = strncmp(e, name, namelen) == 0 &&
1330 (sep == NULL || e[namelen] == FILESUBSEPCHR || e[namelen] == '\0');
1331 free(e);
1333 return 0;
1337 * Collection iterator cursor.
1339 * There may be an array of locations, and for each location we'll try
1340 * resolving it, as well as doing a readdir() of the dirname of it and output
1341 * all ccache names in that directory that begin with the current location and
1342 * end in "+${subsidiary}".
1344 struct fcache_iter {
1345 const char *curr_location;
1346 char *def_ccname; /* The default ccname */
1347 char **locations; /* All the other places we'll look for a ccache */
1348 char *dname; /* dirname() of curr_location */
1349 DIR *d;
1350 struct dirent *dentry;
1351 int location; /* Index of `locations' */
1352 unsigned int first:1;
1353 unsigned int dead:1;
1356 /* Initiate FILE collection iteration */
1357 static krb5_error_code KRB5_CALLCONV
1358 fcc_get_cache_first(krb5_context context, krb5_cc_cursor *cursor)
1360 struct fcache_iter *iter = NULL;
1361 krb5_error_code ret;
1362 const char *def_ccname = NULL;
1363 char **def_locs = NULL;
1364 int is_def_coll = 0;
1366 if (krb5_config_get_bool_default(context, NULL, FALSE, "libdefaults",
1367 "enable_file_cache_iteration", NULL)) {
1368 def_ccname = krb5_cc_default_name(context);
1369 def_locs = krb5_config_get_strings(context, NULL, "libdefaults",
1370 "default_file_cache_collections",
1371 NULL);
1375 * Note: do not allow krb5_cc_default_name() to recurse via
1376 * krb5_cc_cache_match().
1377 * Note that context->default_cc_name will be NULL even though
1378 * KRB5CCNAME is set in the environment if neither krb5_cc_default_name()
1379 * nor krb5_cc_set_default_name() have been called.
1383 * Figure out if the current default ccache name is a really a default one
1384 * so we know whether to search any other default FILE collection
1385 * locations.
1387 if ((ret = is_default_collection(context, def_ccname,
1388 (const char **)def_locs,
1389 &is_def_coll)))
1390 goto out;
1392 /* Setup the cursor */
1393 if ((iter = calloc(1, sizeof(*iter))) == NULL ||
1394 (def_ccname && (iter->def_ccname = strdup(def_ccname)) == NULL)) {
1395 ret = krb5_enomem(context);
1396 goto out;
1399 if (is_def_coll) {
1400 /* Since def_ccname is in the `def_locs', we'll include those */
1401 iter->locations = def_locs;
1402 free(iter->def_ccname);
1403 iter->def_ccname = NULL;
1404 def_locs = NULL;
1405 } else {
1406 /* Since def_ccname is NOT in the `def_locs', we'll exclude those */
1407 iter->locations = NULL;
1409 iter->curr_location = NULL;
1410 iter->location = -1; /* Pre-incremented */
1411 iter->first = 1;
1412 iter->dname = NULL;
1413 iter->d = NULL;
1414 *cursor = iter;
1415 iter = NULL;
1416 ret = 0;
1418 out:
1419 krb5_config_free_strings(def_locs);
1420 free(iter);
1421 return ret;
1424 /* Pick the next location as the `iter->curr_location' */
1425 static krb5_error_code
1426 next_location(krb5_context context, struct fcache_iter *iter)
1428 if (iter->first && iter->def_ccname) {
1429 iter->curr_location = iter->def_ccname;
1430 iter->first = 0;
1431 return 0;
1433 iter->first = 0;
1435 if (iter->d)
1436 closedir(iter->d);
1437 iter->d = NULL;
1438 iter->curr_location = NULL;
1439 if (iter->locations &&
1440 (iter->curr_location = iter->locations[++(iter->location)]))
1441 return 0;
1443 iter->dead = 1; /* Do not run off the end of iter->locations */
1444 return KRB5_CC_END;
1447 /* Output the next match for `iter->curr_location' from readdir() */
1448 static krb5_error_code
1449 next_dir_match(krb5_context context, struct fcache_iter *iter, char **fn)
1451 struct stat st;
1452 const char *base = my_basename(iter->curr_location);
1453 size_t baselen = strlen(base);
1454 char *s;
1456 *fn = NULL;
1457 if (iter->d == NULL)
1458 return 0;
1459 for (iter->dentry = readdir(iter->d);
1460 iter->dentry;
1461 iter->dentry = readdir(iter->d)) {
1462 if (!matchbase(iter->dentry->d_name, base, baselen))
1463 continue;
1464 if (asprintf(&s, "FILE:%s/%s", iter->dname, iter->dentry->d_name) == -1 ||
1465 s == NULL)
1466 return krb5_enomem(context);
1467 if (stat(s + sizeof("FILE:") - 1, &st) == 0 && S_ISREG(st.st_mode)) {
1468 *fn = s;
1469 return 0;
1471 free(s);
1473 iter->curr_location = NULL;
1474 closedir(iter->d);
1475 iter->d = NULL;
1476 return 0;
1479 /* See if the given `ccname' is a FILE ccache we can resolve */
1480 static krb5_error_code
1481 try1(krb5_context context, const char *ccname, krb5_ccache *id)
1483 krb5_error_code ret;
1484 krb5_ccache cc;
1486 ret = krb5_cc_resolve(context, ccname, &cc);
1487 if (ret == ENOMEM)
1488 return ret;
1489 if (ret == 0) {
1490 if (strcmp(krb5_cc_get_type(context, cc), "FILE") == 0) {
1491 *id = cc;
1492 cc = NULL;
1494 krb5_cc_close(context, cc);
1496 return 0;
1499 /* Output the next FILE ccache in the FILE ccache collection */
1500 static krb5_error_code KRB5_CALLCONV
1501 fcc_get_cache_next(krb5_context context, krb5_cc_cursor cursor, krb5_ccache *id)
1503 struct fcache_iter *iter = cursor;
1504 krb5_error_code ret;
1505 char *name = NULL;
1507 *id = NULL;
1508 if (iter == NULL)
1509 return krb5_einval(context, 2);
1511 /* Do not run off the end of iter->locations */
1512 if (iter->dead)
1513 return KRB5_CC_END;
1515 if (!iter->curr_location) {
1516 /* Next base location */
1517 if ((ret = next_location(context, iter)))
1518 return ret;
1519 /* Output the current base location */
1520 if ((ret = try1(context, iter->curr_location, id)) || *id)
1521 return ret;
1524 /* Look for subsidiaries of iter->curr_location */
1525 if (!iter->d) {
1526 free(iter->dname);
1527 if ((iter->dname = my_dirname(iter->curr_location)) == NULL)
1528 return krb5_enomem(context);
1529 if ((iter->d = opendir(iter->dname)) == NULL) {
1530 /* Dirname ENOENT -> next location */
1531 if ((ret = next_location(context, iter)))
1532 return ret;
1533 /* Tail-recurse */
1534 return fcc_get_cache_next(context, cursor, id);
1537 for (ret = next_dir_match(context, iter, &name);
1538 ret == 0 && name != NULL;
1539 ret = next_dir_match(context, iter, &name)) {
1540 if ((ret = try1(context, name, id)) || *id) {
1541 free(name);
1542 return ret;
1544 free(name);
1547 /* Directory listing exhausted -> go to next location, tail-recurse */
1548 if ((ret = next_location(context, iter)))
1549 return ret;
1550 return fcc_get_cache_next(context, cursor, id);
1553 static krb5_error_code KRB5_CALLCONV
1554 fcc_end_cache_get(krb5_context context, krb5_cc_cursor cursor)
1556 struct fcache_iter *iter = cursor;
1558 if (iter == NULL)
1559 return krb5_einval(context, 2);
1561 krb5_config_free_strings(iter->locations);
1562 if (iter->d)
1563 closedir(iter->d);
1564 free(iter->def_ccname);
1565 free(iter->dname);
1566 free(iter);
1567 return 0;
1570 static krb5_error_code KRB5_CALLCONV
1571 fcc_move(krb5_context context, krb5_ccache from, krb5_ccache to)
1573 krb5_error_code ret = 0;
1574 krb5_fcache *f = FCACHE(from);
1575 krb5_fcache *t = FCACHE(to);
1577 if (f->tmpfn) {
1579 * If `from' has a temp file and we haven't renamed it into place yet,
1580 * then we should rename TMPFILENAME(from) to FILENAME(to).
1582 * This can only happen if we're moving a ccache where only cc config
1583 * entries, or no entries, have been written. That's not likely.
1585 if (rk_rename(f->tmpfn, t->filename)) {
1586 ret = errno;
1587 } else {
1588 free(f->tmpfn);
1589 f->tmpfn = NULL;
1591 } else if (rk_rename(f->filename, t->filename)) {
1592 ret = errno;
1595 * We need only close from -- we can't destroy it since the rename
1596 * succeeded, which "destroyed" it at its old name.
1598 if (ret == 0)
1599 krb5_cc_close(context, from);
1600 return ret;
1603 static krb5_error_code KRB5_CALLCONV
1604 fcc_get_default_name(krb5_context context, char **str)
1606 return _krb5_expand_default_cc_name(context,
1607 KRB5_DEFAULT_CCNAME_FILE,
1608 str);
1611 static krb5_error_code KRB5_CALLCONV
1612 fcc_set_default_cache(krb5_context context, krb5_ccache id)
1614 krb5_error_code ret;
1615 krb5_ccache dest;
1616 char *s = NULL;
1618 if (SUBFILENAME(id) == NULL)
1619 return 0; /* Already a primary */
1620 if (asprintf(&s, "FILE:%s", RESFILENAME(id)) == -1 || s == NULL)
1621 return krb5_enomem(context);
1624 * We can't hard-link, since we refuse to open ccaches with st_nlink > 1,
1625 * and we can't rename() the ccache because the old name should remain
1626 * available. Ergo, we copy the ccache.
1628 ret = krb5_cc_resolve(context, s, &dest);
1629 if (ret == 0)
1630 ret = krb5_cc_copy_cache(context, id, dest);
1631 free(s);
1632 if (ret)
1633 krb5_set_error_message(context, ret,
1634 N_("Failed to copy subsidiary cache file %s to "
1635 "default %s", ""), FILENAME(id),
1636 RESFILENAME(id));
1637 return ret;
1640 static krb5_error_code KRB5_CALLCONV
1641 fcc_lastchange(krb5_context context, krb5_ccache id, krb5_timestamp *mtime)
1643 krb5_error_code ret;
1644 struct stat sb;
1645 int fd;
1647 ret = fcc_open(context, id, "lastchange", &fd, O_RDONLY, 0);
1648 if(ret)
1649 return ret;
1650 ret = fstat(fd, &sb);
1651 close(fd);
1652 if (ret) {
1653 ret = errno;
1654 krb5_set_error_message(context, ret, N_("Failed to stat cache file", ""));
1655 return ret;
1657 *mtime = sb.st_mtime;
1658 return 0;
1661 static krb5_error_code KRB5_CALLCONV
1662 fcc_set_kdc_offset(krb5_context context, krb5_ccache id, krb5_deltat kdc_offset)
1664 return 0;
1667 static krb5_error_code KRB5_CALLCONV
1668 fcc_get_kdc_offset(krb5_context context, krb5_ccache id, krb5_deltat *kdc_offset)
1670 krb5_error_code ret;
1671 krb5_storage *sp = NULL;
1672 int fd;
1673 ret = init_fcc(context, id, "get-kdc-offset", &sp, &fd, kdc_offset);
1674 if (sp)
1675 krb5_storage_free(sp);
1676 close(fd);
1678 return ret;
1683 * Variable containing the FILE based credential cache implementation.
1685 * @ingroup krb5_ccache
1688 KRB5_LIB_VARIABLE const krb5_cc_ops krb5_fcc_ops = {
1689 KRB5_CC_OPS_VERSION_5,
1690 "FILE",
1691 NULL,
1692 NULL,
1693 fcc_gen_new,
1694 fcc_initialize,
1695 fcc_destroy,
1696 fcc_close,
1697 fcc_store_cred,
1698 NULL, /* fcc_retrieve */
1699 fcc_get_principal,
1700 fcc_get_first,
1701 fcc_get_next,
1702 fcc_end_get,
1703 fcc_remove_cred,
1704 fcc_set_flags,
1705 fcc_get_version,
1706 fcc_get_cache_first,
1707 fcc_get_cache_next,
1708 fcc_end_cache_get,
1709 fcc_move,
1710 fcc_get_default_name,
1711 fcc_set_default_cache,
1712 fcc_lastchange,
1713 fcc_set_kdc_offset,
1714 fcc_get_kdc_offset,
1715 fcc_get_name_2,
1716 fcc_resolve_2