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"
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(int fd
)
110 #ifdef HAVE_FCNTL_LOCK
115 l
.l_whence
= SEEK_SET
;
116 return fcntl(fd
, F_SETLKW
, &l
);
118 return flock(fd
, LOCK_UN
);
122 static krb5_error_code
123 fcc_lock(krb5_context context
, krb5_ccache id
,
124 int fd
, krb5_boolean exclusive
)
126 return _krb5_xlock(context
, fd
, exclusive
, fcc_get_name(context
, id
));
129 static krb5_error_code
130 fcc_unlock(krb5_context context
, int fd
)
132 return _krb5_xunlock(fd
);
135 static krb5_error_code
136 fcc_resolve(krb5_context context
, krb5_ccache
*id
, const char *res
)
139 f
= malloc(sizeof(*f
));
141 krb5_set_error_string(context
, "malloc: out of memory");
142 return KRB5_CC_NOMEM
;
144 f
->filename
= strdup(res
);
145 if(f
->filename
== NULL
){
147 krb5_set_error_string(context
, "malloc: out of memory");
148 return KRB5_CC_NOMEM
;
151 (*id
)->data
.data
= f
;
152 (*id
)->data
.length
= sizeof(*f
);
157 * Try to scrub the contents of `filename' safely.
166 pos
= lseek(fd
, 0, SEEK_END
);
169 if (lseek(fd
, 0, SEEK_SET
) < 0)
171 memset(buf
, 0, sizeof(buf
));
173 ssize_t tmp
= write(fd
, buf
, min(sizeof(buf
), pos
));
184 * Erase `filename' if it exists, trying to remove the contents if
185 * it's `safe'. We always try to remove the file, it it exists. It's
186 * only overwritten if it's a regular file (not a symlink and not a
190 static krb5_error_code
191 erase_file(const char *filename
)
194 struct stat sb1
, sb2
;
197 ret
= lstat (filename
, &sb1
);
201 fd
= open(filename
, O_RDWR
| O_BINARY
);
208 if (unlink(filename
) < 0) {
212 ret
= fstat (fd
, &sb2
);
218 /* check if someone was playing with symlinks */
220 if (sb1
.st_dev
!= sb2
.st_dev
|| sb1
.st_ino
!= sb2
.st_ino
) {
225 /* there are still hard links to this file */
227 if (sb2
.st_nlink
!= 0) {
232 ret
= scrub_file (fd
);
237 static krb5_error_code
238 fcc_gen_new(krb5_context context
, krb5_ccache
*id
)
244 f
= malloc(sizeof(*f
));
246 krb5_set_error_string(context
, "malloc: out of memory");
247 return KRB5_CC_NOMEM
;
249 asprintf (&file
, "%sXXXXXX", KRB5_DEFAULT_CCFILE_ROOT
);
252 krb5_set_error_string(context
, "malloc: out of memory");
253 return KRB5_CC_NOMEM
;
259 krb5_set_error_string(context
, "mkstemp %s", file
);
265 (*id
)->data
.data
= f
;
266 (*id
)->data
.length
= sizeof(*f
);
271 storage_set_flags(krb5_context context
, krb5_storage
*sp
, int vno
)
275 case KRB5_FCC_FVNO_1
:
276 flags
|= KRB5_STORAGE_PRINCIPAL_WRONG_NUM_COMPONENTS
;
277 flags
|= KRB5_STORAGE_PRINCIPAL_NO_NAME_TYPE
;
278 flags
|= KRB5_STORAGE_HOST_BYTEORDER
;
280 case KRB5_FCC_FVNO_2
:
281 flags
|= KRB5_STORAGE_HOST_BYTEORDER
;
283 case KRB5_FCC_FVNO_3
:
284 flags
|= KRB5_STORAGE_KEYBLOCK_KEYTYPE_TWICE
;
286 case KRB5_FCC_FVNO_4
:
290 "storage_set_flags called with bad vno (%x)", vno
);
292 krb5_storage_set_flags(sp
, flags
);
295 static krb5_error_code
296 fcc_open(krb5_context context
,
302 krb5_boolean exclusive
= ((flags
| O_WRONLY
) == flags
||
303 (flags
| O_RDWR
) == flags
);
305 const char *filename
= FILENAME(id
);
307 fd
= open(filename
, flags
, mode
);
310 krb5_set_error_string(context
, "open(%s): %s", filename
,
315 if((ret
= fcc_lock(context
, id
, fd
, exclusive
)) != 0) {
323 static krb5_error_code
324 fcc_initialize(krb5_context context
,
326 krb5_principal primary_principal
)
328 krb5_fcache
*f
= FCACHE(id
);
331 char *filename
= f
->filename
;
335 ret
= fcc_open(context
, id
, &fd
, O_RDWR
| O_CREAT
| O_EXCL
| O_BINARY
, 0600);
340 sp
= krb5_storage_from_fd(fd
);
341 krb5_storage_set_eof_code(sp
, KRB5_CC_END
);
342 if(context
->fcache_vno
!= 0)
343 f
->version
= context
->fcache_vno
;
345 f
->version
= KRB5_FCC_FVNO_4
;
346 ret
|= krb5_store_int8(sp
, 5);
347 ret
|= krb5_store_int8(sp
, f
->version
);
348 storage_set_flags(context
, sp
, f
->version
);
349 if(f
->version
== KRB5_FCC_FVNO_4
&& ret
== 0) {
351 if (context
->kdc_sec_offset
) {
352 ret
|= krb5_store_int16 (sp
, 12); /* length */
353 ret
|= krb5_store_int16 (sp
, FCC_TAG_DELTATIME
); /* Tag */
354 ret
|= krb5_store_int16 (sp
, 8); /* length of data */
355 ret
|= krb5_store_int32 (sp
, context
->kdc_sec_offset
);
356 ret
|= krb5_store_int32 (sp
, context
->kdc_usec_offset
);
358 ret
|= krb5_store_int16 (sp
, 0);
361 ret
|= krb5_store_principal(sp
, primary_principal
);
363 krb5_storage_free(sp
);
365 fcc_unlock(context
, fd
);
369 krb5_set_error_string (context
, "close %s: %s",
370 FILENAME(id
), strerror(ret
));
375 static krb5_error_code
376 fcc_close(krb5_context context
,
380 krb5_data_free(&id
->data
);
384 static krb5_error_code
385 fcc_destroy(krb5_context context
,
388 erase_file(FILENAME(id
));
392 static krb5_error_code
393 fcc_store_cred(krb5_context context
,
400 ret
= fcc_open(context
, id
, &fd
, O_WRONLY
| O_APPEND
| O_BINARY
, 0);
405 sp
= krb5_storage_from_fd(fd
);
406 krb5_storage_set_eof_code(sp
, KRB5_CC_END
);
407 storage_set_flags(context
, sp
, FCACHE(id
)->version
);
408 if (krb5_config_get_bool_default(context
, NULL
, FALSE
,
410 "fcc-mit-ticketflags",
412 ret
= _krb5_store_creds_heimdal_0_7(sp
, creds
);
414 ret
= _krb5_store_creds_heimdal_pre_0_7(sp
, creds
);
415 krb5_storage_free(sp
);
417 fcc_unlock(context
, fd
);
421 krb5_set_error_string (context
, "close %s: %s",
422 FILENAME(id
), strerror(ret
));
427 static krb5_error_code
428 init_fcc (krb5_context context
,
430 krb5_storage
**ret_sp
,
438 ret
= fcc_open(context
, id
, &fd
, O_RDONLY
| O_BINARY
, 0);
443 sp
= krb5_storage_from_fd(fd
);
448 krb5_storage_set_eof_code(sp
, KRB5_CC_END
);
449 ret
= krb5_ret_int8(sp
, &pvno
);
451 if(ret
== KRB5_CC_END
)
452 ret
= ENOENT
; /* empty file */
456 ret
= KRB5_CCACHE_BADVNO
;
459 ret
= krb5_ret_int8(sp
, &tag
); /* should not be host byte order */
461 ret
= KRB5_CC_FORMAT
;
464 FCACHE(id
)->version
= tag
;
465 storage_set_flags(context
, sp
, FCACHE(id
)->version
);
467 case KRB5_FCC_FVNO_4
: {
470 ret
= krb5_ret_int16 (sp
, &length
);
472 ret
= KRB5_CC_FORMAT
;
476 int16_t tag
, data_len
;
480 ret
= krb5_ret_int16 (sp
, &tag
);
482 ret
= KRB5_CC_FORMAT
;
485 ret
= krb5_ret_int16 (sp
, &data_len
);
487 ret
= KRB5_CC_FORMAT
;
491 case FCC_TAG_DELTATIME
:
492 ret
= krb5_ret_int32 (sp
, &context
->kdc_sec_offset
);
494 ret
= KRB5_CC_FORMAT
;
497 ret
= krb5_ret_int32 (sp
, &context
->kdc_usec_offset
);
499 ret
= KRB5_CC_FORMAT
;
504 for (i
= 0; i
< data_len
; ++i
) {
505 ret
= krb5_ret_int8 (sp
, &dummy
);
507 ret
= KRB5_CC_FORMAT
;
513 length
-= 4 + data_len
;
517 case KRB5_FCC_FVNO_3
:
518 case KRB5_FCC_FVNO_2
:
519 case KRB5_FCC_FVNO_1
:
522 ret
= KRB5_CCACHE_BADVNO
;
531 krb5_storage_free(sp
);
532 fcc_unlock(context
, fd
);
537 static krb5_error_code
538 fcc_get_principal(krb5_context context
,
540 krb5_principal
*principal
)
546 ret
= init_fcc (context
, id
, &sp
, &fd
);
549 ret
= krb5_ret_principal(sp
, principal
);
550 krb5_storage_free(sp
);
551 fcc_unlock(context
, fd
);
556 static krb5_error_code
557 fcc_end_get (krb5_context context
,
559 krb5_cc_cursor
*cursor
);
561 static krb5_error_code
562 fcc_get_first (krb5_context context
,
564 krb5_cc_cursor
*cursor
)
567 krb5_principal principal
;
569 *cursor
= malloc(sizeof(struct fcc_cursor
));
571 ret
= init_fcc (context
, id
, &FCC_CURSOR(*cursor
)->sp
,
572 &FCC_CURSOR(*cursor
)->fd
);
577 ret
= krb5_ret_principal (FCC_CURSOR(*cursor
)->sp
, &principal
);
579 fcc_end_get(context
, id
, cursor
);
582 krb5_free_principal (context
, principal
);
583 fcc_unlock(context
, FCC_CURSOR(*cursor
)->fd
);
587 static krb5_error_code
588 fcc_get_next (krb5_context context
,
590 krb5_cc_cursor
*cursor
,
594 if((ret
= fcc_lock(context
, id
, FCC_CURSOR(*cursor
)->fd
, FALSE
)) != 0)
597 ret
= krb5_ret_creds(FCC_CURSOR(*cursor
)->sp
, creds
);
599 fcc_unlock(context
, FCC_CURSOR(*cursor
)->fd
);
603 static krb5_error_code
604 fcc_end_get (krb5_context context
,
606 krb5_cc_cursor
*cursor
)
608 krb5_storage_free(FCC_CURSOR(*cursor
)->sp
);
609 close (FCC_CURSOR(*cursor
)->fd
);
615 static krb5_error_code
616 fcc_remove_cred(krb5_context context
,
624 static krb5_error_code
625 fcc_set_flags(krb5_context context
,
632 static krb5_error_code
633 fcc_get_version(krb5_context context
,
636 return FCACHE(id
)->version
;
639 const krb5_cc_ops krb5_fcc_ops
= {
648 NULL
, /* fcc_retrieve */