2 * Copyright (c) 1997 - 2008 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
{
48 #define KRB5_FCC_FVNO_1 1
49 #define KRB5_FCC_FVNO_2 2
50 #define KRB5_FCC_FVNO_3 3
51 #define KRB5_FCC_FVNO_4 4
53 #define FCC_TAG_DELTATIME 1
55 #define FCACHE(X) ((krb5_fcache*)(X)->data.data)
57 #define FILENAME(X) (FCACHE(X)->filename)
59 #define FCC_CURSOR(C) ((struct fcc_cursor*)(C))
62 fcc_get_name(krb5_context context
,
69 _krb5_xlock(krb5_context context
, int fd
, krb5_boolean exclusive
,
78 l
.l_type
= exclusive
? F_WRLCK
: F_RDLCK
;
79 l
.l_whence
= SEEK_SET
;
80 ret
= fcntl(fd
, F_SETLKW
, &l
);
82 ret
= flock(fd
, exclusive
? LOCK_EX
: LOCK_SH
);
86 if(ret
== EACCES
) /* fcntl can return EACCES instead of EAGAIN */
92 case EINVAL
: /* filesystem doesn't support locking, let the user have it */
96 krb5_set_error_message(context
, ret
,
97 N_("timed out locking cache file %s", "file"),
102 rk_strerror_r(ret
, buf
, sizeof(buf
));
103 krb5_set_error_message(context
, ret
,
104 N_("error locking cache file %s: %s",
105 "file, error"), filename
, buf
);
113 _krb5_xunlock(krb5_context context
, int fd
)
121 l
.l_whence
= SEEK_SET
;
122 ret
= fcntl(fd
, F_SETLKW
, &l
);
124 ret
= flock(fd
, LOCK_UN
);
131 case EINVAL
: /* filesystem doesn't support locking, let the user have it */
136 rk_strerror_r(ret
, buf
, sizeof(buf
));
137 krb5_set_error_message(context
, ret
,
138 N_("Failed to unlock file: %s", ""), buf
);
145 static krb5_error_code
146 write_storage(krb5_context context
, krb5_storage
*sp
, int fd
)
152 ret
= krb5_storage_to_data(sp
, &data
);
154 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
157 sret
= write(fd
, data
.data
, data
.length
);
158 ret
= (sret
!= data
.length
);
159 krb5_data_free(&data
);
162 krb5_set_error_message(context
, ret
,
163 N_("Failed to write FILE credential data", ""));
170 static krb5_error_code
171 fcc_lock(krb5_context context
, krb5_ccache id
,
172 int fd
, krb5_boolean exclusive
)
174 return _krb5_xlock(context
, fd
, exclusive
, fcc_get_name(context
, id
));
177 static krb5_error_code
178 fcc_unlock(krb5_context context
, int fd
)
180 return _krb5_xunlock(context
, fd
);
183 static krb5_error_code
184 fcc_resolve(krb5_context context
, krb5_ccache
*id
, const char *res
)
187 f
= malloc(sizeof(*f
));
189 krb5_set_error_message(context
, KRB5_CC_NOMEM
,
190 N_("malloc: out of memory", ""));
191 return KRB5_CC_NOMEM
;
193 f
->filename
= strdup(res
);
194 if(f
->filename
== NULL
){
196 krb5_set_error_message(context
, KRB5_CC_NOMEM
,
197 N_("malloc: out of memory", ""));
198 return KRB5_CC_NOMEM
;
201 (*id
)->data
.data
= f
;
202 (*id
)->data
.length
= sizeof(*f
);
207 * Try to scrub the contents of `filename' safely.
216 pos
= lseek(fd
, 0, SEEK_END
);
219 if (lseek(fd
, 0, SEEK_SET
) < 0)
221 memset(buf
, 0, sizeof(buf
));
223 ssize_t tmp
= write(fd
, buf
, min(sizeof(buf
), pos
));
238 * Erase `filename' if it exists, trying to remove the contents if
239 * it's `safe'. We always try to remove the file, it it exists. It's
240 * only overwritten if it's a regular file (not a symlink and not a
245 _krb5_erase_file(krb5_context context
, const char *filename
)
248 struct stat sb1
, sb2
;
251 ret
= lstat (filename
, &sb1
);
255 fd
= open(filename
, O_RDWR
| O_BINARY
);
263 ret
= _krb5_xlock(context
, fd
, 1, filename
);
268 if (unlink(filename
) < 0) {
269 _krb5_xunlock(context
, fd
);
273 ret
= fstat (fd
, &sb2
);
275 _krb5_xunlock(context
, fd
);
280 /* check if someone was playing with symlinks */
282 if (sb1
.st_dev
!= sb2
.st_dev
|| sb1
.st_ino
!= sb2
.st_ino
) {
283 _krb5_xunlock(context
, fd
);
288 /* there are still hard links to this file */
290 if (sb2
.st_nlink
!= 0) {
291 _krb5_xunlock(context
, fd
);
296 ret
= scrub_file (fd
);
298 _krb5_xunlock(context
, fd
);
302 ret
= _krb5_xunlock(context
, fd
);
307 static krb5_error_code
308 fcc_gen_new(krb5_context context
, krb5_ccache
*id
)
314 f
= malloc(sizeof(*f
));
316 krb5_set_error_message(context
, KRB5_CC_NOMEM
,
317 N_("malloc: out of memory", ""));
318 return KRB5_CC_NOMEM
;
320 asprintf (&file
, "%sXXXXXX", KRB5_DEFAULT_CCFILE_ROOT
);
323 krb5_set_error_message(context
, KRB5_CC_NOMEM
,
324 N_("malloc: out of memory", ""));
325 return KRB5_CC_NOMEM
;
327 #ifdef KRB5_USE_PATH_TOKENS
329 char * exp_file
= NULL
;
332 ec
= _krb5_expand_path_tokens(context
, file
, &exp_file
);
346 krb5_set_error_message(context
, ret
, N_("mkstemp %s failed", ""), file
);
354 (*id
)->data
.data
= f
;
355 (*id
)->data
.length
= sizeof(*f
);
360 storage_set_flags(krb5_context context
, krb5_storage
*sp
, int vno
)
364 case KRB5_FCC_FVNO_1
:
365 flags
|= KRB5_STORAGE_PRINCIPAL_WRONG_NUM_COMPONENTS
;
366 flags
|= KRB5_STORAGE_PRINCIPAL_NO_NAME_TYPE
;
367 flags
|= KRB5_STORAGE_HOST_BYTEORDER
;
369 case KRB5_FCC_FVNO_2
:
370 flags
|= KRB5_STORAGE_HOST_BYTEORDER
;
372 case KRB5_FCC_FVNO_3
:
373 flags
|= KRB5_STORAGE_KEYBLOCK_KEYTYPE_TWICE
;
375 case KRB5_FCC_FVNO_4
:
379 "storage_set_flags called with bad vno (%x)", vno
);
381 krb5_storage_set_flags(sp
, flags
);
384 static krb5_error_code
385 fcc_open(krb5_context context
,
391 krb5_boolean exclusive
= ((flags
| O_WRONLY
) == flags
||
392 (flags
| O_RDWR
) == flags
);
394 const char *filename
= FILENAME(id
);
396 fd
= open(filename
, flags
, mode
);
400 rk_strerror_r(ret
, buf
, sizeof(buf
));
401 krb5_set_error_message(context
, ret
, N_("open(%s): %s", "file, error"),
407 if((ret
= fcc_lock(context
, id
, fd
, exclusive
)) != 0) {
415 static krb5_error_code
416 fcc_initialize(krb5_context context
,
418 krb5_principal primary_principal
)
420 krb5_fcache
*f
= FCACHE(id
);
423 char *filename
= f
->filename
;
427 ret
= fcc_open(context
, id
, &fd
, O_RDWR
| O_CREAT
| O_EXCL
| O_BINARY
| O_CLOEXEC
, 0600);
432 sp
= krb5_storage_emem();
433 krb5_storage_set_eof_code(sp
, KRB5_CC_END
);
434 if(context
->fcache_vno
!= 0)
435 f
->version
= context
->fcache_vno
;
437 f
->version
= KRB5_FCC_FVNO_4
;
438 ret
|= krb5_store_int8(sp
, 5);
439 ret
|= krb5_store_int8(sp
, f
->version
);
440 storage_set_flags(context
, sp
, f
->version
);
441 if(f
->version
== KRB5_FCC_FVNO_4
&& ret
== 0) {
443 if (context
->kdc_sec_offset
) {
444 ret
|= krb5_store_int16 (sp
, 12); /* length */
445 ret
|= krb5_store_int16 (sp
, FCC_TAG_DELTATIME
); /* Tag */
446 ret
|= krb5_store_int16 (sp
, 8); /* length of data */
447 ret
|= krb5_store_int32 (sp
, context
->kdc_sec_offset
);
448 ret
|= krb5_store_int32 (sp
, context
->kdc_usec_offset
);
450 ret
|= krb5_store_int16 (sp
, 0);
453 ret
|= krb5_store_principal(sp
, primary_principal
);
455 ret
|= write_storage(context
, sp
, fd
);
457 krb5_storage_free(sp
);
459 fcc_unlock(context
, fd
);
464 rk_strerror_r(ret
, buf
, sizeof(buf
));
465 krb5_set_error_message (context
, ret
, N_("close %s: %s", ""),
471 static krb5_error_code
472 fcc_close(krb5_context context
,
476 krb5_data_free(&id
->data
);
480 static krb5_error_code
481 fcc_destroy(krb5_context context
,
484 _krb5_erase_file(context
, FILENAME(id
));
488 static krb5_error_code
489 fcc_store_cred(krb5_context context
,
496 ret
= fcc_open(context
, id
, &fd
, O_WRONLY
| O_APPEND
| O_BINARY
| O_CLOEXEC
, 0);
502 sp
= krb5_storage_emem();
503 krb5_storage_set_eof_code(sp
, KRB5_CC_END
);
504 storage_set_flags(context
, sp
, FCACHE(id
)->version
);
505 if (!krb5_config_get_bool_default(context
, NULL
, TRUE
,
507 "fcc-mit-ticketflags",
509 krb5_storage_set_flags(sp
, KRB5_STORAGE_CREDS_FLAGS_WRONG_BITORDER
);
510 ret
= krb5_store_creds(sp
, creds
);
512 ret
= write_storage(context
, sp
, fd
);
513 krb5_storage_free(sp
);
515 fcc_unlock(context
, fd
);
519 rk_strerror_r(ret
, buf
, sizeof(buf
));
521 krb5_set_error_message (context
, ret
, N_("close %s: %s", ""),
528 static krb5_error_code
529 init_fcc (krb5_context context
,
531 krb5_storage
**ret_sp
,
533 krb5_deltat
*kdc_offset
)
543 ret
= fcc_open(context
, id
, &fd
, O_RDONLY
| O_BINARY
| O_CLOEXEC
, 0);
547 sp
= krb5_storage_from_fd(fd
);
549 krb5_clear_error_message(context
);
553 krb5_storage_set_eof_code(sp
, KRB5_CC_END
);
554 ret
= krb5_ret_int8(sp
, &pvno
);
556 if(ret
== KRB5_CC_END
) {
558 krb5_set_error_message(context
, ret
,
559 N_("Empty credential cache file: %s", ""),
562 krb5_set_error_message(context
, ret
, N_("Error reading pvno "
563 "in cache file: %s", ""),
568 ret
= KRB5_CCACHE_BADVNO
;
569 krb5_set_error_message(context
, ret
, N_("Bad version number in credential "
570 "cache file: %s", ""),
574 ret
= krb5_ret_int8(sp
, &tag
); /* should not be host byte order */
576 ret
= KRB5_CC_FORMAT
;
577 krb5_set_error_message(context
, ret
, "Error reading tag in "
578 "cache file: %s", FILENAME(id
));
581 FCACHE(id
)->version
= tag
;
582 storage_set_flags(context
, sp
, FCACHE(id
)->version
);
584 case KRB5_FCC_FVNO_4
: {
587 ret
= krb5_ret_int16 (sp
, &length
);
589 ret
= KRB5_CC_FORMAT
;
590 krb5_set_error_message(context
, ret
,
591 N_("Error reading tag length in "
592 "cache file: %s", ""), FILENAME(id
));
596 int16_t dtag
, data_len
;
600 ret
= krb5_ret_int16 (sp
, &dtag
);
602 ret
= KRB5_CC_FORMAT
;
603 krb5_set_error_message(context
, ret
, N_("Error reading dtag in "
604 "cache file: %s", ""),
608 ret
= krb5_ret_int16 (sp
, &data_len
);
610 ret
= KRB5_CC_FORMAT
;
611 krb5_set_error_message(context
, ret
,
612 N_("Error reading dlength "
613 "in cache file: %s",""),
618 case FCC_TAG_DELTATIME
: {
621 ret
= krb5_ret_int32 (sp
, &offset
);
622 ret
|= krb5_ret_int32 (sp
, &context
->kdc_usec_offset
);
624 ret
= KRB5_CC_FORMAT
;
625 krb5_set_error_message(context
, ret
,
626 N_("Error reading kdc_sec in "
627 "cache file: %s", ""),
631 context
->kdc_sec_offset
= offset
;
633 *kdc_offset
= offset
;
637 for (i
= 0; i
< data_len
; ++i
) {
638 ret
= krb5_ret_int8 (sp
, &dummy
);
640 ret
= KRB5_CC_FORMAT
;
641 krb5_set_error_message(context
, ret
,
642 N_("Error reading unknown "
643 "tag in cache file: %s", ""),
650 length
-= 4 + data_len
;
654 case KRB5_FCC_FVNO_3
:
655 case KRB5_FCC_FVNO_2
:
656 case KRB5_FCC_FVNO_1
:
659 ret
= KRB5_CCACHE_BADVNO
;
660 krb5_set_error_message(context
, ret
,
661 N_("Unknown version number (%d) in "
662 "credential cache file: %s", ""),
663 (int)tag
, FILENAME(id
));
672 krb5_storage_free(sp
);
673 fcc_unlock(context
, fd
);
678 static krb5_error_code
679 fcc_get_principal(krb5_context context
,
681 krb5_principal
*principal
)
687 ret
= init_fcc (context
, id
, &sp
, &fd
, NULL
);
690 ret
= krb5_ret_principal(sp
, principal
);
692 krb5_clear_error_message(context
);
693 krb5_storage_free(sp
);
694 fcc_unlock(context
, fd
);
699 static krb5_error_code
700 fcc_end_get (krb5_context context
,
702 krb5_cc_cursor
*cursor
);
704 static krb5_error_code
705 fcc_get_first (krb5_context context
,
707 krb5_cc_cursor
*cursor
)
710 krb5_principal principal
;
712 *cursor
= malloc(sizeof(struct fcc_cursor
));
713 if (*cursor
== NULL
) {
714 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
717 memset(*cursor
, 0, sizeof(struct fcc_cursor
));
719 ret
= init_fcc (context
, id
, &FCC_CURSOR(*cursor
)->sp
,
720 &FCC_CURSOR(*cursor
)->fd
, NULL
);
726 ret
= krb5_ret_principal (FCC_CURSOR(*cursor
)->sp
, &principal
);
728 krb5_clear_error_message(context
);
729 fcc_end_get(context
, id
, cursor
);
732 krb5_free_principal (context
, principal
);
733 fcc_unlock(context
, FCC_CURSOR(*cursor
)->fd
);
737 static krb5_error_code
738 fcc_get_next (krb5_context context
,
740 krb5_cc_cursor
*cursor
,
744 if((ret
= fcc_lock(context
, id
, FCC_CURSOR(*cursor
)->fd
, FALSE
)) != 0)
747 ret
= krb5_ret_creds(FCC_CURSOR(*cursor
)->sp
, creds
);
749 krb5_clear_error_message(context
);
751 fcc_unlock(context
, FCC_CURSOR(*cursor
)->fd
);
755 static krb5_error_code
756 fcc_end_get (krb5_context context
,
758 krb5_cc_cursor
*cursor
)
760 krb5_storage_free(FCC_CURSOR(*cursor
)->sp
);
761 close (FCC_CURSOR(*cursor
)->fd
);
767 static krb5_error_code
768 fcc_remove_cred(krb5_context context
,
774 krb5_ccache copy
, newfile
;
778 ret
= krb5_cc_new_unique(context
, krb5_cc_type_memory
, NULL
, ©
);
782 ret
= krb5_cc_copy_cache(context
, id
, copy
);
784 krb5_cc_destroy(context
, copy
);
788 ret
= krb5_cc_remove_cred(context
, copy
, which
, cred
);
790 krb5_cc_destroy(context
, copy
);
794 asprintf(&newname
, "FILE:%s.XXXXXX", FILENAME(id
));
795 if (newname
== NULL
) {
796 krb5_cc_destroy(context
, copy
);
800 fd
= mkstemp(&newname
[5]);
803 krb5_cc_destroy(context
, copy
);
808 ret
= krb5_cc_resolve(context
, newname
, &newfile
);
812 krb5_cc_destroy(context
, copy
);
816 ret
= krb5_cc_copy_cache(context
, copy
, newfile
);
817 krb5_cc_destroy(context
, copy
);
820 krb5_cc_destroy(context
, newfile
);
824 ret
= rename(&newname
[5], FILENAME(id
));
828 krb5_cc_close(context
, newfile
);
833 static krb5_error_code
834 fcc_set_flags(krb5_context context
,
842 fcc_get_version(krb5_context context
,
845 return FCACHE(id
)->version
;
852 static krb5_error_code
853 fcc_get_cache_first(krb5_context context
, krb5_cc_cursor
*cursor
)
855 struct fcache_iter
*iter
;
857 iter
= calloc(1, sizeof(*iter
));
859 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
867 static krb5_error_code
868 fcc_get_cache_next(krb5_context context
, krb5_cc_cursor cursor
, krb5_ccache
*id
)
870 struct fcache_iter
*iter
= cursor
;
873 char *expandedfn
= NULL
;
876 krb5_clear_error_message(context
);
881 fn
= krb5_cc_default_name(context
);
882 if (fn
== NULL
|| strncasecmp(fn
, "FILE:", 5) != 0) {
883 ret
= _krb5_expand_default_cc_name(context
,
884 KRB5_DEFAULT_CCNAME_FILE
,
890 /* check if file exists, don't return a non existant "next" */
891 if (strncasecmp(fn
, "FILE:", 5) == 0) {
893 ret
= stat(fn
+ 5, &sb
);
899 ret
= krb5_cc_resolve(context
, fn
, id
);
907 static krb5_error_code
908 fcc_end_cache_get(krb5_context context
, krb5_cc_cursor cursor
)
910 struct fcache_iter
*iter
= cursor
;
915 static krb5_error_code
916 fcc_move(krb5_context context
, krb5_ccache from
, krb5_ccache to
)
918 krb5_error_code ret
= 0;
920 ret
= rename(FILENAME(from
), FILENAME(to
));
921 #ifdef RENAME_DOES_NOT_UNLINK
922 if (ret
&& (errno
== EEXIST
|| errno
== EACCES
)) {
923 ret
= unlink(FILENAME(to
));
925 ret
= rename(FILENAME(from
), FILENAME(to
));
930 if (ret
&& errno
!= EXDEV
) {
933 rk_strerror_r(ret
, buf
, sizeof(buf
));
934 krb5_set_error_message(context
, ret
,
935 N_("Rename of file from %s "
936 "to %s failed: %s", ""),
937 FILENAME(from
), FILENAME(to
), buf
);
939 } else if (ret
&& errno
== EXDEV
) {
940 /* make a copy and delete the orignal */
941 krb5_ssize_t sz1
, sz2
;
945 ret
= fcc_open(context
, from
, &fd1
, O_RDONLY
| O_BINARY
| O_CLOEXEC
, 0);
949 unlink(FILENAME(to
));
951 ret
= fcc_open(context
, to
, &fd2
,
952 O_WRONLY
| O_CREAT
| O_EXCL
| O_BINARY
| O_CLOEXEC
, 0600);
956 while((sz1
= read(fd1
, buf
, sizeof(buf
))) > 0) {
957 sz2
= write(fd2
, buf
, sz1
);
960 krb5_set_error_message(context
, ret
,
961 N_("Failed to write data from one file "
962 "credential cache to the other", ""));
968 krb5_set_error_message(context
, ret
,
969 N_("Failed to read data from one file "
970 "credential cache to the other", ""));
974 fcc_unlock(context
, fd2
);
978 fcc_unlock(context
, fd1
);
981 _krb5_erase_file(context
, FILENAME(from
));
984 _krb5_erase_file(context
, FILENAME(to
));
989 /* make sure ->version is uptodate */
993 ret
= init_fcc (context
, to
, &sp
, &fd
, NULL
);
995 krb5_storage_free(sp
);
996 fcc_unlock(context
, fd
);
1000 fcc_close(context
, from
);
1005 static krb5_error_code
1006 fcc_get_default_name(krb5_context context
, char **str
)
1008 return _krb5_expand_default_cc_name(context
,
1009 KRB5_DEFAULT_CCNAME_FILE
,
1013 static krb5_error_code
1014 fcc_lastchange(krb5_context context
, krb5_ccache id
, krb5_timestamp
*mtime
)
1016 krb5_error_code ret
;
1020 ret
= fcc_open(context
, id
, &fd
, O_RDONLY
| O_BINARY
| O_CLOEXEC
, 0);
1023 ret
= fstat(fd
, &sb
);
1027 krb5_set_error_message(context
, ret
, N_("Failed to stat cache file", ""));
1030 *mtime
= sb
.st_mtime
;
1034 static krb5_error_code
1035 fcc_set_kdc_offset(krb5_context context
, krb5_ccache id
, krb5_deltat kdc_offset
)
1040 static krb5_error_code
1041 fcc_get_kdc_offset(krb5_context context
, krb5_ccache id
, krb5_deltat
*kdc_offset
)
1043 krb5_error_code ret
;
1046 ret
= init_fcc(context
, id
, &sp
, &fd
, kdc_offset
);
1048 krb5_storage_free(sp
);
1049 fcc_unlock(context
, fd
);
1057 * Variable containing the FILE based credential cache implemention.
1059 * @ingroup krb5_ccache
1062 KRB5_LIB_VARIABLE
const krb5_cc_ops krb5_fcc_ops
= {
1063 KRB5_CC_OPS_VERSION
,
1072 NULL
, /* fcc_retrieve */
1080 fcc_get_cache_first
,
1084 fcc_get_default_name
,