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
;
400 if (FCACHE(id
) == NULL
)
401 return krb5_einval(context
, 2);
403 filename
= FILENAME(id
);
405 if ((flags
& O_CREAT
) == 0) {
406 ret
= lstat(filename
, &sb1
);
408 krb5_set_error_message(context
, ret
, N_("%s lstat(%s)", "file, error"),
409 operation
, filename
);
415 fd
= open(filename
, flags
, mode
);
419 rk_strerror_r(ret
, buf
, sizeof(buf
));
420 krb5_set_error_message(context
, ret
, N_("%s open(%s): %s", "file, error"),
421 operation
, filename
, buf
);
426 if ((flags
& O_CREAT
) == 0) {
428 ret
= fstat(fd
, &sb2
);
430 krb5_clear_error_message(context
);
434 if (!S_ISREG(sb2
.st_mode
)) {
435 krb5_set_error_message(context
, EPERM
, N_("Refuses to open non files caches: FILE:%s", ""), filename
);
440 if (sb1
.st_dev
!= sb2
.st_dev
|| sb1
.st_ino
!= sb2
.st_ino
) {
441 krb5_set_error_message(context
, EPERM
, N_("Refuses to open symlinks for caches FILE:%s", ""), filename
);
445 if (sb2
.st_nlink
!= 1) {
446 krb5_set_error_message(context
, EPERM
, N_("Refuses to open hardlinks for caches FILE:%s", ""), filename
);
450 if ((sb2
.st_mode
& 077) != 0) {
451 krb5_set_error_message(context
, EPERM
,
452 N_("Refuses to open group/other readable files FILE:%s", ""), filename
);
459 if((ret
= fcc_lock(context
, id
, fd
, exclusive
)) != 0) {
467 static krb5_error_code KRB5_CALLCONV
468 fcc_initialize(krb5_context context
,
470 krb5_principal primary_principal
)
472 krb5_fcache
*f
= FCACHE(id
);
477 return krb5_einval(context
, 2);
479 unlink (f
->filename
);
481 ret
= fcc_open(context
, id
, "initialize", &fd
, O_RDWR
| O_CREAT
| O_EXCL
| O_BINARY
| O_CLOEXEC
| O_NOFOLLOW
, 0600);
486 sp
= krb5_storage_emem();
487 krb5_storage_set_eof_code(sp
, KRB5_CC_END
);
488 if(context
->fcache_vno
!= 0)
489 f
->version
= context
->fcache_vno
;
491 f
->version
= KRB5_FCC_FVNO_4
;
492 ret
|= krb5_store_int8(sp
, 5);
493 ret
|= krb5_store_int8(sp
, f
->version
);
494 storage_set_flags(context
, sp
, f
->version
);
495 if(f
->version
== KRB5_FCC_FVNO_4
&& ret
== 0) {
497 if (context
->kdc_sec_offset
) {
498 ret
|= krb5_store_int16 (sp
, 12); /* length */
499 ret
|= krb5_store_int16 (sp
, FCC_TAG_DELTATIME
); /* Tag */
500 ret
|= krb5_store_int16 (sp
, 8); /* length of data */
501 ret
|= krb5_store_int32 (sp
, context
->kdc_sec_offset
);
502 ret
|= krb5_store_int32 (sp
, context
->kdc_usec_offset
);
504 ret
|= krb5_store_int16 (sp
, 0);
507 ret
|= krb5_store_principal(sp
, primary_principal
);
509 ret
|= write_storage(context
, sp
, fd
);
511 krb5_storage_free(sp
);
513 fcc_unlock(context
, fd
);
518 rk_strerror_r(ret
, buf
, sizeof(buf
));
519 krb5_set_error_message (context
, ret
, N_("close %s: %s", ""),
525 static krb5_error_code KRB5_CALLCONV
526 fcc_close(krb5_context context
,
529 if (FCACHE(id
) == NULL
)
530 return krb5_einval(context
, 2);
533 krb5_data_free(&id
->data
);
537 static krb5_error_code KRB5_CALLCONV
538 fcc_destroy(krb5_context context
,
541 if (FCACHE(id
) == NULL
)
542 return krb5_einval(context
, 2);
544 _krb5_erase_file(context
, FILENAME(id
));
548 static krb5_error_code KRB5_CALLCONV
549 fcc_store_cred(krb5_context context
,
556 ret
= fcc_open(context
, id
, "store", &fd
, O_WRONLY
| O_APPEND
| O_BINARY
| O_CLOEXEC
| O_NOFOLLOW
, 0);
562 sp
= krb5_storage_emem();
563 krb5_storage_set_eof_code(sp
, KRB5_CC_END
);
564 storage_set_flags(context
, sp
, FCACHE(id
)->version
);
565 if (!krb5_config_get_bool_default(context
, NULL
, TRUE
,
567 "fcc-mit-ticketflags",
569 krb5_storage_set_flags(sp
, KRB5_STORAGE_CREDS_FLAGS_WRONG_BITORDER
);
570 ret
= krb5_store_creds(sp
, creds
);
572 ret
= write_storage(context
, sp
, fd
);
573 krb5_storage_free(sp
);
575 fcc_unlock(context
, fd
);
579 rk_strerror_r(ret
, buf
, sizeof(buf
));
581 krb5_set_error_message (context
, ret
, N_("close %s: %s", ""),
588 static krb5_error_code
589 init_fcc(krb5_context context
,
591 const char *operation
,
592 krb5_storage
**ret_sp
,
594 krb5_deltat
*kdc_offset
)
604 ret
= fcc_open(context
, id
, operation
, &fd
, O_RDONLY
| O_BINARY
| O_CLOEXEC
| O_NOFOLLOW
, 0);
608 sp
= krb5_storage_from_fd(fd
);
610 krb5_clear_error_message(context
);
614 krb5_storage_set_eof_code(sp
, KRB5_CC_END
);
615 ret
= krb5_ret_int8(sp
, &pvno
);
617 if(ret
== KRB5_CC_END
) {
619 krb5_set_error_message(context
, ret
,
620 N_("Empty credential cache file: %s", ""),
623 krb5_set_error_message(context
, ret
, N_("Error reading pvno "
624 "in cache file: %s", ""),
629 ret
= KRB5_CCACHE_BADVNO
;
630 krb5_set_error_message(context
, ret
, N_("Bad version number in credential "
631 "cache file: %s", ""),
635 ret
= krb5_ret_int8(sp
, &tag
); /* should not be host byte order */
637 ret
= KRB5_CC_FORMAT
;
638 krb5_set_error_message(context
, ret
, "Error reading tag in "
639 "cache file: %s", FILENAME(id
));
642 FCACHE(id
)->version
= tag
;
643 storage_set_flags(context
, sp
, FCACHE(id
)->version
);
645 case KRB5_FCC_FVNO_4
: {
648 ret
= krb5_ret_int16 (sp
, &length
);
650 ret
= KRB5_CC_FORMAT
;
651 krb5_set_error_message(context
, ret
,
652 N_("Error reading tag length in "
653 "cache file: %s", ""), FILENAME(id
));
657 int16_t dtag
, data_len
;
661 ret
= krb5_ret_int16 (sp
, &dtag
);
663 ret
= KRB5_CC_FORMAT
;
664 krb5_set_error_message(context
, ret
, N_("Error reading dtag in "
665 "cache file: %s", ""),
669 ret
= krb5_ret_int16 (sp
, &data_len
);
671 ret
= KRB5_CC_FORMAT
;
672 krb5_set_error_message(context
, ret
,
673 N_("Error reading dlength "
674 "in cache file: %s",""),
679 case FCC_TAG_DELTATIME
: {
682 ret
= krb5_ret_int32 (sp
, &offset
);
683 ret
|= krb5_ret_int32 (sp
, &context
->kdc_usec_offset
);
685 ret
= KRB5_CC_FORMAT
;
686 krb5_set_error_message(context
, ret
,
687 N_("Error reading kdc_sec in "
688 "cache file: %s", ""),
692 context
->kdc_sec_offset
= offset
;
694 *kdc_offset
= offset
;
698 for (i
= 0; i
< data_len
; ++i
) {
699 ret
= krb5_ret_int8 (sp
, &dummy
);
701 ret
= KRB5_CC_FORMAT
;
702 krb5_set_error_message(context
, ret
,
703 N_("Error reading unknown "
704 "tag in cache file: %s", ""),
711 length
-= 4 + data_len
;
715 case KRB5_FCC_FVNO_3
:
716 case KRB5_FCC_FVNO_2
:
717 case KRB5_FCC_FVNO_1
:
720 ret
= KRB5_CCACHE_BADVNO
;
721 krb5_set_error_message(context
, ret
,
722 N_("Unknown version number (%d) in "
723 "credential cache file: %s", ""),
724 (int)tag
, FILENAME(id
));
733 krb5_storage_free(sp
);
734 fcc_unlock(context
, fd
);
739 static krb5_error_code KRB5_CALLCONV
740 fcc_get_principal(krb5_context context
,
742 krb5_principal
*principal
)
748 ret
= init_fcc (context
, id
, "get-pricipal", &sp
, &fd
, NULL
);
751 ret
= krb5_ret_principal(sp
, principal
);
753 krb5_clear_error_message(context
);
754 krb5_storage_free(sp
);
755 fcc_unlock(context
, fd
);
760 static krb5_error_code KRB5_CALLCONV
761 fcc_end_get (krb5_context context
,
763 krb5_cc_cursor
*cursor
);
765 static krb5_error_code KRB5_CALLCONV
766 fcc_get_first (krb5_context context
,
768 krb5_cc_cursor
*cursor
)
771 krb5_principal principal
;
773 if (FCACHE(id
) == NULL
)
774 return krb5_einval(context
, 2);
776 *cursor
= malloc(sizeof(struct fcc_cursor
));
777 if (*cursor
== NULL
) {
778 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
781 memset(*cursor
, 0, sizeof(struct fcc_cursor
));
783 ret
= init_fcc(context
, id
, "get-frist", &FCC_CURSOR(*cursor
)->sp
,
784 &FCC_CURSOR(*cursor
)->fd
, NULL
);
790 ret
= krb5_ret_principal (FCC_CURSOR(*cursor
)->sp
, &principal
);
792 krb5_clear_error_message(context
);
793 fcc_end_get(context
, id
, cursor
);
796 krb5_free_principal (context
, principal
);
797 fcc_unlock(context
, FCC_CURSOR(*cursor
)->fd
);
801 static krb5_error_code KRB5_CALLCONV
802 fcc_get_next (krb5_context context
,
804 krb5_cc_cursor
*cursor
,
809 if (FCACHE(id
) == NULL
)
810 return krb5_einval(context
, 2);
812 if (FCC_CURSOR(*cursor
) == NULL
)
813 return krb5_einval(context
, 3);
815 if((ret
= fcc_lock(context
, id
, FCC_CURSOR(*cursor
)->fd
, FALSE
)) != 0)
818 ret
= krb5_ret_creds(FCC_CURSOR(*cursor
)->sp
, creds
);
820 krb5_clear_error_message(context
);
822 fcc_unlock(context
, FCC_CURSOR(*cursor
)->fd
);
826 static krb5_error_code KRB5_CALLCONV
827 fcc_end_get (krb5_context context
,
829 krb5_cc_cursor
*cursor
)
832 if (FCACHE(id
) == NULL
)
833 return krb5_einval(context
, 2);
835 if (FCC_CURSOR(*cursor
) == NULL
)
836 return krb5_einval(context
, 3);
838 krb5_storage_free(FCC_CURSOR(*cursor
)->sp
);
839 close (FCC_CURSOR(*cursor
)->fd
);
845 static krb5_error_code KRB5_CALLCONV
846 fcc_remove_cred(krb5_context context
,
852 krb5_ccache copy
, newfile
;
853 char *newname
= NULL
;
856 if (FCACHE(id
) == NULL
)
857 return krb5_einval(context
, 2);
859 ret
= krb5_cc_new_unique(context
, krb5_cc_type_memory
, NULL
, ©
);
863 ret
= krb5_cc_copy_cache(context
, id
, copy
);
865 krb5_cc_destroy(context
, copy
);
869 ret
= krb5_cc_remove_cred(context
, copy
, which
, cred
);
871 krb5_cc_destroy(context
, copy
);
875 ret
= asprintf(&newname
, "FILE:%s.XXXXXX", FILENAME(id
));
876 if (ret
< 0 || newname
== NULL
) {
877 krb5_cc_destroy(context
, copy
);
881 fd
= mkstemp(&newname
[5]);
884 krb5_cc_destroy(context
, copy
);
889 ret
= krb5_cc_resolve(context
, newname
, &newfile
);
893 krb5_cc_destroy(context
, copy
);
897 ret
= krb5_cc_copy_cache(context
, copy
, newfile
);
898 krb5_cc_destroy(context
, copy
);
901 krb5_cc_destroy(context
, newfile
);
905 ret
= rk_rename(&newname
[5], FILENAME(id
));
909 krb5_cc_close(context
, newfile
);
914 static krb5_error_code KRB5_CALLCONV
915 fcc_set_flags(krb5_context context
,
919 if (FCACHE(id
) == NULL
)
920 return krb5_einval(context
, 2);
925 static int KRB5_CALLCONV
926 fcc_get_version(krb5_context context
,
929 if (FCACHE(id
) == NULL
)
932 return FCACHE(id
)->version
;
939 static krb5_error_code KRB5_CALLCONV
940 fcc_get_cache_first(krb5_context context
, krb5_cc_cursor
*cursor
)
942 struct fcache_iter
*iter
;
944 iter
= calloc(1, sizeof(*iter
));
946 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
954 static krb5_error_code KRB5_CALLCONV
955 fcc_get_cache_next(krb5_context context
, krb5_cc_cursor cursor
, krb5_ccache
*id
)
957 struct fcache_iter
*iter
= cursor
;
960 char *expandedfn
= NULL
;
963 return krb5_einval(context
, 2);
966 krb5_clear_error_message(context
);
972 * Can't call krb5_cc_default_name here since it refers back to
973 * krb5_cc_cache_match() which will call back into this function.
975 * Just use the default value if its set, otherwise, use the
976 * default hardcoded value.
978 fn
= context
->default_cc_name
;
979 if (fn
== NULL
|| strncasecmp(fn
, "FILE:", 5) != 0) {
980 ret
= _krb5_expand_default_cc_name(context
,
981 KRB5_DEFAULT_CCNAME_FILE
,
987 /* check if file exists, don't return a non existant "next" */
988 if (strncasecmp(fn
, "FILE:", 5) == 0) {
990 ret
= stat(fn
+ 5, &sb
);
996 ret
= krb5_cc_resolve(context
, fn
, id
);
1004 static krb5_error_code KRB5_CALLCONV
1005 fcc_end_cache_get(krb5_context context
, krb5_cc_cursor cursor
)
1007 struct fcache_iter
*iter
= cursor
;
1010 return krb5_einval(context
, 2);
1016 static krb5_error_code KRB5_CALLCONV
1017 fcc_move(krb5_context context
, krb5_ccache from
, krb5_ccache to
)
1019 krb5_error_code ret
= 0;
1021 ret
= rk_rename(FILENAME(from
), FILENAME(to
));
1023 if (ret
&& errno
!= EXDEV
) {
1026 rk_strerror_r(ret
, buf
, sizeof(buf
));
1027 krb5_set_error_message(context
, ret
,
1028 N_("Rename of file from %s "
1029 "to %s failed: %s", ""),
1030 FILENAME(from
), FILENAME(to
), buf
);
1032 } else if (ret
&& errno
== EXDEV
) {
1033 /* make a copy and delete the orignal */
1034 krb5_ssize_t sz1
, sz2
;
1038 ret
= fcc_open(context
, from
, "move/from", &fd1
, O_RDONLY
| O_BINARY
| O_CLOEXEC
| O_NOFOLLOW
, 0);
1042 unlink(FILENAME(to
));
1044 ret
= fcc_open(context
, to
, "move/to", &fd2
,
1045 O_WRONLY
| O_CREAT
| O_EXCL
| O_BINARY
| O_CLOEXEC
| O_NOFOLLOW
, 0600);
1049 while((sz1
= read(fd1
, buf
, sizeof(buf
))) > 0) {
1050 sz2
= write(fd2
, buf
, sz1
);
1053 krb5_set_error_message(context
, ret
,
1054 N_("Failed to write data from one file "
1055 "credential cache to the other", ""));
1061 krb5_set_error_message(context
, ret
,
1062 N_("Failed to read data from one file "
1063 "credential cache to the other", ""));
1067 fcc_unlock(context
, fd2
);
1071 fcc_unlock(context
, fd1
);
1074 _krb5_erase_file(context
, FILENAME(from
));
1077 _krb5_erase_file(context
, FILENAME(to
));
1082 /* make sure ->version is uptodate */
1086 if ((ret
= init_fcc (context
, to
, "move", &sp
, &fd
, NULL
)) == 0) {
1088 krb5_storage_free(sp
);
1089 fcc_unlock(context
, fd
);
1094 fcc_close(context
, from
);
1099 static krb5_error_code KRB5_CALLCONV
1100 fcc_get_default_name(krb5_context context
, char **str
)
1102 return _krb5_expand_default_cc_name(context
,
1103 KRB5_DEFAULT_CCNAME_FILE
,
1107 static krb5_error_code KRB5_CALLCONV
1108 fcc_lastchange(krb5_context context
, krb5_ccache id
, krb5_timestamp
*mtime
)
1110 krb5_error_code ret
;
1114 ret
= fcc_open(context
, id
, "lastchange", &fd
, O_RDONLY
| O_BINARY
| O_CLOEXEC
| O_NOFOLLOW
, 0);
1117 ret
= fstat(fd
, &sb
);
1121 krb5_set_error_message(context
, ret
, N_("Failed to stat cache file", ""));
1124 *mtime
= sb
.st_mtime
;
1128 static krb5_error_code KRB5_CALLCONV
1129 fcc_set_kdc_offset(krb5_context context
, krb5_ccache id
, krb5_deltat kdc_offset
)
1134 static krb5_error_code KRB5_CALLCONV
1135 fcc_get_kdc_offset(krb5_context context
, krb5_ccache id
, krb5_deltat
*kdc_offset
)
1137 krb5_error_code ret
;
1138 krb5_storage
*sp
= NULL
;
1140 ret
= init_fcc(context
, id
, "get-kdc-offset", &sp
, &fd
, kdc_offset
);
1142 krb5_storage_free(sp
);
1143 fcc_unlock(context
, fd
);
1151 * Variable containing the FILE based credential cache implemention.
1153 * @ingroup krb5_ccache
1156 KRB5_LIB_VARIABLE
const krb5_cc_ops krb5_fcc_ops
= {
1157 KRB5_CC_OPS_VERSION
,
1166 NULL
, /* fcc_retrieve */
1174 fcc_get_cache_first
,
1178 fcc_get_default_name
,