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))
61 static const char* KRB5_CALLCONV
62 fcc_get_name(krb5_context context
,
65 if (FCACHE(id
) == NULL
)
71 KRB5_LIB_FUNCTION
int KRB5_LIB_CALL
72 _krb5_xlock(krb5_context context
, int fd
, krb5_boolean exclusive
,
81 l
.l_type
= exclusive
? F_WRLCK
: F_RDLCK
;
82 l
.l_whence
= SEEK_SET
;
83 ret
= fcntl(fd
, F_SETLKW
, &l
);
85 ret
= flock(fd
, exclusive
? LOCK_EX
: LOCK_SH
);
89 if(ret
== EACCES
) /* fcntl can return EACCES instead of EAGAIN */
95 case EINVAL
: /* filesystem doesn't support locking, let the user have it */
99 krb5_set_error_message(context
, ret
,
100 N_("timed out locking cache file %s", "file"),
105 rk_strerror_r(ret
, buf
, sizeof(buf
));
106 krb5_set_error_message(context
, ret
,
107 N_("error locking cache file %s: %s",
108 "file, error"), filename
, buf
);
115 KRB5_LIB_FUNCTION
int KRB5_LIB_CALL
116 _krb5_xunlock(krb5_context context
, int fd
)
124 l
.l_whence
= SEEK_SET
;
125 ret
= fcntl(fd
, F_SETLKW
, &l
);
127 ret
= flock(fd
, LOCK_UN
);
134 case EINVAL
: /* filesystem doesn't support locking, let the user have it */
139 rk_strerror_r(ret
, buf
, sizeof(buf
));
140 krb5_set_error_message(context
, ret
,
141 N_("Failed to unlock file: %s", ""), buf
);
148 static krb5_error_code
149 write_storage(krb5_context context
, krb5_storage
*sp
, int fd
)
155 ret
= krb5_storage_to_data(sp
, &data
);
157 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
160 sret
= write(fd
, data
.data
, data
.length
);
161 ret
= (sret
!= (ssize_t
)data
.length
);
162 krb5_data_free(&data
);
165 krb5_set_error_message(context
, ret
,
166 N_("Failed to write FILE credential data", ""));
173 static krb5_error_code KRB5_CALLCONV
174 fcc_lock(krb5_context context
, krb5_ccache id
,
175 int fd
, krb5_boolean exclusive
)
177 return _krb5_xlock(context
, fd
, exclusive
, fcc_get_name(context
, id
));
180 static krb5_error_code KRB5_CALLCONV
181 fcc_unlock(krb5_context context
, int fd
)
183 return _krb5_xunlock(context
, fd
);
186 static krb5_error_code KRB5_CALLCONV
187 fcc_resolve(krb5_context context
, krb5_ccache
*id
, const char *res
)
190 f
= malloc(sizeof(*f
));
192 krb5_set_error_message(context
, KRB5_CC_NOMEM
,
193 N_("malloc: out of memory", ""));
194 return KRB5_CC_NOMEM
;
196 f
->filename
= strdup(res
);
197 if(f
->filename
== NULL
){
199 krb5_set_error_message(context
, KRB5_CC_NOMEM
,
200 N_("malloc: out of memory", ""));
201 return KRB5_CC_NOMEM
;
204 (*id
)->data
.data
= f
;
205 (*id
)->data
.length
= sizeof(*f
);
210 * Try to scrub the contents of `filename' safely.
219 pos
= lseek(fd
, 0, SEEK_END
);
222 if (lseek(fd
, 0, SEEK_SET
) < 0)
224 memset(buf
, 0, sizeof(buf
));
227 size_t wr
= sizeof(buf
);
230 tmp
= write(fd
, buf
, wr
);
245 * Erase `filename' if it exists, trying to remove the contents if
246 * it's `safe'. We always try to remove the file, it it exists. It's
247 * only overwritten if it's a regular file (not a symlink and not a
251 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
252 _krb5_erase_file(krb5_context context
, const char *filename
)
255 struct stat sb1
, sb2
;
258 ret
= lstat (filename
, &sb1
);
262 fd
= open(filename
, O_RDWR
| O_BINARY
);
270 ret
= _krb5_xlock(context
, fd
, 1, filename
);
275 if (unlink(filename
) < 0) {
276 _krb5_xunlock(context
, fd
);
280 ret
= fstat(fd
, &sb2
);
282 _krb5_xunlock(context
, fd
);
287 /* check if someone was playing with symlinks */
289 if (sb1
.st_dev
!= sb2
.st_dev
|| sb1
.st_ino
!= sb2
.st_ino
) {
290 _krb5_xunlock(context
, fd
);
295 /* there are still hard links to this file */
297 if (sb2
.st_nlink
!= 0) {
298 _krb5_xunlock(context
, fd
);
303 ret
= scrub_file(fd
);
305 _krb5_xunlock(context
, fd
);
309 ret
= _krb5_xunlock(context
, fd
);
314 static krb5_error_code KRB5_CALLCONV
315 fcc_gen_new(krb5_context context
, krb5_ccache
*id
)
317 char *file
= NULL
, *exp_file
= NULL
;
322 f
= malloc(sizeof(*f
));
324 krb5_set_error_message(context
, KRB5_CC_NOMEM
,
325 N_("malloc: out of memory", ""));
326 return KRB5_CC_NOMEM
;
328 ret
= asprintf(&file
, "%sXXXXXX", KRB5_DEFAULT_CCFILE_ROOT
);
329 if(ret
< 0 || file
== NULL
) {
331 krb5_set_error_message(context
, KRB5_CC_NOMEM
,
332 N_("malloc: out of memory", ""));
333 return KRB5_CC_NOMEM
;
335 ret
= _krb5_expand_path_tokens(context
, file
, &exp_file
);
344 fd
= mkstemp(exp_file
);
346 ret
= (krb5_error_code
)errno
;
347 krb5_set_error_message(context
, ret
, N_("mkstemp %s failed", ""), exp_file
);
353 f
->filename
= exp_file
;
355 (*id
)->data
.data
= f
;
356 (*id
)->data
.length
= sizeof(*f
);
361 storage_set_flags(krb5_context context
, krb5_storage
*sp
, int vno
)
365 case KRB5_FCC_FVNO_1
:
366 flags
|= KRB5_STORAGE_PRINCIPAL_WRONG_NUM_COMPONENTS
;
367 flags
|= KRB5_STORAGE_PRINCIPAL_NO_NAME_TYPE
;
368 flags
|= KRB5_STORAGE_HOST_BYTEORDER
;
370 case KRB5_FCC_FVNO_2
:
371 flags
|= KRB5_STORAGE_HOST_BYTEORDER
;
373 case KRB5_FCC_FVNO_3
:
374 flags
|= KRB5_STORAGE_KEYBLOCK_KEYTYPE_TWICE
;
376 case KRB5_FCC_FVNO_4
:
380 "storage_set_flags called with bad vno (%x)", vno
);
382 krb5_storage_set_flags(sp
, flags
);
385 static krb5_error_code KRB5_CALLCONV
386 fcc_open(krb5_context context
,
388 const char *operation
,
393 krb5_boolean exclusive
= ((flags
| O_WRONLY
) == flags
||
394 (flags
| O_RDWR
) == flags
);
396 const char *filename
;
397 struct stat sb1
, sb2
;
398 int strict_checking
;;
401 if (FCACHE(id
) == NULL
)
402 return krb5_einval(context
, 2);
404 filename
= FILENAME(id
);
406 strict_checking
= (flags
& O_CREAT
) == 0 &&
407 (context
->flags
& KRB5_CTX_F_FCACHE_STRICT_CHECKING
) != 0;
409 if (strict_checking
) {
410 ret
= lstat(filename
, &sb1
);
412 krb5_set_error_message(context
, ret
, N_("%s lstat(%s)", "file, error"),
413 operation
, filename
);
419 fd
= open(filename
, flags
, mode
);
423 rk_strerror_r(ret
, buf
, sizeof(buf
));
424 krb5_set_error_message(context
, ret
, N_("%s open(%s): %s", "file, error"),
425 operation
, filename
, buf
);
430 if (strict_checking
) {
432 ret
= fstat(fd
, &sb2
);
434 krb5_clear_error_message(context
);
438 if (!S_ISREG(sb2
.st_mode
)) {
439 krb5_set_error_message(context
, EPERM
, N_("Refuses to open non files caches: FILE:%s", ""), filename
);
444 if (sb1
.st_dev
!= sb2
.st_dev
|| sb1
.st_ino
!= sb2
.st_ino
) {
445 krb5_set_error_message(context
, EPERM
, N_("Refuses to open symlinks for caches FILE:%s", ""), filename
);
449 if (sb2
.st_nlink
!= 1) {
450 krb5_set_error_message(context
, EPERM
, N_("Refuses to open hardlinks for caches FILE:%s", ""), filename
);
454 if ((sb2
.st_mode
& 077) != 0) {
455 krb5_set_error_message(context
, EPERM
,
456 N_("Refuses to open group/other readable files FILE:%s", ""), filename
);
463 if((ret
= fcc_lock(context
, id
, fd
, exclusive
)) != 0) {
471 static krb5_error_code KRB5_CALLCONV
472 fcc_initialize(krb5_context context
,
474 krb5_principal primary_principal
)
476 krb5_fcache
*f
= FCACHE(id
);
481 return krb5_einval(context
, 2);
483 unlink (f
->filename
);
485 ret
= fcc_open(context
, id
, "initialize", &fd
, O_RDWR
| O_CREAT
| O_EXCL
| O_BINARY
| O_CLOEXEC
| O_NOFOLLOW
, 0600);
490 sp
= krb5_storage_emem();
491 krb5_storage_set_eof_code(sp
, KRB5_CC_END
);
492 if(context
->fcache_vno
!= 0)
493 f
->version
= context
->fcache_vno
;
495 f
->version
= KRB5_FCC_FVNO_4
;
496 ret
|= krb5_store_int8(sp
, 5);
497 ret
|= krb5_store_int8(sp
, f
->version
);
498 storage_set_flags(context
, sp
, f
->version
);
499 if(f
->version
== KRB5_FCC_FVNO_4
&& ret
== 0) {
501 if (context
->kdc_sec_offset
) {
502 ret
|= krb5_store_int16 (sp
, 12); /* length */
503 ret
|= krb5_store_int16 (sp
, FCC_TAG_DELTATIME
); /* Tag */
504 ret
|= krb5_store_int16 (sp
, 8); /* length of data */
505 ret
|= krb5_store_int32 (sp
, context
->kdc_sec_offset
);
506 ret
|= krb5_store_int32 (sp
, context
->kdc_usec_offset
);
508 ret
|= krb5_store_int16 (sp
, 0);
511 ret
|= krb5_store_principal(sp
, primary_principal
);
513 ret
|= write_storage(context
, sp
, fd
);
515 krb5_storage_free(sp
);
517 fcc_unlock(context
, fd
);
522 rk_strerror_r(ret
, buf
, sizeof(buf
));
523 krb5_set_error_message (context
, ret
, N_("close %s: %s", ""),
529 static krb5_error_code KRB5_CALLCONV
530 fcc_close(krb5_context context
,
533 if (FCACHE(id
) == NULL
)
534 return krb5_einval(context
, 2);
537 krb5_data_free(&id
->data
);
541 static krb5_error_code KRB5_CALLCONV
542 fcc_destroy(krb5_context context
,
545 if (FCACHE(id
) == NULL
)
546 return krb5_einval(context
, 2);
548 _krb5_erase_file(context
, FILENAME(id
));
552 static krb5_error_code KRB5_CALLCONV
553 fcc_store_cred(krb5_context context
,
560 ret
= fcc_open(context
, id
, "store", &fd
, O_WRONLY
| O_APPEND
| O_BINARY
| O_CLOEXEC
| O_NOFOLLOW
, 0);
566 sp
= krb5_storage_emem();
567 krb5_storage_set_eof_code(sp
, KRB5_CC_END
);
568 storage_set_flags(context
, sp
, FCACHE(id
)->version
);
569 if (!krb5_config_get_bool_default(context
, NULL
, TRUE
,
571 "fcc-mit-ticketflags",
573 krb5_storage_set_flags(sp
, KRB5_STORAGE_CREDS_FLAGS_WRONG_BITORDER
);
574 ret
= krb5_store_creds(sp
, creds
);
576 ret
= write_storage(context
, sp
, fd
);
577 krb5_storage_free(sp
);
579 fcc_unlock(context
, fd
);
583 rk_strerror_r(ret
, buf
, sizeof(buf
));
585 krb5_set_error_message (context
, ret
, N_("close %s: %s", ""),
592 static krb5_error_code
593 init_fcc(krb5_context context
,
595 const char *operation
,
596 krb5_storage
**ret_sp
,
598 krb5_deltat
*kdc_offset
)
608 ret
= fcc_open(context
, id
, operation
, &fd
, O_RDONLY
| O_BINARY
| O_CLOEXEC
| O_NOFOLLOW
, 0);
612 sp
= krb5_storage_from_fd(fd
);
614 krb5_clear_error_message(context
);
618 krb5_storage_set_eof_code(sp
, KRB5_CC_END
);
619 ret
= krb5_ret_int8(sp
, &pvno
);
621 if(ret
== KRB5_CC_END
) {
623 krb5_set_error_message(context
, ret
,
624 N_("Empty credential cache file: %s", ""),
627 krb5_set_error_message(context
, ret
, N_("Error reading pvno "
628 "in cache file: %s", ""),
633 ret
= KRB5_CCACHE_BADVNO
;
634 krb5_set_error_message(context
, ret
, N_("Bad version number in credential "
635 "cache file: %s", ""),
639 ret
= krb5_ret_int8(sp
, &tag
); /* should not be host byte order */
641 ret
= KRB5_CC_FORMAT
;
642 krb5_set_error_message(context
, ret
, "Error reading tag in "
643 "cache file: %s", FILENAME(id
));
646 FCACHE(id
)->version
= tag
;
647 storage_set_flags(context
, sp
, FCACHE(id
)->version
);
649 case KRB5_FCC_FVNO_4
: {
652 ret
= krb5_ret_int16 (sp
, &length
);
654 ret
= KRB5_CC_FORMAT
;
655 krb5_set_error_message(context
, ret
,
656 N_("Error reading tag length in "
657 "cache file: %s", ""), FILENAME(id
));
661 int16_t dtag
, data_len
;
665 ret
= krb5_ret_int16 (sp
, &dtag
);
667 ret
= KRB5_CC_FORMAT
;
668 krb5_set_error_message(context
, ret
, N_("Error reading dtag in "
669 "cache file: %s", ""),
673 ret
= krb5_ret_int16 (sp
, &data_len
);
675 ret
= KRB5_CC_FORMAT
;
676 krb5_set_error_message(context
, ret
,
677 N_("Error reading dlength "
678 "in cache file: %s",""),
683 case FCC_TAG_DELTATIME
: {
686 ret
= krb5_ret_int32 (sp
, &offset
);
687 ret
|= krb5_ret_int32 (sp
, &context
->kdc_usec_offset
);
689 ret
= KRB5_CC_FORMAT
;
690 krb5_set_error_message(context
, ret
,
691 N_("Error reading kdc_sec in "
692 "cache file: %s", ""),
696 context
->kdc_sec_offset
= offset
;
698 *kdc_offset
= offset
;
702 for (i
= 0; i
< data_len
; ++i
) {
703 ret
= krb5_ret_int8 (sp
, &dummy
);
705 ret
= KRB5_CC_FORMAT
;
706 krb5_set_error_message(context
, ret
,
707 N_("Error reading unknown "
708 "tag in cache file: %s", ""),
715 length
-= 4 + data_len
;
719 case KRB5_FCC_FVNO_3
:
720 case KRB5_FCC_FVNO_2
:
721 case KRB5_FCC_FVNO_1
:
724 ret
= KRB5_CCACHE_BADVNO
;
725 krb5_set_error_message(context
, ret
,
726 N_("Unknown version number (%d) in "
727 "credential cache file: %s", ""),
728 (int)tag
, FILENAME(id
));
737 krb5_storage_free(sp
);
738 fcc_unlock(context
, fd
);
743 static krb5_error_code KRB5_CALLCONV
744 fcc_get_principal(krb5_context context
,
746 krb5_principal
*principal
)
752 ret
= init_fcc (context
, id
, "get-pricipal", &sp
, &fd
, NULL
);
755 ret
= krb5_ret_principal(sp
, principal
);
757 krb5_clear_error_message(context
);
758 krb5_storage_free(sp
);
759 fcc_unlock(context
, fd
);
764 static krb5_error_code KRB5_CALLCONV
765 fcc_end_get (krb5_context context
,
767 krb5_cc_cursor
*cursor
);
769 static krb5_error_code KRB5_CALLCONV
770 fcc_get_first (krb5_context context
,
772 krb5_cc_cursor
*cursor
)
775 krb5_principal principal
;
777 if (FCACHE(id
) == NULL
)
778 return krb5_einval(context
, 2);
780 *cursor
= malloc(sizeof(struct fcc_cursor
));
781 if (*cursor
== NULL
) {
782 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
785 memset(*cursor
, 0, sizeof(struct fcc_cursor
));
787 ret
= init_fcc(context
, id
, "get-frist", &FCC_CURSOR(*cursor
)->sp
,
788 &FCC_CURSOR(*cursor
)->fd
, NULL
);
794 ret
= krb5_ret_principal (FCC_CURSOR(*cursor
)->sp
, &principal
);
796 krb5_clear_error_message(context
);
797 fcc_end_get(context
, id
, cursor
);
800 krb5_free_principal (context
, principal
);
801 fcc_unlock(context
, FCC_CURSOR(*cursor
)->fd
);
805 static krb5_error_code KRB5_CALLCONV
806 fcc_get_next (krb5_context context
,
808 krb5_cc_cursor
*cursor
,
813 if (FCACHE(id
) == NULL
)
814 return krb5_einval(context
, 2);
816 if (FCC_CURSOR(*cursor
) == NULL
)
817 return krb5_einval(context
, 3);
819 if((ret
= fcc_lock(context
, id
, FCC_CURSOR(*cursor
)->fd
, FALSE
)) != 0)
822 ret
= krb5_ret_creds(FCC_CURSOR(*cursor
)->sp
, creds
);
824 krb5_clear_error_message(context
);
826 fcc_unlock(context
, FCC_CURSOR(*cursor
)->fd
);
830 static krb5_error_code KRB5_CALLCONV
831 fcc_end_get (krb5_context context
,
833 krb5_cc_cursor
*cursor
)
836 if (FCACHE(id
) == NULL
)
837 return krb5_einval(context
, 2);
839 if (FCC_CURSOR(*cursor
) == NULL
)
840 return krb5_einval(context
, 3);
842 krb5_storage_free(FCC_CURSOR(*cursor
)->sp
);
843 close (FCC_CURSOR(*cursor
)->fd
);
849 static krb5_error_code KRB5_CALLCONV
850 fcc_remove_cred(krb5_context context
,
856 krb5_ccache copy
, newfile
;
857 char *newname
= NULL
;
860 if (FCACHE(id
) == NULL
)
861 return krb5_einval(context
, 2);
863 ret
= krb5_cc_new_unique(context
, krb5_cc_type_memory
, NULL
, ©
);
867 ret
= krb5_cc_copy_cache(context
, id
, copy
);
869 krb5_cc_destroy(context
, copy
);
873 ret
= krb5_cc_remove_cred(context
, copy
, which
, cred
);
875 krb5_cc_destroy(context
, copy
);
879 ret
= asprintf(&newname
, "FILE:%s.XXXXXX", FILENAME(id
));
880 if (ret
< 0 || newname
== NULL
) {
881 krb5_cc_destroy(context
, copy
);
885 fd
= mkstemp(&newname
[5]);
888 krb5_cc_destroy(context
, copy
);
893 ret
= krb5_cc_resolve(context
, newname
, &newfile
);
897 krb5_cc_destroy(context
, copy
);
901 ret
= krb5_cc_copy_cache(context
, copy
, newfile
);
902 krb5_cc_destroy(context
, copy
);
905 krb5_cc_destroy(context
, newfile
);
909 ret
= rk_rename(&newname
[5], FILENAME(id
));
913 krb5_cc_close(context
, newfile
);
918 static krb5_error_code KRB5_CALLCONV
919 fcc_set_flags(krb5_context context
,
923 if (FCACHE(id
) == NULL
)
924 return krb5_einval(context
, 2);
929 static int KRB5_CALLCONV
930 fcc_get_version(krb5_context context
,
933 if (FCACHE(id
) == NULL
)
936 return FCACHE(id
)->version
;
943 static krb5_error_code KRB5_CALLCONV
944 fcc_get_cache_first(krb5_context context
, krb5_cc_cursor
*cursor
)
946 struct fcache_iter
*iter
;
948 iter
= calloc(1, sizeof(*iter
));
950 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
958 static krb5_error_code KRB5_CALLCONV
959 fcc_get_cache_next(krb5_context context
, krb5_cc_cursor cursor
, krb5_ccache
*id
)
961 struct fcache_iter
*iter
= cursor
;
964 char *expandedfn
= NULL
;
967 return krb5_einval(context
, 2);
970 krb5_clear_error_message(context
);
976 * Can't call krb5_cc_default_name here since it refers back to
977 * krb5_cc_cache_match() which will call back into this function.
979 * Just use the default value if its set, otherwise, use the
980 * default hardcoded value.
982 fn
= context
->default_cc_name
;
983 if (fn
== NULL
|| strncasecmp(fn
, "FILE:", 5) != 0) {
984 ret
= _krb5_expand_default_cc_name(context
,
985 KRB5_DEFAULT_CCNAME_FILE
,
991 /* check if file exists, don't return a non existant "next" */
992 if (strncasecmp(fn
, "FILE:", 5) == 0) {
994 ret
= stat(fn
+ 5, &sb
);
1000 ret
= krb5_cc_resolve(context
, fn
, id
);
1008 static krb5_error_code KRB5_CALLCONV
1009 fcc_end_cache_get(krb5_context context
, krb5_cc_cursor cursor
)
1011 struct fcache_iter
*iter
= cursor
;
1014 return krb5_einval(context
, 2);
1020 static krb5_error_code KRB5_CALLCONV
1021 fcc_move(krb5_context context
, krb5_ccache from
, krb5_ccache to
)
1023 krb5_error_code ret
= 0;
1025 ret
= rk_rename(FILENAME(from
), FILENAME(to
));
1027 if (ret
&& errno
!= EXDEV
) {
1030 rk_strerror_r(ret
, buf
, sizeof(buf
));
1031 krb5_set_error_message(context
, ret
,
1032 N_("Rename of file from %s "
1033 "to %s failed: %s", ""),
1034 FILENAME(from
), FILENAME(to
), buf
);
1036 } else if (ret
&& errno
== EXDEV
) {
1037 /* make a copy and delete the orignal */
1038 krb5_ssize_t sz1
, sz2
;
1042 ret
= fcc_open(context
, from
, "move/from", &fd1
, O_RDONLY
| O_BINARY
| O_CLOEXEC
| O_NOFOLLOW
, 0);
1046 unlink(FILENAME(to
));
1048 ret
= fcc_open(context
, to
, "move/to", &fd2
,
1049 O_WRONLY
| O_CREAT
| O_EXCL
| O_BINARY
| O_CLOEXEC
| O_NOFOLLOW
, 0600);
1053 while((sz1
= read(fd1
, buf
, sizeof(buf
))) > 0) {
1054 sz2
= write(fd2
, buf
, sz1
);
1057 krb5_set_error_message(context
, ret
,
1058 N_("Failed to write data from one file "
1059 "credential cache to the other", ""));
1065 krb5_set_error_message(context
, ret
,
1066 N_("Failed to read data from one file "
1067 "credential cache to the other", ""));
1071 fcc_unlock(context
, fd2
);
1075 fcc_unlock(context
, fd1
);
1078 _krb5_erase_file(context
, FILENAME(from
));
1081 _krb5_erase_file(context
, FILENAME(to
));
1086 /* make sure ->version is uptodate */
1090 if ((ret
= init_fcc (context
, to
, "move", &sp
, &fd
, NULL
)) == 0) {
1092 krb5_storage_free(sp
);
1093 fcc_unlock(context
, fd
);
1098 fcc_close(context
, from
);
1103 static krb5_error_code KRB5_CALLCONV
1104 fcc_get_default_name(krb5_context context
, char **str
)
1106 return _krb5_expand_default_cc_name(context
,
1107 KRB5_DEFAULT_CCNAME_FILE
,
1111 static krb5_error_code KRB5_CALLCONV
1112 fcc_lastchange(krb5_context context
, krb5_ccache id
, krb5_timestamp
*mtime
)
1114 krb5_error_code ret
;
1118 ret
= fcc_open(context
, id
, "lastchange", &fd
, O_RDONLY
| O_BINARY
| O_CLOEXEC
| O_NOFOLLOW
, 0);
1121 ret
= fstat(fd
, &sb
);
1125 krb5_set_error_message(context
, ret
, N_("Failed to stat cache file", ""));
1128 *mtime
= sb
.st_mtime
;
1132 static krb5_error_code KRB5_CALLCONV
1133 fcc_set_kdc_offset(krb5_context context
, krb5_ccache id
, krb5_deltat kdc_offset
)
1138 static krb5_error_code KRB5_CALLCONV
1139 fcc_get_kdc_offset(krb5_context context
, krb5_ccache id
, krb5_deltat
*kdc_offset
)
1141 krb5_error_code ret
;
1142 krb5_storage
*sp
= NULL
;
1144 ret
= init_fcc(context
, id
, "get-kdc-offset", &sp
, &fd
, kdc_offset
);
1146 krb5_storage_free(sp
);
1147 fcc_unlock(context
, fd
);
1155 * Variable containing the FILE based credential cache implemention.
1157 * @ingroup krb5_ccache
1160 KRB5_LIB_VARIABLE
const krb5_cc_ops krb5_fcc_ops
= {
1161 KRB5_CC_OPS_VERSION
,
1170 NULL
, /* fcc_retrieve */
1178 fcc_get_cache_first
,
1182 fcc_get_default_name
,