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.51 2005/08/12 13:31:19 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
;
274 krb5_set_error_string(context
, "mkstemp %s", file
);
280 (*id
)->data
.data
= f
;
281 (*id
)->data
.length
= sizeof(*f
);
286 storage_set_flags(krb5_context context
, krb5_storage
*sp
, int vno
)
290 case KRB5_FCC_FVNO_1
:
291 flags
|= KRB5_STORAGE_PRINCIPAL_WRONG_NUM_COMPONENTS
;
292 flags
|= KRB5_STORAGE_PRINCIPAL_NO_NAME_TYPE
;
293 flags
|= KRB5_STORAGE_HOST_BYTEORDER
;
295 case KRB5_FCC_FVNO_2
:
296 flags
|= KRB5_STORAGE_HOST_BYTEORDER
;
298 case KRB5_FCC_FVNO_3
:
299 flags
|= KRB5_STORAGE_KEYBLOCK_KEYTYPE_TWICE
;
301 case KRB5_FCC_FVNO_4
:
305 "storage_set_flags called with bad vno (%x)", vno
);
307 krb5_storage_set_flags(sp
, flags
);
310 static krb5_error_code
311 fcc_open(krb5_context context
,
317 krb5_boolean exclusive
= ((flags
| O_WRONLY
) == flags
||
318 (flags
| O_RDWR
) == flags
);
320 const char *filename
= FILENAME(id
);
322 fd
= open(filename
, flags
, mode
);
325 krb5_set_error_string(context
, "open(%s): %s", filename
,
330 if((ret
= fcc_lock(context
, id
, fd
, exclusive
)) != 0) {
338 static krb5_error_code
339 fcc_initialize(krb5_context context
,
341 krb5_principal primary_principal
)
343 krb5_fcache
*f
= FCACHE(id
);
346 char *filename
= f
->filename
;
350 ret
= fcc_open(context
, id
, &fd
, O_RDWR
| O_CREAT
| O_EXCL
| O_BINARY
, 0600);
355 sp
= krb5_storage_from_fd(fd
);
356 krb5_storage_set_eof_code(sp
, KRB5_CC_END
);
357 if(context
->fcache_vno
!= 0)
358 f
->version
= context
->fcache_vno
;
360 f
->version
= KRB5_FCC_FVNO_4
;
361 ret
|= krb5_store_int8(sp
, 5);
362 ret
|= krb5_store_int8(sp
, f
->version
);
363 storage_set_flags(context
, sp
, f
->version
);
364 if(f
->version
== KRB5_FCC_FVNO_4
&& ret
== 0) {
366 if (context
->kdc_sec_offset
) {
367 ret
|= krb5_store_int16 (sp
, 12); /* length */
368 ret
|= krb5_store_int16 (sp
, FCC_TAG_DELTATIME
); /* Tag */
369 ret
|= krb5_store_int16 (sp
, 8); /* length of data */
370 ret
|= krb5_store_int32 (sp
, context
->kdc_sec_offset
);
371 ret
|= krb5_store_int32 (sp
, context
->kdc_usec_offset
);
373 ret
|= krb5_store_int16 (sp
, 0);
376 ret
|= krb5_store_principal(sp
, primary_principal
);
378 krb5_storage_free(sp
);
380 fcc_unlock(context
, fd
);
384 krb5_set_error_string (context
, "close %s: %s",
385 FILENAME(id
), strerror(ret
));
390 static krb5_error_code
391 fcc_close(krb5_context context
,
395 krb5_data_free(&id
->data
);
399 static krb5_error_code
400 fcc_destroy(krb5_context context
,
403 erase_file(FILENAME(id
));
407 static krb5_error_code
408 fcc_store_cred(krb5_context context
,
415 ret
= fcc_open(context
, id
, &fd
, O_WRONLY
| O_APPEND
| O_BINARY
, 0);
420 sp
= krb5_storage_from_fd(fd
);
421 krb5_storage_set_eof_code(sp
, KRB5_CC_END
);
422 storage_set_flags(context
, sp
, FCACHE(id
)->version
);
423 if (!krb5_config_get_bool_default(context
, NULL
, TRUE
,
425 "fcc-mit-ticketflags",
427 krb5_storage_set_flags(sp
, KRB5_STORAGE_CREDS_FLAGS_WRONG_BITORDER
);
428 ret
= krb5_store_creds(sp
, creds
);
429 krb5_storage_free(sp
);
431 fcc_unlock(context
, fd
);
435 krb5_set_error_string (context
, "close %s: %s",
436 FILENAME(id
), strerror(ret
));
441 static krb5_error_code
442 init_fcc (krb5_context context
,
444 krb5_storage
**ret_sp
,
452 ret
= fcc_open(context
, id
, &fd
, O_RDONLY
| O_BINARY
, 0);
456 sp
= krb5_storage_from_fd(fd
);
458 krb5_clear_error_string(context
);
462 krb5_storage_set_eof_code(sp
, KRB5_CC_END
);
463 ret
= krb5_ret_int8(sp
, &pvno
);
465 if(ret
== KRB5_CC_END
)
466 ret
= ENOENT
; /* empty file */
467 krb5_clear_error_string(context
);
471 krb5_set_error_string(context
, "Bad version number in credential "
472 "cache file: %s", FILENAME(id
));
473 ret
= KRB5_CCACHE_BADVNO
;
476 ret
= krb5_ret_int8(sp
, &tag
); /* should not be host byte order */
478 krb5_clear_error_string(context
);
479 ret
= KRB5_CC_FORMAT
;
482 FCACHE(id
)->version
= tag
;
483 storage_set_flags(context
, sp
, FCACHE(id
)->version
);
485 case KRB5_FCC_FVNO_4
: {
488 ret
= krb5_ret_int16 (sp
, &length
);
490 ret
= KRB5_CC_FORMAT
;
491 krb5_clear_error_string(context
);
495 int16_t dtag
, data_len
;
499 ret
= krb5_ret_int16 (sp
, &dtag
);
501 krb5_clear_error_string(context
);
502 ret
= KRB5_CC_FORMAT
;
505 ret
= krb5_ret_int16 (sp
, &data_len
);
507 krb5_clear_error_string(context
);
508 ret
= KRB5_CC_FORMAT
;
512 case FCC_TAG_DELTATIME
:
513 ret
= krb5_ret_int32 (sp
, &context
->kdc_sec_offset
);
515 krb5_clear_error_string(context
);
516 ret
= KRB5_CC_FORMAT
;
519 ret
= krb5_ret_int32 (sp
, &context
->kdc_usec_offset
);
521 krb5_clear_error_string(context
);
522 ret
= KRB5_CC_FORMAT
;
527 for (i
= 0; i
< data_len
; ++i
) {
528 ret
= krb5_ret_int8 (sp
, &dummy
);
530 krb5_clear_error_string(context
);
531 ret
= KRB5_CC_FORMAT
;
537 length
-= 4 + data_len
;
541 case KRB5_FCC_FVNO_3
:
542 case KRB5_FCC_FVNO_2
:
543 case KRB5_FCC_FVNO_1
:
546 ret
= KRB5_CCACHE_BADVNO
;
547 krb5_set_error_string(context
, "Unknown version number (%d) in "
548 "credential cache file: %s",
549 (int)tag
, FILENAME(id
));
558 krb5_storage_free(sp
);
559 fcc_unlock(context
, fd
);
564 static krb5_error_code
565 fcc_get_principal(krb5_context context
,
567 krb5_principal
*principal
)
573 ret
= init_fcc (context
, id
, &sp
, &fd
);
576 ret
= krb5_ret_principal(sp
, principal
);
578 krb5_clear_error_string(context
);
579 krb5_storage_free(sp
);
580 fcc_unlock(context
, fd
);
585 static krb5_error_code
586 fcc_end_get (krb5_context context
,
588 krb5_cc_cursor
*cursor
);
590 static krb5_error_code
591 fcc_get_first (krb5_context context
,
593 krb5_cc_cursor
*cursor
)
596 krb5_principal principal
;
598 *cursor
= malloc(sizeof(struct fcc_cursor
));
599 if (*cursor
== NULL
) {
600 krb5_set_error_string (context
, "malloc: out of memory");
603 memset(*cursor
, 0, sizeof(struct fcc_cursor
));
605 ret
= init_fcc (context
, id
, &FCC_CURSOR(*cursor
)->sp
,
606 &FCC_CURSOR(*cursor
)->fd
);
612 ret
= krb5_ret_principal (FCC_CURSOR(*cursor
)->sp
, &principal
);
614 krb5_clear_error_string(context
);
615 fcc_end_get(context
, id
, cursor
);
618 krb5_free_principal (context
, principal
);
619 fcc_unlock(context
, FCC_CURSOR(*cursor
)->fd
);
623 static krb5_error_code
624 fcc_get_next (krb5_context context
,
626 krb5_cc_cursor
*cursor
,
630 if((ret
= fcc_lock(context
, id
, FCC_CURSOR(*cursor
)->fd
, FALSE
)) != 0)
633 ret
= krb5_ret_creds(FCC_CURSOR(*cursor
)->sp
, creds
);
635 krb5_clear_error_string(context
);
637 fcc_unlock(context
, FCC_CURSOR(*cursor
)->fd
);
641 static krb5_error_code
642 fcc_end_get (krb5_context context
,
644 krb5_cc_cursor
*cursor
)
646 krb5_storage_free(FCC_CURSOR(*cursor
)->sp
);
647 close (FCC_CURSOR(*cursor
)->fd
);
653 static krb5_error_code
654 fcc_remove_cred(krb5_context context
,
662 ret
= krb5_cc_gen_new(context
, &krb5_mcc_ops
, ©
);
666 ret
= krb5_cc_copy_cache(context
, id
, copy
);
668 krb5_cc_destroy(context
, copy
);
672 ret
= krb5_cc_remove_cred(context
, copy
, which
, cred
);
674 krb5_cc_destroy(context
, copy
);
678 fcc_destroy(context
, id
);
680 ret
= krb5_cc_copy_cache(context
, copy
, id
);
681 krb5_cc_destroy(context
, copy
);
686 static krb5_error_code
687 fcc_set_flags(krb5_context context
,
694 static krb5_error_code
695 fcc_get_version(krb5_context context
,
698 return FCACHE(id
)->version
;
701 const krb5_cc_ops krb5_fcc_ops
= {
710 NULL
, /* fcc_retrieve */