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 __RCSID("$Heimdal: fcache.c 22522 2008-01-24 11:56:25Z lha $"
39 typedef struct krb5_fcache
{
49 #define KRB5_FCC_FVNO_1 1
50 #define KRB5_FCC_FVNO_2 2
51 #define KRB5_FCC_FVNO_3 3
52 #define KRB5_FCC_FVNO_4 4
54 #define FCC_TAG_DELTATIME 1
56 #define FCACHE(X) ((krb5_fcache*)(X)->data.data)
58 #define FILENAME(X) (FCACHE(X)->filename)
60 #define FCC_CURSOR(C) ((struct fcc_cursor*)(C))
63 fcc_get_name(krb5_context context
,
70 _krb5_xlock(krb5_context context
, int fd
, krb5_boolean exclusive
,
79 l
.l_type
= exclusive
? F_WRLCK
: F_RDLCK
;
80 l
.l_whence
= SEEK_SET
;
81 ret
= fcntl(fd
, F_SETLKW
, &l
);
83 ret
= flock(fd
, exclusive
? LOCK_EX
: LOCK_SH
);
87 if(ret
== EACCES
) /* fcntl can return EACCES instead of EAGAIN */
93 case EINVAL
: /* filesystem doesn't support locking, let the user have it */
97 krb5_set_error_string(context
, "timed out locking cache file %s",
101 krb5_set_error_string(context
, "error locking cache file %s: %s",
102 filename
, strerror(ret
));
109 _krb5_xunlock(krb5_context context
, int fd
)
117 l
.l_whence
= SEEK_SET
;
118 ret
= fcntl(fd
, F_SETLKW
, &l
);
120 ret
= flock(fd
, LOCK_UN
);
127 case EINVAL
: /* filesystem doesn't support locking, let the user have it */
131 krb5_set_error_string(context
,
132 "Failed to unlock file: %s", strerror(ret
));
138 static krb5_error_code
139 fcc_lock(krb5_context context
, krb5_ccache id
,
140 int fd
, krb5_boolean exclusive
)
142 return _krb5_xlock(context
, fd
, exclusive
, fcc_get_name(context
, id
));
145 static krb5_error_code
146 fcc_unlock(krb5_context context
, int fd
)
148 return _krb5_xunlock(context
, fd
);
151 static krb5_error_code
152 fcc_resolve(krb5_context context
, krb5_ccache
*id
, const char *res
)
155 f
= malloc(sizeof(*f
));
157 krb5_set_error_string(context
, "malloc: out of memory");
158 return KRB5_CC_NOMEM
;
160 f
->filename
= strdup(res
);
161 if(f
->filename
== NULL
){
163 krb5_set_error_string(context
, "malloc: out of memory");
164 return KRB5_CC_NOMEM
;
167 (*id
)->data
.data
= f
;
168 (*id
)->data
.length
= sizeof(*f
);
173 * Try to scrub the contents of `filename' safely.
182 pos
= lseek(fd
, 0, SEEK_END
);
185 if (lseek(fd
, 0, SEEK_SET
) < 0)
187 memset(buf
, 0, sizeof(buf
));
189 ssize_t tmp
= write(fd
, buf
, min(sizeof(buf
), pos
));
200 * Erase `filename' if it exists, trying to remove the contents if
201 * it's `safe'. We always try to remove the file, it it exists. It's
202 * only overwritten if it's a regular file (not a symlink and not a
206 static krb5_error_code
207 erase_file(const char *filename
)
210 struct stat sb1
, sb2
;
213 ret
= lstat (filename
, &sb1
);
217 fd
= open(filename
, O_RDWR
| O_BINARY
);
224 if (unlink(filename
) < 0) {
228 ret
= fstat (fd
, &sb2
);
234 /* check if someone was playing with symlinks */
236 if (sb1
.st_dev
!= sb2
.st_dev
|| sb1
.st_ino
!= sb2
.st_ino
) {
241 /* there are still hard links to this file */
243 if (sb2
.st_nlink
!= 0) {
248 ret
= scrub_file (fd
);
253 static krb5_error_code
254 fcc_gen_new(krb5_context context
, krb5_ccache
*id
)
260 f
= malloc(sizeof(*f
));
262 krb5_set_error_string(context
, "malloc: out of memory");
263 return KRB5_CC_NOMEM
;
265 asprintf (&file
, "%sXXXXXX", KRB5_DEFAULT_CCFILE_ROOT
);
268 krb5_set_error_string(context
, "malloc: out of memory");
269 return KRB5_CC_NOMEM
;
274 krb5_set_error_string(context
, "mkstemp %s", file
);
282 (*id
)->data
.data
= f
;
283 (*id
)->data
.length
= sizeof(*f
);
288 storage_set_flags(krb5_context context
, krb5_storage
*sp
, int vno
)
292 case KRB5_FCC_FVNO_1
:
293 flags
|= KRB5_STORAGE_PRINCIPAL_WRONG_NUM_COMPONENTS
;
294 flags
|= KRB5_STORAGE_PRINCIPAL_NO_NAME_TYPE
;
295 flags
|= KRB5_STORAGE_HOST_BYTEORDER
;
297 case KRB5_FCC_FVNO_2
:
298 flags
|= KRB5_STORAGE_HOST_BYTEORDER
;
300 case KRB5_FCC_FVNO_3
:
301 flags
|= KRB5_STORAGE_KEYBLOCK_KEYTYPE_TWICE
;
303 case KRB5_FCC_FVNO_4
:
307 "storage_set_flags called with bad vno (%x)", vno
);
309 krb5_storage_set_flags(sp
, flags
);
312 static krb5_error_code
313 fcc_open(krb5_context context
,
319 krb5_boolean exclusive
= ((flags
| O_WRONLY
) == flags
||
320 (flags
| O_RDWR
) == flags
);
322 const char *filename
= FILENAME(id
);
324 fd
= open(filename
, flags
, mode
);
327 krb5_set_error_string(context
, "open(%s): %s", filename
,
332 if((ret
= fcc_lock(context
, id
, fd
, exclusive
)) != 0) {
340 static krb5_error_code
341 fcc_initialize(krb5_context context
,
343 krb5_principal primary_principal
)
345 krb5_fcache
*f
= FCACHE(id
);
348 char *filename
= f
->filename
;
352 ret
= fcc_open(context
, id
, &fd
, O_RDWR
| O_CREAT
| O_EXCL
| O_BINARY
, 0600);
357 sp
= krb5_storage_from_fd(fd
);
358 krb5_storage_set_eof_code(sp
, KRB5_CC_END
);
359 if(context
->fcache_vno
!= 0)
360 f
->version
= context
->fcache_vno
;
362 f
->version
= KRB5_FCC_FVNO_4
;
363 ret
|= krb5_store_int8(sp
, 5);
364 ret
|= krb5_store_int8(sp
, f
->version
);
365 storage_set_flags(context
, sp
, f
->version
);
366 if(f
->version
== KRB5_FCC_FVNO_4
&& ret
== 0) {
368 if (context
->kdc_sec_offset
) {
369 ret
|= krb5_store_int16 (sp
, 12); /* length */
370 ret
|= krb5_store_int16 (sp
, FCC_TAG_DELTATIME
); /* Tag */
371 ret
|= krb5_store_int16 (sp
, 8); /* length of data */
372 ret
|= krb5_store_int32 (sp
, context
->kdc_sec_offset
);
373 ret
|= krb5_store_int32 (sp
, context
->kdc_usec_offset
);
375 ret
|= krb5_store_int16 (sp
, 0);
378 ret
|= krb5_store_principal(sp
, primary_principal
);
380 krb5_storage_free(sp
);
382 fcc_unlock(context
, fd
);
386 krb5_set_error_string (context
, "close %s: %s",
387 FILENAME(id
), strerror(ret
));
392 static krb5_error_code
393 fcc_close(krb5_context context
,
397 krb5_data_free(&id
->data
);
401 static krb5_error_code
402 fcc_destroy(krb5_context context
,
405 erase_file(FILENAME(id
));
409 static krb5_error_code
410 fcc_store_cred(krb5_context context
,
417 ret
= fcc_open(context
, id
, &fd
, O_WRONLY
| O_APPEND
| O_BINARY
, 0);
422 sp
= krb5_storage_from_fd(fd
);
423 krb5_storage_set_eof_code(sp
, KRB5_CC_END
);
424 storage_set_flags(context
, sp
, FCACHE(id
)->version
);
425 if (!krb5_config_get_bool_default(context
, NULL
, TRUE
,
427 "fcc-mit-ticketflags",
429 krb5_storage_set_flags(sp
, KRB5_STORAGE_CREDS_FLAGS_WRONG_BITORDER
);
430 ret
= krb5_store_creds(sp
, creds
);
431 krb5_storage_free(sp
);
433 fcc_unlock(context
, fd
);
437 krb5_set_error_string (context
, "close %s: %s",
438 FILENAME(id
), strerror(ret
));
443 static krb5_error_code
444 init_fcc (krb5_context context
,
446 krb5_storage
**ret_sp
,
454 ret
= fcc_open(context
, id
, &fd
, O_RDONLY
| O_BINARY
, 0);
458 sp
= krb5_storage_from_fd(fd
);
460 krb5_clear_error_string(context
);
464 krb5_storage_set_eof_code(sp
, KRB5_CC_END
);
465 ret
= krb5_ret_int8(sp
, &pvno
);
467 if(ret
== KRB5_CC_END
) {
468 krb5_set_error_string(context
, "Empty credential cache file: %s",
472 krb5_set_error_string(context
, "Error reading pvno in "
473 "cache file: %s", FILENAME(id
));
477 krb5_set_error_string(context
, "Bad version number in credential "
478 "cache file: %s", FILENAME(id
));
479 ret
= KRB5_CCACHE_BADVNO
;
482 ret
= krb5_ret_int8(sp
, &tag
); /* should not be host byte order */
484 krb5_set_error_string(context
, "Error reading tag in "
485 "cache file: %s", FILENAME(id
));
486 ret
= KRB5_CC_FORMAT
;
489 FCACHE(id
)->version
= tag
;
490 storage_set_flags(context
, sp
, FCACHE(id
)->version
);
492 case KRB5_FCC_FVNO_4
: {
495 ret
= krb5_ret_int16 (sp
, &length
);
497 ret
= KRB5_CC_FORMAT
;
498 krb5_set_error_string(context
, "Error reading tag length in "
499 "cache file: %s", FILENAME(id
));
503 int16_t dtag
, data_len
;
507 ret
= krb5_ret_int16 (sp
, &dtag
);
509 krb5_set_error_string(context
, "Error reading dtag in "
510 "cache file: %s", FILENAME(id
));
511 ret
= KRB5_CC_FORMAT
;
514 ret
= krb5_ret_int16 (sp
, &data_len
);
516 krb5_set_error_string(context
, "Error reading dlength in "
517 "cache file: %s", FILENAME(id
));
518 ret
= KRB5_CC_FORMAT
;
522 case FCC_TAG_DELTATIME
:
523 ret
= krb5_ret_int32 (sp
, &context
->kdc_sec_offset
);
525 krb5_set_error_string(context
, "Error reading kdc_sec in "
526 "cache file: %s", FILENAME(id
));
527 ret
= KRB5_CC_FORMAT
;
530 ret
= krb5_ret_int32 (sp
, &context
->kdc_usec_offset
);
532 krb5_set_error_string(context
, "Error reading kdc_usec in "
533 "cache file: %s", FILENAME(id
));
534 ret
= KRB5_CC_FORMAT
;
539 for (i
= 0; i
< data_len
; ++i
) {
540 ret
= krb5_ret_int8 (sp
, &dummy
);
542 krb5_set_error_string(context
, "Error reading unknown "
543 "tag in cache file: %s",
545 ret
= KRB5_CC_FORMAT
;
551 length
-= 4 + data_len
;
555 case KRB5_FCC_FVNO_3
:
556 case KRB5_FCC_FVNO_2
:
557 case KRB5_FCC_FVNO_1
:
560 ret
= KRB5_CCACHE_BADVNO
;
561 krb5_set_error_string(context
, "Unknown version number (%d) in "
562 "credential cache file: %s",
563 (int)tag
, FILENAME(id
));
572 krb5_storage_free(sp
);
573 fcc_unlock(context
, fd
);
578 static krb5_error_code
579 fcc_get_principal(krb5_context context
,
581 krb5_principal
*principal
)
587 ret
= init_fcc (context
, id
, &sp
, &fd
);
590 ret
= krb5_ret_principal(sp
, principal
);
592 krb5_clear_error_string(context
);
593 krb5_storage_free(sp
);
594 fcc_unlock(context
, fd
);
599 static krb5_error_code
600 fcc_end_get (krb5_context context
,
602 krb5_cc_cursor
*cursor
);
604 static krb5_error_code
605 fcc_get_first (krb5_context context
,
607 krb5_cc_cursor
*cursor
)
610 krb5_principal principal
;
612 *cursor
= malloc(sizeof(struct fcc_cursor
));
613 if (*cursor
== NULL
) {
614 krb5_set_error_string (context
, "malloc: out of memory");
617 memset(*cursor
, 0, sizeof(struct fcc_cursor
));
619 ret
= init_fcc (context
, id
, &FCC_CURSOR(*cursor
)->sp
,
620 &FCC_CURSOR(*cursor
)->fd
);
626 ret
= krb5_ret_principal (FCC_CURSOR(*cursor
)->sp
, &principal
);
628 krb5_clear_error_string(context
);
629 fcc_end_get(context
, id
, cursor
);
632 krb5_free_principal (context
, principal
);
633 fcc_unlock(context
, FCC_CURSOR(*cursor
)->fd
);
637 static krb5_error_code
638 fcc_get_next (krb5_context context
,
640 krb5_cc_cursor
*cursor
,
644 if((ret
= fcc_lock(context
, id
, FCC_CURSOR(*cursor
)->fd
, FALSE
)) != 0)
647 ret
= krb5_ret_creds(FCC_CURSOR(*cursor
)->sp
, creds
);
649 krb5_clear_error_string(context
);
651 fcc_unlock(context
, FCC_CURSOR(*cursor
)->fd
);
655 static krb5_error_code
656 fcc_end_get (krb5_context context
,
658 krb5_cc_cursor
*cursor
)
660 krb5_storage_free(FCC_CURSOR(*cursor
)->sp
);
661 close (FCC_CURSOR(*cursor
)->fd
);
667 static krb5_error_code
668 fcc_remove_cred(krb5_context context
,
676 ret
= krb5_cc_gen_new(context
, &krb5_mcc_ops
, ©
);
680 ret
= krb5_cc_copy_cache(context
, id
, copy
);
682 krb5_cc_destroy(context
, copy
);
686 ret
= krb5_cc_remove_cred(context
, copy
, which
, cred
);
688 krb5_cc_destroy(context
, copy
);
692 fcc_destroy(context
, id
);
694 ret
= krb5_cc_copy_cache(context
, copy
, id
);
695 krb5_cc_destroy(context
, copy
);
700 static krb5_error_code
701 fcc_set_flags(krb5_context context
,
708 static krb5_error_code
709 fcc_get_version(krb5_context context
,
712 return FCACHE(id
)->version
;
719 static krb5_error_code
720 fcc_get_cache_first(krb5_context context
, krb5_cc_cursor
*cursor
)
722 struct fcache_iter
*iter
;
724 iter
= calloc(1, sizeof(*iter
));
726 krb5_set_error_string(context
, "malloc - out of memory");
734 static krb5_error_code
735 fcc_get_cache_next(krb5_context context
, krb5_cc_cursor cursor
, krb5_ccache
*id
)
737 struct fcache_iter
*iter
= cursor
;
740 char *expandedfn
= NULL
;
743 krb5_clear_error_string(context
);
748 fn
= krb5_cc_default_name(context
);
749 if (strncasecmp(fn
, "FILE:", 5) != 0) {
750 ret
= _krb5_expand_default_cc_name(context
,
751 KRB5_DEFAULT_CCNAME_FILE
,
756 ret
= krb5_cc_resolve(context
, fn
, id
);
763 static krb5_error_code
764 fcc_end_cache_get(krb5_context context
, krb5_cc_cursor cursor
)
766 struct fcache_iter
*iter
= cursor
;
771 static krb5_error_code
772 fcc_move(krb5_context context
, krb5_ccache from
, krb5_ccache to
)
774 krb5_error_code ret
= 0;
776 ret
= rename(FILENAME(from
), FILENAME(to
));
777 if (ret
&& errno
!= EXDEV
) {
779 krb5_set_error_string(context
,
780 "Rename of file from %s to %s failed: %s",
781 FILENAME(from
), FILENAME(to
),
784 } else if (ret
&& errno
== EXDEV
) {
785 /* make a copy and delete the orignal */
786 krb5_ssize_t sz1
, sz2
;
790 ret
= fcc_open(context
, from
, &fd1
, O_RDONLY
| O_BINARY
, 0);
794 unlink(FILENAME(to
));
796 ret
= fcc_open(context
, to
, &fd2
,
797 O_WRONLY
| O_CREAT
| O_EXCL
| O_BINARY
, 0600);
801 while((sz1
= read(fd1
, buf
, sizeof(buf
))) > 0) {
802 sz2
= write(fd2
, buf
, sz1
);
805 krb5_set_error_string(context
,
806 "Failed to write data from one file "
807 "credential cache to the other");
813 krb5_set_error_string(context
,
814 "Failed to read data from one file "
815 "credential cache to the other");
818 erase_file(FILENAME(from
));
821 fcc_unlock(context
, fd2
);
825 fcc_unlock(context
, fd1
);
829 erase_file(FILENAME(to
));
834 /* make sure ->version is uptodate */
838 ret
= init_fcc (context
, to
, &sp
, &fd
);
839 krb5_storage_free(sp
);
840 fcc_unlock(context
, fd
);
846 static krb5_error_code
847 fcc_default_name(krb5_context context
, char **str
)
849 return _krb5_expand_default_cc_name(context
,
850 KRB5_DEFAULT_CCNAME_FILE
,
855 * Variable containing the FILE based credential cache implemention.
857 * @ingroup krb5_ccache
860 const krb5_cc_ops krb5_fcc_ops
= {
869 NULL
, /* fcc_retrieve */