2 * Copyright (c) 1997 - 2017 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
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
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
36 #include "krb5_locl.h"
38 typedef struct krb5_fcache
{
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
,
76 if (FCACHE(id
) == NULL
)
77 return KRB5_CC_NOTFOUND
;
82 *colname
= FILENAME(id
);
88 KRB5_LIB_FUNCTION
int KRB5_LIB_CALL
89 _krb5_xlock(krb5_context context
, int fd
, krb5_boolean exclusive
,
98 l
.l_type
= exclusive
? F_WRLCK
: F_RDLCK
;
99 l
.l_whence
= SEEK_SET
;
100 ret
= fcntl(fd
, F_SETLKW
, &l
);
102 ret
= flock(fd
, exclusive
? LOCK_EX
: LOCK_SH
);
106 if(ret
== EACCES
) /* fcntl can return EACCES instead of EAGAIN */
112 case EINVAL
: /* filesystem doesn't support locking, let the user have it */
116 krb5_set_error_message(context
, ret
,
117 N_("timed out locking cache file %s", "file"),
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
);
132 KRB5_LIB_FUNCTION
int KRB5_LIB_CALL
133 _krb5_xunlock(krb5_context context
, int fd
)
141 l
.l_whence
= SEEK_SET
;
142 ret
= fcntl(fd
, F_SETLKW
, &l
);
144 ret
= flock(fd
, LOCK_UN
);
151 case EINVAL
: /* filesystem doesn't support locking, let the user have it */
156 rk_strerror_r(ret
, buf
, sizeof(buf
));
157 krb5_set_error_message(context
, ret
,
158 N_("Failed to unlock file: %s", ""), buf
);
165 static krb5_error_code
166 write_storage(krb5_context context
, krb5_storage
*sp
, int fd
)
172 ret
= krb5_storage_to_data(sp
, &data
);
174 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
177 sret
= write(fd
, data
.data
, data
.length
);
178 ret
= (sret
!= (ssize_t
)data
.length
);
179 krb5_data_free(&data
);
182 krb5_set_error_message(context
, ret
,
183 N_("Failed to write FILE credential data", ""));
190 static krb5_error_code KRB5_CALLCONV
191 fcc_lock(krb5_context context
, krb5_ccache id
,
192 int fd
, krb5_boolean exclusive
)
197 if (exclusive
== FALSE
)
199 ret
= fcc_get_name_2(context
, id
, &name
, NULL
, NULL
);
201 ret
= _krb5_xlock(context
, fd
, exclusive
, name
);
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
,
225 if (res
== NULL
&& sub
== NULL
)
226 return krb5_einval(context
, 3);
230 if ((ret
= fcc_get_default_name(context
, &freeme
)))
232 res
= freeme
+ sizeof("FILE:") - 1;
233 } else if (!sub
&& (sub
= strchr(res
, FILESUBSEPCHR
))) {
234 if (sub
[1] == '\0') {
237 /* `res' has a subsidiary component, so split on it */
238 if ((freeme
= strndup(res
, sub
- res
)) == NULL
)
239 return krb5_enomem(context
);
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
) {
258 return krb5_enomem(context
);
262 (*id
)->data
.data
= f
;
263 (*id
)->data
.length
= sizeof(*f
);
270 * Try to scrub the contents of `filename' safely.
279 pos
= lseek(fd
, 0, SEEK_END
);
282 if (lseek(fd
, 0, SEEK_SET
) < 0)
284 memset(buf
, 0, sizeof(buf
));
287 size_t wr
= sizeof(buf
);
290 tmp
= write(fd
, buf
, wr
);
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
311 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
312 _krb5_erase_file(krb5_context context
, const char *filename
)
315 struct stat sb1
, sb2
;
318 ret
= lstat (filename
, &sb1
);
326 fd
= open(filename
, O_RDWR
| O_BINARY
| O_CLOEXEC
| O_NOFOLLOW
);
334 ret
= _krb5_xlock(context
, fd
, 1, filename
);
339 if (unlink(filename
) < 0) {
342 krb5_set_error_message(context
, errno
,
343 N_("krb5_cc_destroy: unlinking \"%s\": %s", ""),
344 filename
, strerror(ret
));
347 ret
= fstat(fd
, &sb2
);
354 /* check if someone was playing with symlinks */
356 if (sb1
.st_dev
!= sb2
.st_dev
|| sb1
.st_ino
!= sb2
.st_ino
) {
361 /* there are still hard links to this file */
363 if (sb2
.st_nlink
!= 0) {
368 ret
= scrub_file(fd
);
373 static krb5_error_code KRB5_CALLCONV
374 fcc_gen_new(krb5_context context
, krb5_ccache
*id
)
376 char *file
= NULL
, *exp_file
= NULL
;
381 f
= calloc(1, sizeof(*f
));
383 krb5_set_error_message(context
, KRB5_CC_NOMEM
,
384 N_("malloc: out of memory", ""));
385 return KRB5_CC_NOMEM
;
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.
393 ret
= asprintf(&file
, "%sXXXXXX", KRB5_DEFAULT_CCFILE_ROOT
);
394 if(ret
< 0 || file
== NULL
) {
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
);
409 fd
= mkostemp(exp_file
, O_CLOEXEC
);
411 ret
= (krb5_error_code
)errno
;
412 krb5_set_error_message(context
, ret
, N_("mkstemp %s failed", ""), exp_file
);
418 f
->filename
= exp_file
;
419 f
->res
= strdup(exp_file
); /* XXX See above commentary about collection */
422 (*id
)->data
.data
= f
;
423 (*id
)->data
.length
= sizeof(*f
);
428 storage_set_flags(krb5_context context
, krb5_storage
*sp
, int 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
;
437 case KRB5_FCC_FVNO_2
:
438 flags
|= KRB5_STORAGE_HOST_BYTEORDER
;
440 case KRB5_FCC_FVNO_3
:
441 flags
|= KRB5_STORAGE_KEYBLOCK_KEYTYPE_TWICE
;
443 case KRB5_FCC_FVNO_4
:
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
,
455 const char *operation
,
460 krb5_boolean exclusive
= ((flags
| O_WRONLY
) == flags
||
461 (flags
| O_RDWR
) == flags
);
463 const char *filename
;
464 struct stat sb1
, sb2
;
472 flags
|= O_BINARY
| O_CLOEXEC
| O_NOFOLLOW
;
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.
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", ""),
496 free(TMPFILENAME(id
));
497 TMPFILENAME(id
) = NULL
;
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;
510 memset(&sb1
, 0, sizeof(sb1
));
511 ret
= lstat(filename
, &sb1
);
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
);
518 } else if (errno
!= ENOENT
|| !(flags
& O_CREAT
)) {
519 krb5_set_error_message(context
, errno
, N_("%s lstat(%s)", "file, error"),
520 operation
, filename
);
524 fd
= open(filename
, flags
, mode
);
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
);
535 ret
= fstat(fd
, &sb2
);
537 krb5_clear_error_message(context
);
542 if (!S_ISREG(sb2
.st_mode
)) {
543 krb5_set_error_message(context
, EPERM
, N_("Refuses to open non files caches: FILE:%s", ""), filename
);
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...
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
);
566 krb5_set_error_message(context
, EPERM
, N_("Raced too many times with renames of FILE:%s", ""), filename
);
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
);
590 if (strict_checking
) {
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
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
);
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
);
616 if((ret
= fcc_lock(context
, id
, fd
, exclusive
)) != 0) {
624 static krb5_error_code KRB5_CALLCONV
625 fcc_initialize(krb5_context context
,
627 krb5_principal primary_principal
)
629 krb5_fcache
*f
= FCACHE(id
);
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);
645 sp
= krb5_storage_emem();
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
;
652 f
->version
= KRB5_FCC_FVNO_4
;
654 ret
= krb5_store_int8(sp
, 5);
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) {
660 if (context
->kdc_sec_offset
) {
662 ret
= krb5_store_int16 (sp
, 12); /* length */
664 ret
= krb5_store_int16 (sp
, FCC_TAG_DELTATIME
); /* Tag */
666 ret
= krb5_store_int16 (sp
, 8); /* length of data */
668 ret
= krb5_store_int32 (sp
, context
->kdc_sec_offset
);
670 ret
= krb5_store_int32 (sp
, context
->kdc_usec_offset
);
673 ret
= krb5_store_int16 (sp
, 0);
677 ret
= krb5_store_principal(sp
, primary_principal
);
680 ret
= write_storage(context
, sp
, fd
);
682 krb5_storage_free(sp
);
688 rk_strerror_r(ret
, buf
, sizeof(buf
));
689 krb5_set_error_message(context
, ret
, N_("close %s: %s", ""),
695 static krb5_error_code KRB5_CALLCONV
696 fcc_close(krb5_context context
,
699 if (FCACHE(id
) == NULL
)
700 return krb5_einval(context
, 2);
703 (void) unlink(TMPFILENAME(id
));
704 free(TMPFILENAME(id
));
705 free(RESFILENAME(id
));
706 free(SUBFILENAME(id
));
708 krb5_data_free(&id
->data
);
712 static krb5_error_code KRB5_CALLCONV
713 fcc_destroy(krb5_context context
,
716 if (FCACHE(id
) == NULL
)
717 return krb5_einval(context
, 2);
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
,
732 ret
= fcc_open(context
, id
, "store", &fd
, O_WRONLY
| O_APPEND
, 0);
738 sp
= krb5_storage_emem();
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
);
745 ret
= write_storage(context
, sp
, fd
);
746 krb5_storage_free(sp
);
752 rk_strerror_r(ret
, buf
, sizeof(buf
));
753 krb5_set_error_message(context
, ret
, N_("close %s: %s", ""),
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
));
766 free(TMPFILENAME(id
));
767 TMPFILENAME(id
) = NULL
;
775 static krb5_error_code
776 init_fcc(krb5_context context
,
778 const char *operation
,
779 krb5_storage
**ret_sp
,
781 krb5_deltat
*kdc_offset
)
793 ret
= fcc_open(context
, id
, operation
, &fd
, O_RDONLY
, 0);
797 sp
= krb5_storage_stdio_from_fd(fd
, "r");
799 krb5_clear_error_message(context
);
803 krb5_storage_set_eof_code(sp
, KRB5_CC_END
);
804 ret
= krb5_ret_int8(sp
, &pvno
);
806 if(ret
== KRB5_CC_END
) {
808 krb5_set_error_message(context
, ret
,
809 N_("Empty credential cache file: %s", ""),
812 krb5_set_error_message(context
, ret
, N_("Error reading pvno "
813 "in cache file: %s", ""),
818 ret
= KRB5_CCACHE_BADVNO
;
819 krb5_set_error_message(context
, ret
, N_("Bad version number in credential "
820 "cache file: %s", ""),
824 ret
= krb5_ret_int8(sp
, &tag
); /* should not be host byte order */
826 ret
= KRB5_CC_FORMAT
;
827 krb5_set_error_message(context
, ret
, "Error reading tag in "
828 "cache file: %s", FILENAME(id
));
831 FCACHE(id
)->version
= tag
;
832 storage_set_flags(context
, sp
, FCACHE(id
)->version
);
834 case KRB5_FCC_FVNO_4
: {
837 ret
= krb5_ret_int16 (sp
, &length
);
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
));
846 int16_t dtag
, data_len
;
850 ret
= krb5_ret_int16 (sp
, &dtag
);
852 ret
= KRB5_CC_FORMAT
;
853 krb5_set_error_message(context
, ret
, N_("Error reading dtag in "
854 "cache file: %s", ""),
858 ret
= krb5_ret_int16 (sp
, &data_len
);
860 ret
= KRB5_CC_FORMAT
;
861 krb5_set_error_message(context
, ret
,
862 N_("Error reading dlength "
863 "in cache file: %s",""),
868 case FCC_TAG_DELTATIME
: {
871 ret
= krb5_ret_int32 (sp
, &offset
);
872 ret
|= krb5_ret_int32 (sp
, &context
->kdc_usec_offset
);
874 ret
= KRB5_CC_FORMAT
;
875 krb5_set_error_message(context
, ret
,
876 N_("Error reading kdc_sec in "
877 "cache file: %s", ""),
881 context
->kdc_sec_offset
= offset
;
883 *kdc_offset
= offset
;
887 for (i
= 0; i
< data_len
; ++i
) {
888 ret
= krb5_ret_int8 (sp
, &dummy
);
890 ret
= KRB5_CC_FORMAT
;
891 krb5_set_error_message(context
, ret
,
892 N_("Error reading unknown "
893 "tag in cache file: %s", ""),
900 length
-= 4 + data_len
;
904 case KRB5_FCC_FVNO_3
:
905 case KRB5_FCC_FVNO_2
:
906 case KRB5_FCC_FVNO_1
:
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
));
922 krb5_storage_free(sp
);
927 static krb5_error_code KRB5_CALLCONV
928 fcc_get_principal(krb5_context context
,
930 krb5_principal
*principal
)
936 ret
= init_fcc (context
, id
, "get-principal", &sp
, &fd
, NULL
);
939 ret
= krb5_ret_principal(sp
, principal
);
941 krb5_clear_error_message(context
);
942 krb5_storage_free(sp
);
947 static krb5_error_code KRB5_CALLCONV
948 fcc_end_get(krb5_context context
,
950 krb5_cc_cursor
*cursor
);
952 static krb5_error_code KRB5_CALLCONV
953 fcc_get_first(krb5_context context
,
955 krb5_cc_cursor
*cursor
)
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", ""));
969 ret
= init_fcc(context
, id
, "get-first", &FCC_CURSOR(*cursor
)->sp
,
970 &FCC_CURSOR(*cursor
)->fd
, NULL
);
976 ret
= krb5_ret_principal (FCC_CURSOR(*cursor
)->sp
, &principal
);
978 krb5_clear_error_message(context
);
979 fcc_end_get(context
, id
, cursor
);
982 krb5_free_principal (context
, principal
);
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
,
1000 krb5_cc_cursor
*cursor
,
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);
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
);
1021 krb5_clear_error_message(context
);
1025 if (!cred_removed(creds
))
1028 krb5_free_cred_contents(context
, creds
);
1034 static krb5_error_code KRB5_CALLCONV
1035 fcc_end_get (krb5_context context
,
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
);
1053 static void KRB5_CALLCONV
1054 cred_delete(krb5_context context
,
1056 krb5_cc_cursor
*cursor
,
1059 krb5_error_code ret
;
1061 krb5_data orig_cred_data
;
1062 unsigned char *cred_data_in_file
= NULL
;
1064 struct stat sb1
, sb2
;
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();
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
);
1087 ret
= krb5_storage_to_data(sp
, &orig_cred_data
);
1090 krb5_storage_free(sp
);
1092 cred_data_in_file
= malloc(orig_cred_data
.length
);
1093 if (cred_data_in_file
== NULL
)
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:");
1112 sp
= krb5_storage_emem();
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", ""),
1133 ret
= fcc_open(context
, id
, "remove_cred", &fd
, O_RDWR
, 0);
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)
1144 if (sb1
.st_dev
!= sb2
.st_dev
|| sb1
.st_ino
!= sb2
.st_ino
)
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)
1157 bytes
= read(fd
, cred_data_in_file
, orig_cred_data
.length
);
1158 if (bytes
!= orig_cred_data
.length
)
1160 if (memcmp(orig_cred_data
.data
, cred_data_in_file
, bytes
) != 0)
1162 if (lseek(fd
, FCC_CURSOR(*cursor
)->cred_start
, SEEK_SET
) == (off_t
)-1)
1164 ret
= write_storage(context
, sp
, fd
);
1167 if (close(fd
) < 0 && ret
== 0) {
1168 krb5_set_error_message(context
, errno
, N_("close %s", ""),
1172 krb5_data_free(&orig_cred_data
);
1173 free(cred_data_in_file
);
1174 krb5_storage_free(sp
);
1178 static krb5_error_code KRB5_CALLCONV
1179 fcc_remove_cred(krb5_context context
,
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
);
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
);
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 */
1205 if (ret
== KRB5_CC_END
)
1210 static krb5_error_code KRB5_CALLCONV
1211 fcc_set_flags(krb5_context context
,
1215 if (FCACHE(id
) == NULL
)
1216 return krb5_einval(context
, 2);
1221 static int KRB5_CALLCONV
1222 fcc_get_version(krb5_context context
,
1225 if (FCACHE(id
) == NULL
)
1228 return FCACHE(id
)->version
;
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
++) {
1240 if (*p
== '/' || *p
== '\\')
1250 /* We could use an rk_dirname()... */
1252 my_dirname(const char *fn
)
1257 if (strncmp(fn
, "FILE:", sizeof("FILE:") - 1) == 0)
1258 fn
+= sizeof("FILE:") - 1;
1260 if ((dname
= strdup(fn
)) == NULL
)
1262 len
= strlen(dname
);
1263 for (i
= 0; i
< len
; i
++) {
1265 if (dname
[len
- i
] == '\\' ||
1266 dname
[len
- i
] == '/') {
1267 dname
[len
- i
] = '\0';
1271 if (dname
[len
- i
] == '/') {
1272 dname
[len
- i
] = '\0';
1284 * This checks that a directory entry matches a required basename and has a
1285 * non-empty subsidiary component.
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
};
1318 if ((sep
= strchr(name
, FILESUBSEPCHR
)))
1319 namelen
= (size_t)(sep
- name
);
1321 namelen
= strlen(name
);
1322 if (def_locs
== NULL
)
1324 for (i
= 0; !(*res
) && def_locs
[i
]; i
++) {
1327 if ((ret
= _krb5_expand_default_cc_name(context
, def_locs
[i
], &e
)))
1329 *res
= strncmp(e
, name
, namelen
) == 0 &&
1330 (sep
== NULL
|| e
[namelen
] == FILESUBSEPCHR
|| e
[namelen
] == '\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 */
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",
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
1387 if ((ret
= is_default_collection(context
, def_ccname
,
1388 (const char **)def_locs
,
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
);
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
;
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 */
1419 krb5_config_free_strings(def_locs
);
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
;
1438 iter
->curr_location
= NULL
;
1439 if (iter
->locations
&&
1440 (iter
->curr_location
= iter
->locations
[++(iter
->location
)]))
1443 iter
->dead
= 1; /* Do not run off the end of iter->locations */
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
)
1452 const char *base
= my_basename(iter
->curr_location
);
1453 size_t baselen
= strlen(base
);
1457 if (iter
->d
== NULL
)
1459 for (iter
->dentry
= readdir(iter
->d
);
1461 iter
->dentry
= readdir(iter
->d
)) {
1462 if (!matchbase(iter
->dentry
->d_name
, base
, baselen
))
1464 if (asprintf(&s
, "FILE:%s/%s", iter
->dname
, iter
->dentry
->d_name
) == -1 ||
1466 return krb5_enomem(context
);
1467 if (stat(s
+ sizeof("FILE:") - 1, &st
) == 0 && S_ISREG(st
.st_mode
)) {
1473 iter
->curr_location
= NULL
;
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
;
1486 ret
= krb5_cc_resolve(context
, ccname
, &cc
);
1490 if (strcmp(krb5_cc_get_type(context
, cc
), "FILE") == 0) {
1494 krb5_cc_close(context
, cc
);
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
;
1509 return krb5_einval(context
, 2);
1511 /* Do not run off the end of iter->locations */
1515 if (!iter
->curr_location
) {
1516 /* Next base location */
1517 if ((ret
= next_location(context
, iter
)))
1519 /* Output the current base location */
1520 if ((ret
= try1(context
, iter
->curr_location
, id
)) || *id
)
1524 /* Look for subsidiaries of iter->curr_location */
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
)))
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
) {
1547 /* Directory listing exhausted -> go to next location, tail-recurse */
1548 if ((ret
= next_location(context
, iter
)))
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
;
1559 return krb5_einval(context
, 2);
1561 krb5_config_free_strings(iter
->locations
);
1564 free(iter
->def_ccname
);
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
);
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
)) {
1591 } else if (rk_rename(f
->filename
, t
->filename
)) {
1595 * We need only close from -- we can't destroy it since the rename
1596 * succeeded, which "destroyed" it at its old name.
1599 krb5_cc_close(context
, from
);
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
,
1611 static krb5_error_code KRB5_CALLCONV
1612 fcc_set_default_cache(krb5_context context
, krb5_ccache id
)
1614 krb5_error_code ret
;
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
);
1630 ret
= krb5_cc_copy_cache(context
, id
, dest
);
1633 krb5_set_error_message(context
, ret
,
1634 N_("Failed to copy subsidiary cache file %s to "
1635 "default %s", ""), FILENAME(id
),
1640 static krb5_error_code KRB5_CALLCONV
1641 fcc_lastchange(krb5_context context
, krb5_ccache id
, krb5_timestamp
*mtime
)
1643 krb5_error_code ret
;
1647 ret
= fcc_open(context
, id
, "lastchange", &fd
, O_RDONLY
, 0);
1650 ret
= fstat(fd
, &sb
);
1654 krb5_set_error_message(context
, ret
, N_("Failed to stat cache file", ""));
1657 *mtime
= sb
.st_mtime
;
1661 static krb5_error_code KRB5_CALLCONV
1662 fcc_set_kdc_offset(krb5_context context
, krb5_ccache id
, krb5_deltat kdc_offset
)
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
;
1673 ret
= init_fcc(context
, id
, "get-kdc-offset", &sp
, &fd
, kdc_offset
);
1675 krb5_storage_free(sp
);
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
,
1698 NULL
, /* fcc_retrieve */
1706 fcc_get_cache_first
,
1710 fcc_get_default_name
,
1711 fcc_set_default_cache
,