2 * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include "krb5_locl.h"
36 typedef struct krb5_fcache
{
46 #define KRB5_FCC_FVNO_1 1
47 #define KRB5_FCC_FVNO_2 2
48 #define KRB5_FCC_FVNO_3 3
49 #define KRB5_FCC_FVNO_4 4
51 #define FCC_TAG_DELTATIME 1
53 #define FCACHE(X) ((krb5_fcache*)(X)->data.data)
55 #define FILENAME(X) (FCACHE(X)->filename)
57 #define FCC_CURSOR(C) ((struct fcc_cursor*)(C))
60 fcc_get_name(krb5_context context
,
67 _krb5_xlock(krb5_context context
, int fd
, krb5_boolean exclusive
,
76 l
.l_type
= exclusive
? F_WRLCK
: F_RDLCK
;
77 l
.l_whence
= SEEK_SET
;
78 ret
= fcntl(fd
, F_SETLKW
, &l
);
80 ret
= flock(fd
, exclusive
? LOCK_EX
: LOCK_SH
);
84 if(ret
== EACCES
) /* fcntl can return EACCES instead of EAGAIN */
90 case EINVAL
: /* filesystem doesn't support locking, let the user have it */
94 krb5_set_error_message(context
, ret
,
95 N_("timed out locking cache file %s", "file"),
100 rk_strerror_r(ret
, buf
, sizeof(buf
));
101 krb5_set_error_message(context
, ret
,
102 N_("error locking cache file %s: %s",
103 "file, error"), filename
, buf
);
111 _krb5_xunlock(krb5_context context
, int fd
)
119 l
.l_whence
= SEEK_SET
;
120 ret
= fcntl(fd
, F_SETLKW
, &l
);
122 ret
= flock(fd
, LOCK_UN
);
129 case EINVAL
: /* filesystem doesn't support locking, let the user have it */
134 rk_strerror_r(ret
, buf
, sizeof(buf
));
135 krb5_set_error_message(context
, ret
,
136 N_("Failed to unlock file: %s", ""), buf
);
143 static krb5_error_code
144 write_storage(krb5_context context
, krb5_storage
*sp
, int fd
)
150 ret
= krb5_storage_to_data(sp
, &data
);
152 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
155 sret
= write(fd
, data
.data
, data
.length
);
156 ret
= (sret
!= data
.length
);
157 krb5_data_free(&data
);
160 krb5_set_error_message(context
, ret
,
161 N_("Failed to write FILE credential data", ""));
168 static krb5_error_code
169 fcc_lock(krb5_context context
, krb5_ccache id
,
170 int fd
, krb5_boolean exclusive
)
172 return _krb5_xlock(context
, fd
, exclusive
, fcc_get_name(context
, id
));
175 static krb5_error_code
176 fcc_unlock(krb5_context context
, int fd
)
178 return _krb5_xunlock(context
, fd
);
181 static krb5_error_code
182 fcc_resolve(krb5_context context
, krb5_ccache
*id
, const char *res
)
185 f
= malloc(sizeof(*f
));
187 krb5_set_error_message(context
, KRB5_CC_NOMEM
,
188 N_("malloc: out of memory", ""));
189 return KRB5_CC_NOMEM
;
191 f
->filename
= strdup(res
);
192 if(f
->filename
== NULL
){
194 krb5_set_error_message(context
, KRB5_CC_NOMEM
,
195 N_("malloc: out of memory", ""));
196 return KRB5_CC_NOMEM
;
199 (*id
)->data
.data
= f
;
200 (*id
)->data
.length
= sizeof(*f
);
205 * Try to scrub the contents of `filename' safely.
214 pos
= lseek(fd
, 0, SEEK_END
);
217 if (lseek(fd
, 0, SEEK_SET
) < 0)
219 memset(buf
, 0, sizeof(buf
));
221 ssize_t tmp
= write(fd
, buf
, min(sizeof(buf
), pos
));
232 * Erase `filename' if it exists, trying to remove the contents if
233 * it's `safe'. We always try to remove the file, it it exists. It's
234 * only overwritten if it's a regular file (not a symlink and not a
239 _krb5_erase_file(krb5_context context
, const char *filename
)
242 struct stat sb1
, sb2
;
245 ret
= lstat (filename
, &sb1
);
249 fd
= open(filename
, O_RDWR
| O_BINARY
);
257 ret
= _krb5_xlock(context
, fd
, 1, filename
);
262 if (unlink(filename
) < 0) {
263 _krb5_xunlock(context
, fd
);
267 ret
= fstat (fd
, &sb2
);
269 _krb5_xunlock(context
, fd
);
274 /* check if someone was playing with symlinks */
276 if (sb1
.st_dev
!= sb2
.st_dev
|| sb1
.st_ino
!= sb2
.st_ino
) {
277 _krb5_xunlock(context
, fd
);
282 /* there are still hard links to this file */
284 if (sb2
.st_nlink
!= 0) {
285 _krb5_xunlock(context
, fd
);
290 ret
= scrub_file (fd
);
292 _krb5_xunlock(context
, fd
);
296 ret
= _krb5_xunlock(context
, fd
);
301 static krb5_error_code
302 fcc_gen_new(krb5_context context
, krb5_ccache
*id
)
308 f
= malloc(sizeof(*f
));
310 krb5_set_error_message(context
, KRB5_CC_NOMEM
,
311 N_("malloc: out of memory", ""));
312 return KRB5_CC_NOMEM
;
314 asprintf (&file
, "%sXXXXXX", KRB5_DEFAULT_CCFILE_ROOT
);
317 krb5_set_error_message(context
, KRB5_CC_NOMEM
,
318 N_("malloc: out of memory", ""));
319 return KRB5_CC_NOMEM
;
324 krb5_set_error_message(context
, ret
, N_("mkstemp %s failed", ""), file
);
332 (*id
)->data
.data
= f
;
333 (*id
)->data
.length
= sizeof(*f
);
338 storage_set_flags(krb5_context context
, krb5_storage
*sp
, int vno
)
342 case KRB5_FCC_FVNO_1
:
343 flags
|= KRB5_STORAGE_PRINCIPAL_WRONG_NUM_COMPONENTS
;
344 flags
|= KRB5_STORAGE_PRINCIPAL_NO_NAME_TYPE
;
345 flags
|= KRB5_STORAGE_HOST_BYTEORDER
;
347 case KRB5_FCC_FVNO_2
:
348 flags
|= KRB5_STORAGE_HOST_BYTEORDER
;
350 case KRB5_FCC_FVNO_3
:
351 flags
|= KRB5_STORAGE_KEYBLOCK_KEYTYPE_TWICE
;
353 case KRB5_FCC_FVNO_4
:
357 "storage_set_flags called with bad vno (%x)", vno
);
359 krb5_storage_set_flags(sp
, flags
);
362 static krb5_error_code
363 fcc_open(krb5_context context
,
369 krb5_boolean exclusive
= ((flags
| O_WRONLY
) == flags
||
370 (flags
| O_RDWR
) == flags
);
372 const char *filename
= FILENAME(id
);
374 fd
= open(filename
, flags
, mode
);
378 rk_strerror_r(ret
, buf
, sizeof(buf
));
379 krb5_set_error_message(context
, ret
, N_("open(%s): %s", "file, error"),
385 if((ret
= fcc_lock(context
, id
, fd
, exclusive
)) != 0) {
393 static krb5_error_code
394 fcc_initialize(krb5_context context
,
396 krb5_principal primary_principal
)
398 krb5_fcache
*f
= FCACHE(id
);
401 char *filename
= f
->filename
;
405 ret
= fcc_open(context
, id
, &fd
, O_RDWR
| O_CREAT
| O_EXCL
| O_BINARY
| O_CLOEXEC
, 0600);
410 sp
= krb5_storage_emem();
411 krb5_storage_set_eof_code(sp
, KRB5_CC_END
);
412 if(context
->fcache_vno
!= 0)
413 f
->version
= context
->fcache_vno
;
415 f
->version
= KRB5_FCC_FVNO_4
;
416 ret
|= krb5_store_int8(sp
, 5);
417 ret
|= krb5_store_int8(sp
, f
->version
);
418 storage_set_flags(context
, sp
, f
->version
);
419 if(f
->version
== KRB5_FCC_FVNO_4
&& ret
== 0) {
421 if (context
->kdc_sec_offset
) {
422 ret
|= krb5_store_int16 (sp
, 12); /* length */
423 ret
|= krb5_store_int16 (sp
, FCC_TAG_DELTATIME
); /* Tag */
424 ret
|= krb5_store_int16 (sp
, 8); /* length of data */
425 ret
|= krb5_store_int32 (sp
, context
->kdc_sec_offset
);
426 ret
|= krb5_store_int32 (sp
, context
->kdc_usec_offset
);
428 ret
|= krb5_store_int16 (sp
, 0);
431 ret
|= krb5_store_principal(sp
, primary_principal
);
433 ret
|= write_storage(context
, sp
, fd
);
435 krb5_storage_free(sp
);
437 fcc_unlock(context
, fd
);
442 rk_strerror_r(ret
, buf
, sizeof(buf
));
443 krb5_set_error_message (context
, ret
, N_("close %s: %s", ""),
449 static krb5_error_code
450 fcc_close(krb5_context context
,
454 krb5_data_free(&id
->data
);
458 static krb5_error_code
459 fcc_destroy(krb5_context context
,
462 _krb5_erase_file(context
, FILENAME(id
));
466 static krb5_error_code
467 fcc_store_cred(krb5_context context
,
474 ret
= fcc_open(context
, id
, &fd
, O_WRONLY
| O_APPEND
| O_BINARY
| O_CLOEXEC
, 0);
480 sp
= krb5_storage_emem();
481 krb5_storage_set_eof_code(sp
, KRB5_CC_END
);
482 storage_set_flags(context
, sp
, FCACHE(id
)->version
);
483 if (!krb5_config_get_bool_default(context
, NULL
, TRUE
,
485 "fcc-mit-ticketflags",
487 krb5_storage_set_flags(sp
, KRB5_STORAGE_CREDS_FLAGS_WRONG_BITORDER
);
488 ret
= krb5_store_creds(sp
, creds
);
490 ret
= write_storage(context
, sp
, fd
);
491 krb5_storage_free(sp
);
493 fcc_unlock(context
, fd
);
497 rk_strerror_r(ret
, buf
, sizeof(buf
));
499 krb5_set_error_message (context
, ret
, N_("close %s: %s", ""),
506 static krb5_error_code
507 init_fcc (krb5_context context
,
509 krb5_storage
**ret_sp
,
517 ret
= fcc_open(context
, id
, &fd
, O_RDONLY
| O_BINARY
| O_CLOEXEC
, 0);
521 sp
= krb5_storage_from_fd(fd
);
523 krb5_clear_error_message(context
);
527 krb5_storage_set_eof_code(sp
, KRB5_CC_END
);
528 ret
= krb5_ret_int8(sp
, &pvno
);
530 if(ret
== KRB5_CC_END
) {
532 krb5_set_error_message(context
, ret
,
533 N_("Empty credential cache file: %s", ""),
536 krb5_set_error_message(context
, ret
, N_("Error reading pvno "
537 "in cache file: %s", ""),
542 ret
= KRB5_CCACHE_BADVNO
;
543 krb5_set_error_message(context
, ret
, N_("Bad version number in credential "
544 "cache file: %s", ""),
548 ret
= krb5_ret_int8(sp
, &tag
); /* should not be host byte order */
550 ret
= KRB5_CC_FORMAT
;
551 krb5_set_error_message(context
, ret
, "Error reading tag in "
552 "cache file: %s", FILENAME(id
));
555 FCACHE(id
)->version
= tag
;
556 storage_set_flags(context
, sp
, FCACHE(id
)->version
);
558 case KRB5_FCC_FVNO_4
: {
561 ret
= krb5_ret_int16 (sp
, &length
);
563 ret
= KRB5_CC_FORMAT
;
564 krb5_set_error_message(context
, ret
,
565 N_("Error reading tag length in "
566 "cache file: %s", ""), FILENAME(id
));
570 int16_t dtag
, data_len
;
574 ret
= krb5_ret_int16 (sp
, &dtag
);
576 ret
= KRB5_CC_FORMAT
;
577 krb5_set_error_message(context
, ret
, N_("Error reading dtag in "
578 "cache file: %s", ""),
582 ret
= krb5_ret_int16 (sp
, &data_len
);
584 ret
= KRB5_CC_FORMAT
;
585 krb5_set_error_message(context
, ret
,
586 N_("Error reading dlength "
587 "in cache file: %s",""),
592 case FCC_TAG_DELTATIME
:
593 ret
= krb5_ret_int32 (sp
, &context
->kdc_sec_offset
);
595 ret
= KRB5_CC_FORMAT
;
596 krb5_set_error_message(context
, ret
,
597 N_("Error reading kdc_sec in "
598 "cache file: %s", ""),
602 ret
= krb5_ret_int32 (sp
, &context
->kdc_usec_offset
);
604 ret
= KRB5_CC_FORMAT
;
605 krb5_set_error_message(context
, ret
,
606 N_("Error reading kdc_usec in "
607 "cache file: %s", ""),
613 for (i
= 0; i
< data_len
; ++i
) {
614 ret
= krb5_ret_int8 (sp
, &dummy
);
616 ret
= KRB5_CC_FORMAT
;
617 krb5_set_error_message(context
, ret
,
618 N_("Error reading unknown "
619 "tag in cache file: %s", ""),
626 length
-= 4 + data_len
;
630 case KRB5_FCC_FVNO_3
:
631 case KRB5_FCC_FVNO_2
:
632 case KRB5_FCC_FVNO_1
:
635 ret
= KRB5_CCACHE_BADVNO
;
636 krb5_set_error_message(context
, ret
,
637 N_("Unknown version number (%d) in "
638 "credential cache file: %s", ""),
639 (int)tag
, FILENAME(id
));
648 krb5_storage_free(sp
);
649 fcc_unlock(context
, fd
);
654 static krb5_error_code
655 fcc_get_principal(krb5_context context
,
657 krb5_principal
*principal
)
663 ret
= init_fcc (context
, id
, &sp
, &fd
);
666 ret
= krb5_ret_principal(sp
, principal
);
668 krb5_clear_error_message(context
);
669 krb5_storage_free(sp
);
670 fcc_unlock(context
, fd
);
675 static krb5_error_code
676 fcc_end_get (krb5_context context
,
678 krb5_cc_cursor
*cursor
);
680 static krb5_error_code
681 fcc_get_first (krb5_context context
,
683 krb5_cc_cursor
*cursor
)
686 krb5_principal principal
;
688 *cursor
= malloc(sizeof(struct fcc_cursor
));
689 if (*cursor
== NULL
) {
690 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
693 memset(*cursor
, 0, sizeof(struct fcc_cursor
));
695 ret
= init_fcc (context
, id
, &FCC_CURSOR(*cursor
)->sp
,
696 &FCC_CURSOR(*cursor
)->fd
);
702 ret
= krb5_ret_principal (FCC_CURSOR(*cursor
)->sp
, &principal
);
704 krb5_clear_error_message(context
);
705 fcc_end_get(context
, id
, cursor
);
708 krb5_free_principal (context
, principal
);
709 fcc_unlock(context
, FCC_CURSOR(*cursor
)->fd
);
713 static krb5_error_code
714 fcc_get_next (krb5_context context
,
716 krb5_cc_cursor
*cursor
,
720 if((ret
= fcc_lock(context
, id
, FCC_CURSOR(*cursor
)->fd
, FALSE
)) != 0)
723 ret
= krb5_ret_creds(FCC_CURSOR(*cursor
)->sp
, creds
);
725 krb5_clear_error_message(context
);
727 fcc_unlock(context
, FCC_CURSOR(*cursor
)->fd
);
731 static krb5_error_code
732 fcc_end_get (krb5_context context
,
734 krb5_cc_cursor
*cursor
)
736 krb5_storage_free(FCC_CURSOR(*cursor
)->sp
);
737 close (FCC_CURSOR(*cursor
)->fd
);
743 static krb5_error_code
744 fcc_remove_cred(krb5_context context
,
750 krb5_ccache copy
, newfile
;
754 ret
= krb5_cc_new_unique(context
, krb5_cc_type_memory
, NULL
, ©
);
758 ret
= krb5_cc_copy_cache(context
, id
, copy
);
760 krb5_cc_destroy(context
, copy
);
764 ret
= krb5_cc_remove_cred(context
, copy
, which
, cred
);
766 krb5_cc_destroy(context
, copy
);
770 asprintf(&newname
, "FILE:%s.XXXXXX", FILENAME(id
));
771 if (newname
== NULL
) {
772 krb5_cc_destroy(context
, copy
);
776 fd
= mkstemp(&newname
[5]);
779 krb5_cc_destroy(context
, copy
);
784 ret
= krb5_cc_resolve(context
, newname
, &newfile
);
788 krb5_cc_destroy(context
, copy
);
792 ret
= krb5_cc_copy_cache(context
, copy
, newfile
);
793 krb5_cc_destroy(context
, copy
);
796 krb5_cc_destroy(context
, newfile
);
800 ret
= rename(&newname
[5], FILENAME(id
));
804 krb5_cc_close(context
, newfile
);
809 static krb5_error_code
810 fcc_set_flags(krb5_context context
,
818 fcc_get_version(krb5_context context
,
821 return FCACHE(id
)->version
;
828 static krb5_error_code
829 fcc_get_cache_first(krb5_context context
, krb5_cc_cursor
*cursor
)
831 struct fcache_iter
*iter
;
833 iter
= calloc(1, sizeof(*iter
));
835 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
843 static krb5_error_code
844 fcc_get_cache_next(krb5_context context
, krb5_cc_cursor cursor
, krb5_ccache
*id
)
846 struct fcache_iter
*iter
= cursor
;
849 char *expandedfn
= NULL
;
852 krb5_clear_error_message(context
);
857 fn
= krb5_cc_default_name(context
);
858 if (fn
== NULL
|| strncasecmp(fn
, "FILE:", 5) != 0) {
859 ret
= _krb5_expand_default_cc_name(context
,
860 KRB5_DEFAULT_CCNAME_FILE
,
866 ret
= krb5_cc_resolve(context
, fn
, id
);
873 static krb5_error_code
874 fcc_end_cache_get(krb5_context context
, krb5_cc_cursor cursor
)
876 struct fcache_iter
*iter
= cursor
;
881 static krb5_error_code
882 fcc_move(krb5_context context
, krb5_ccache from
, krb5_ccache to
)
884 krb5_error_code ret
= 0;
886 ret
= rename(FILENAME(from
), FILENAME(to
));
887 if (ret
&& errno
!= EXDEV
) {
890 rk_strerror_r(ret
, buf
, sizeof(buf
));
891 krb5_set_error_message(context
, ret
,
892 N_("Rename of file from %s "
893 "to %s failed: %s", ""),
894 FILENAME(from
), FILENAME(to
), buf
);
896 } else if (ret
&& errno
== EXDEV
) {
897 /* make a copy and delete the orignal */
898 krb5_ssize_t sz1
, sz2
;
902 ret
= fcc_open(context
, from
, &fd1
, O_RDONLY
| O_BINARY
| O_CLOEXEC
, 0);
906 unlink(FILENAME(to
));
908 ret
= fcc_open(context
, to
, &fd2
,
909 O_WRONLY
| O_CREAT
| O_EXCL
| O_BINARY
| O_CLOEXEC
, 0600);
913 while((sz1
= read(fd1
, buf
, sizeof(buf
))) > 0) {
914 sz2
= write(fd2
, buf
, sz1
);
917 krb5_set_error_message(context
, ret
,
918 N_("Failed to write data from one file "
919 "credential cache to the other", ""));
925 krb5_set_error_message(context
, ret
,
926 N_("Failed to read data from one file "
927 "credential cache to the other", ""));
931 fcc_unlock(context
, fd2
);
935 fcc_unlock(context
, fd1
);
938 _krb5_erase_file(context
, FILENAME(from
));
941 _krb5_erase_file(context
, FILENAME(to
));
946 /* make sure ->version is uptodate */
950 ret
= init_fcc (context
, to
, &sp
, &fd
);
952 krb5_storage_free(sp
);
953 fcc_unlock(context
, fd
);
957 fcc_close(context
, from
);
962 static krb5_error_code
963 fcc_get_default_name(krb5_context context
, char **str
)
965 return _krb5_expand_default_cc_name(context
,
966 KRB5_DEFAULT_CCNAME_FILE
,
970 static krb5_error_code
971 fcc_lastchange(krb5_context context
, krb5_ccache id
, krb5_timestamp
*mtime
)
977 ret
= fcc_open(context
, id
, &fd
, O_RDONLY
| O_BINARY
| O_CLOEXEC
, 0);
980 ret
= fstat(fd
, &sb
);
984 krb5_set_error_message(context
, ret
, N_("Failed to stat cache file", ""));
987 *mtime
= sb
.st_mtime
;
992 * Variable containing the FILE based credential cache implemention.
994 * @ingroup krb5_ccache
997 KRB5_LIB_VARIABLE
const krb5_cc_ops krb5_fcc_ops
= {
1007 NULL
, /* fcc_retrieve */
1015 fcc_get_cache_first
,
1019 fcc_get_default_name
,