2 * Copyright (c) 1997 - 2004 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("$Id: fcache.c,v 1.54 2006/12/15 21:35:52 lha Exp $");
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_string(context
, "timed out locking cache file %s",
100 krb5_set_error_string(context
, "error locking cache file %s: %s",
101 filename
, strerror(ret
));
108 _krb5_xunlock(krb5_context context
, int fd
)
111 #ifdef HAVE_FCNTL_LOCK
116 l
.l_whence
= SEEK_SET
;
117 ret
= fcntl(fd
, F_SETLKW
, &l
);
119 ret
= flock(fd
, LOCK_UN
);
126 case EINVAL
: /* filesystem doesn't support locking, let the user have it */
130 krb5_set_error_string(context
,
131 "Failed to unlock file: %s", strerror(ret
));
137 static krb5_error_code
138 fcc_lock(krb5_context context
, krb5_ccache id
,
139 int fd
, krb5_boolean exclusive
)
141 return _krb5_xlock(context
, fd
, exclusive
, fcc_get_name(context
, id
));
144 static krb5_error_code
145 fcc_unlock(krb5_context context
, int fd
)
147 return _krb5_xunlock(context
, fd
);
150 static krb5_error_code
151 fcc_resolve(krb5_context context
, krb5_ccache
*id
, const char *res
)
154 f
= malloc(sizeof(*f
));
156 krb5_set_error_string(context
, "malloc: out of memory");
157 return KRB5_CC_NOMEM
;
159 f
->filename
= strdup(res
);
160 if(f
->filename
== NULL
){
162 krb5_set_error_string(context
, "malloc: out of memory");
163 return KRB5_CC_NOMEM
;
166 (*id
)->data
.data
= f
;
167 (*id
)->data
.length
= sizeof(*f
);
172 * Try to scrub the contents of `filename' safely.
181 pos
= lseek(fd
, 0, SEEK_END
);
184 if (lseek(fd
, 0, SEEK_SET
) < 0)
186 memset(buf
, 0, sizeof(buf
));
188 ssize_t tmp
= write(fd
, buf
, min(sizeof(buf
), pos
));
199 * Erase `filename' if it exists, trying to remove the contents if
200 * it's `safe'. We always try to remove the file, it it exists. It's
201 * only overwritten if it's a regular file (not a symlink and not a
205 static krb5_error_code
206 erase_file(const char *filename
)
209 struct stat sb1
, sb2
;
212 ret
= lstat (filename
, &sb1
);
216 fd
= open(filename
, O_RDWR
| O_BINARY
);
223 if (unlink(filename
) < 0) {
227 ret
= fstat (fd
, &sb2
);
233 /* check if someone was playing with symlinks */
235 if (sb1
.st_dev
!= sb2
.st_dev
|| sb1
.st_ino
!= sb2
.st_ino
) {
240 /* there are still hard links to this file */
242 if (sb2
.st_nlink
!= 0) {
247 ret
= scrub_file (fd
);
252 static krb5_error_code
253 fcc_gen_new(krb5_context context
, krb5_ccache
*id
)
259 f
= malloc(sizeof(*f
));
261 krb5_set_error_string(context
, "malloc: out of memory");
262 return KRB5_CC_NOMEM
;
264 asprintf (&file
, "%sXXXXXX", KRB5_DEFAULT_CCFILE_ROOT
);
267 krb5_set_error_string(context
, "malloc: out of memory");
268 return KRB5_CC_NOMEM
;
273 krb5_set_error_string(context
, "mkstemp %s", file
);
281 (*id
)->data
.data
= f
;
282 (*id
)->data
.length
= sizeof(*f
);
287 storage_set_flags(krb5_context context
, krb5_storage
*sp
, int vno
)
291 case KRB5_FCC_FVNO_1
:
292 flags
|= KRB5_STORAGE_PRINCIPAL_WRONG_NUM_COMPONENTS
;
293 flags
|= KRB5_STORAGE_PRINCIPAL_NO_NAME_TYPE
;
294 flags
|= KRB5_STORAGE_HOST_BYTEORDER
;
296 case KRB5_FCC_FVNO_2
:
297 flags
|= KRB5_STORAGE_HOST_BYTEORDER
;
299 case KRB5_FCC_FVNO_3
:
300 flags
|= KRB5_STORAGE_KEYBLOCK_KEYTYPE_TWICE
;
302 case KRB5_FCC_FVNO_4
:
306 "storage_set_flags called with bad vno (%x)", vno
);
308 krb5_storage_set_flags(sp
, flags
);
311 static krb5_error_code
312 fcc_open(krb5_context context
,
318 krb5_boolean exclusive
= ((flags
| O_WRONLY
) == flags
||
319 (flags
| O_RDWR
) == flags
);
321 const char *filename
= FILENAME(id
);
323 fd
= open(filename
, flags
, mode
);
326 krb5_set_error_string(context
, "open(%s): %s", filename
,
331 if((ret
= fcc_lock(context
, id
, fd
, exclusive
)) != 0) {
339 static krb5_error_code
340 fcc_initialize(krb5_context context
,
342 krb5_principal primary_principal
)
344 krb5_fcache
*f
= FCACHE(id
);
347 char *filename
= f
->filename
;
351 ret
= fcc_open(context
, id
, &fd
, O_RDWR
| O_CREAT
| O_EXCL
| O_BINARY
, 0600);
356 sp
= krb5_storage_from_fd(fd
);
357 krb5_storage_set_eof_code(sp
, KRB5_CC_END
);
358 if(context
->fcache_vno
!= 0)
359 f
->version
= context
->fcache_vno
;
361 f
->version
= KRB5_FCC_FVNO_4
;
362 ret
|= krb5_store_int8(sp
, 5);
363 ret
|= krb5_store_int8(sp
, f
->version
);
364 storage_set_flags(context
, sp
, f
->version
);
365 if(f
->version
== KRB5_FCC_FVNO_4
&& ret
== 0) {
367 if (context
->kdc_sec_offset
) {
368 ret
|= krb5_store_int16 (sp
, 12); /* length */
369 ret
|= krb5_store_int16 (sp
, FCC_TAG_DELTATIME
); /* Tag */
370 ret
|= krb5_store_int16 (sp
, 8); /* length of data */
371 ret
|= krb5_store_int32 (sp
, context
->kdc_sec_offset
);
372 ret
|= krb5_store_int32 (sp
, context
->kdc_usec_offset
);
374 ret
|= krb5_store_int16 (sp
, 0);
377 ret
|= krb5_store_principal(sp
, primary_principal
);
379 krb5_storage_free(sp
);
381 fcc_unlock(context
, fd
);
385 krb5_set_error_string (context
, "close %s: %s",
386 FILENAME(id
), strerror(ret
));
391 static krb5_error_code
392 fcc_close(krb5_context context
,
396 krb5_data_free(&id
->data
);
400 static krb5_error_code
401 fcc_destroy(krb5_context context
,
404 erase_file(FILENAME(id
));
408 static krb5_error_code
409 fcc_store_cred(krb5_context context
,
416 ret
= fcc_open(context
, id
, &fd
, O_WRONLY
| O_APPEND
| O_BINARY
, 0);
421 sp
= krb5_storage_from_fd(fd
);
422 krb5_storage_set_eof_code(sp
, KRB5_CC_END
);
423 storage_set_flags(context
, sp
, FCACHE(id
)->version
);
424 if (!krb5_config_get_bool_default(context
, NULL
, TRUE
,
426 "fcc-mit-ticketflags",
428 krb5_storage_set_flags(sp
, KRB5_STORAGE_CREDS_FLAGS_WRONG_BITORDER
);
429 ret
= krb5_store_creds(sp
, creds
);
430 krb5_storage_free(sp
);
432 fcc_unlock(context
, fd
);
436 krb5_set_error_string (context
, "close %s: %s",
437 FILENAME(id
), strerror(ret
));
442 static krb5_error_code
443 init_fcc (krb5_context context
,
445 krb5_storage
**ret_sp
,
453 ret
= fcc_open(context
, id
, &fd
, O_RDONLY
| O_BINARY
, 0);
457 sp
= krb5_storage_from_fd(fd
);
459 krb5_clear_error_string(context
);
463 krb5_storage_set_eof_code(sp
, KRB5_CC_END
);
464 ret
= krb5_ret_int8(sp
, &pvno
);
466 if(ret
== KRB5_CC_END
)
467 ret
= ENOENT
; /* empty file */
468 krb5_clear_error_string(context
);
472 krb5_set_error_string(context
, "Bad version number in credential "
473 "cache file: %s", FILENAME(id
));
474 ret
= KRB5_CCACHE_BADVNO
;
477 ret
= krb5_ret_int8(sp
, &tag
); /* should not be host byte order */
479 krb5_clear_error_string(context
);
480 ret
= KRB5_CC_FORMAT
;
483 FCACHE(id
)->version
= tag
;
484 storage_set_flags(context
, sp
, FCACHE(id
)->version
);
486 case KRB5_FCC_FVNO_4
: {
489 ret
= krb5_ret_int16 (sp
, &length
);
491 ret
= KRB5_CC_FORMAT
;
492 krb5_clear_error_string(context
);
496 int16_t dtag
, data_len
;
500 ret
= krb5_ret_int16 (sp
, &dtag
);
502 krb5_clear_error_string(context
);
503 ret
= KRB5_CC_FORMAT
;
506 ret
= krb5_ret_int16 (sp
, &data_len
);
508 krb5_clear_error_string(context
);
509 ret
= KRB5_CC_FORMAT
;
513 case FCC_TAG_DELTATIME
:
514 ret
= krb5_ret_int32 (sp
, &context
->kdc_sec_offset
);
516 krb5_clear_error_string(context
);
517 ret
= KRB5_CC_FORMAT
;
520 ret
= krb5_ret_int32 (sp
, &context
->kdc_usec_offset
);
522 krb5_clear_error_string(context
);
523 ret
= KRB5_CC_FORMAT
;
528 for (i
= 0; i
< data_len
; ++i
) {
529 ret
= krb5_ret_int8 (sp
, &dummy
);
531 krb5_clear_error_string(context
);
532 ret
= KRB5_CC_FORMAT
;
538 length
-= 4 + data_len
;
542 case KRB5_FCC_FVNO_3
:
543 case KRB5_FCC_FVNO_2
:
544 case KRB5_FCC_FVNO_1
:
547 ret
= KRB5_CCACHE_BADVNO
;
548 krb5_set_error_string(context
, "Unknown version number (%d) in "
549 "credential cache file: %s",
550 (int)tag
, FILENAME(id
));
559 krb5_storage_free(sp
);
560 fcc_unlock(context
, fd
);
565 static krb5_error_code
566 fcc_get_principal(krb5_context context
,
568 krb5_principal
*principal
)
574 ret
= init_fcc (context
, id
, &sp
, &fd
);
577 ret
= krb5_ret_principal(sp
, principal
);
579 krb5_clear_error_string(context
);
580 krb5_storage_free(sp
);
581 fcc_unlock(context
, fd
);
586 static krb5_error_code
587 fcc_end_get (krb5_context context
,
589 krb5_cc_cursor
*cursor
);
591 static krb5_error_code
592 fcc_get_first (krb5_context context
,
594 krb5_cc_cursor
*cursor
)
597 krb5_principal principal
;
599 *cursor
= malloc(sizeof(struct fcc_cursor
));
600 if (*cursor
== NULL
) {
601 krb5_set_error_string (context
, "malloc: out of memory");
604 memset(*cursor
, 0, sizeof(struct fcc_cursor
));
606 ret
= init_fcc (context
, id
, &FCC_CURSOR(*cursor
)->sp
,
607 &FCC_CURSOR(*cursor
)->fd
);
613 ret
= krb5_ret_principal (FCC_CURSOR(*cursor
)->sp
, &principal
);
615 krb5_clear_error_string(context
);
616 fcc_end_get(context
, id
, cursor
);
619 krb5_free_principal (context
, principal
);
620 fcc_unlock(context
, FCC_CURSOR(*cursor
)->fd
);
624 static krb5_error_code
625 fcc_get_next (krb5_context context
,
627 krb5_cc_cursor
*cursor
,
631 if((ret
= fcc_lock(context
, id
, FCC_CURSOR(*cursor
)->fd
, FALSE
)) != 0)
634 ret
= krb5_ret_creds(FCC_CURSOR(*cursor
)->sp
, creds
);
636 krb5_clear_error_string(context
);
638 fcc_unlock(context
, FCC_CURSOR(*cursor
)->fd
);
642 static krb5_error_code
643 fcc_end_get (krb5_context context
,
645 krb5_cc_cursor
*cursor
)
647 krb5_storage_free(FCC_CURSOR(*cursor
)->sp
);
648 close (FCC_CURSOR(*cursor
)->fd
);
654 static krb5_error_code
655 fcc_remove_cred(krb5_context context
,
663 ret
= krb5_cc_gen_new(context
, &krb5_mcc_ops
, ©
);
667 ret
= krb5_cc_copy_cache(context
, id
, copy
);
669 krb5_cc_destroy(context
, copy
);
673 ret
= krb5_cc_remove_cred(context
, copy
, which
, cred
);
675 krb5_cc_destroy(context
, copy
);
679 fcc_destroy(context
, id
);
681 ret
= krb5_cc_copy_cache(context
, copy
, id
);
682 krb5_cc_destroy(context
, copy
);
687 static krb5_error_code
688 fcc_set_flags(krb5_context context
,
695 static krb5_error_code
696 fcc_get_version(krb5_context context
,
699 return FCACHE(id
)->version
;
706 static krb5_error_code
707 fcc_get_cache_first(krb5_context context
, krb5_cc_cursor
*cursor
)
709 struct fcache_iter
*iter
;
711 iter
= calloc(1, sizeof(*iter
));
713 krb5_set_error_string(context
, "malloc - out of memory");
721 static krb5_error_code
722 fcc_get_cache_next(krb5_context context
, krb5_cc_cursor cursor
, krb5_ccache
*id
)
724 struct fcache_iter
*iter
= cursor
;
727 char *expandedfn
= NULL
;
730 krb5_clear_error_string(context
);
735 fn
= krb5_cc_default_name(context
);
736 if (strncasecmp(fn
, "FILE:", 5) != 0) {
737 ret
= _krb5_expand_default_cc_name(context
,
738 KRB5_DEFAULT_CCNAME_FILE
,
743 ret
= krb5_cc_resolve(context
, fn
, id
);
750 static krb5_error_code
751 fcc_end_cache_get(krb5_context context
, krb5_cc_cursor cursor
)
753 struct fcache_iter
*iter
= cursor
;
758 const krb5_cc_ops krb5_fcc_ops
= {
767 NULL
, /* fcc_retrieve */