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)
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);
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));
226 ssize_t tmp = write(fd, buf, min((off_t)sizeof(buf), pos));
241 * Erase `filename' if it exists, trying to remove the contents if
242 * it's `safe'. We always try to remove the file, it it exists. It's
243 * only overwritten if it's a regular file (not a symlink and not a
248 _krb5_erase_file(krb5_context context, const char *filename)
251 struct stat sb1, sb2;
254 ret = lstat (filename, &sb1);
258 fd = open(filename, O_RDWR | O_BINARY);
266 ret = _krb5_xlock(context, fd, 1, filename);
271 if (unlink(filename) < 0) {
272 _krb5_xunlock(context, fd);
276 ret = fstat (fd, &sb2);
278 _krb5_xunlock(context, fd);
283 /* check if someone was playing with symlinks */
285 if (sb1.st_dev != sb2.st_dev || sb1.st_ino != sb2.st_ino) {
286 _krb5_xunlock(context, fd);
291 /* there are still hard links to this file */
293 if (sb2.st_nlink != 0) {
294 _krb5_xunlock(context, fd);
299 ret = scrub_file (fd);
301 _krb5_xunlock(context, fd);
305 ret = _krb5_xunlock(context, fd);
310 static krb5_error_code KRB5_CALLCONV
311 fcc_gen_new(krb5_context context, krb5_ccache *id)
313 char *file = NULL, *exp_file = NULL;
318 f = malloc(sizeof(*f));
320 krb5_set_error_message(context, KRB5_CC_NOMEM,
321 N_("malloc: out of memory", ""));
322 return KRB5_CC_NOMEM;
324 ret = asprintf (&file, "%sXXXXXX", KRB5_DEFAULT_CCFILE_ROOT);
325 if(ret < 0 || file == NULL) {
327 krb5_set_error_message(context, KRB5_CC_NOMEM,
328 N_("malloc: out of memory", ""));
329 return KRB5_CC_NOMEM;
331 ret = _krb5_expand_path_tokens(context, file, &exp_file);
338 fd = mkstemp(exp_file);
341 krb5_set_error_message(context, xret, N_("mkstemp %s failed", ""), exp_file);
347 f->filename = exp_file;
349 (*id)->data.data = f;
350 (*id)->data.length = sizeof(*f);
355 storage_set_flags(krb5_context context, krb5_storage *sp, int vno)
359 case KRB5_FCC_FVNO_1:
360 flags |= KRB5_STORAGE_PRINCIPAL_WRONG_NUM_COMPONENTS;
361 flags |= KRB5_STORAGE_PRINCIPAL_NO_NAME_TYPE;
362 flags |= KRB5_STORAGE_HOST_BYTEORDER;
364 case KRB5_FCC_FVNO_2:
365 flags |= KRB5_STORAGE_HOST_BYTEORDER;
367 case KRB5_FCC_FVNO_3:
368 flags |= KRB5_STORAGE_KEYBLOCK_KEYTYPE_TWICE;
370 case KRB5_FCC_FVNO_4:
374 "storage_set_flags called with bad vno (%x)", vno);
376 krb5_storage_set_flags(sp, flags);
379 static krb5_error_code KRB5_CALLCONV
380 fcc_open(krb5_context context,
386 krb5_boolean exclusive = ((flags | O_WRONLY) == flags ||
387 (flags | O_RDWR) == flags);
389 const char *filename;
392 if (FCACHE(id) == NULL)
393 return krb5_einval(context, 2);
395 filename = FILENAME(id);
397 fd = open(filename, flags, mode);
401 rk_strerror_r(ret, buf, sizeof(buf));
402 krb5_set_error_message(context, ret, N_("open(%s): %s", "file, error"),
408 if((ret = fcc_lock(context, id, fd, exclusive)) != 0) {
416 static krb5_error_code KRB5_CALLCONV
417 fcc_initialize(krb5_context context,
419 krb5_principal primary_principal)
421 krb5_fcache *f = FCACHE(id);
426 return krb5_einval(context, 2);
428 unlink (f->filename);
430 ret = fcc_open(context, id, &fd, O_RDWR | O_CREAT | O_EXCL | O_BINARY | O_CLOEXEC, 0600);
435 sp = krb5_storage_emem();
436 krb5_storage_set_eof_code(sp, KRB5_CC_END);
437 if(context->fcache_vno != 0)
438 f->version = context->fcache_vno;
440 f->version = KRB5_FCC_FVNO_4;
441 ret |= krb5_store_int8(sp, 5);
442 ret |= krb5_store_int8(sp, f->version);
443 storage_set_flags(context, sp, f->version);
444 if(f->version == KRB5_FCC_FVNO_4 && ret == 0) {
446 if (context->kdc_sec_offset) {
447 ret |= krb5_store_int16 (sp, 12); /* length */
448 ret |= krb5_store_int16 (sp, FCC_TAG_DELTATIME); /* Tag */
449 ret |= krb5_store_int16 (sp, 8); /* length of data */
450 ret |= krb5_store_int32 (sp, context->kdc_sec_offset);
451 ret |= krb5_store_int32 (sp, context->kdc_usec_offset);
453 ret |= krb5_store_int16 (sp, 0);
456 ret |= krb5_store_principal(sp, primary_principal);
458 ret |= write_storage(context, sp, fd);
460 krb5_storage_free(sp);
462 fcc_unlock(context, fd);
467 rk_strerror_r(ret, buf, sizeof(buf));
468 krb5_set_error_message (context, ret, N_("close %s: %s", ""),
474 static krb5_error_code KRB5_CALLCONV
475 fcc_close(krb5_context context,
478 if (FCACHE(id) == NULL)
479 return krb5_einval(context, 2);
482 krb5_data_free(&id->data);
486 static krb5_error_code KRB5_CALLCONV
487 fcc_destroy(krb5_context context,
490 if (FCACHE(id) == NULL)
491 return krb5_einval(context, 2);
493 _krb5_erase_file(context, FILENAME(id));
497 static krb5_error_code KRB5_CALLCONV
498 fcc_store_cred(krb5_context context,
505 ret = fcc_open(context, id, &fd, O_WRONLY | O_APPEND | O_BINARY | O_CLOEXEC, 0);
511 sp = krb5_storage_emem();
512 krb5_storage_set_eof_code(sp, KRB5_CC_END);
513 storage_set_flags(context, sp, FCACHE(id)->version);
514 if (!krb5_config_get_bool_default(context, NULL, TRUE,
516 "fcc-mit-ticketflags",
518 krb5_storage_set_flags(sp, KRB5_STORAGE_CREDS_FLAGS_WRONG_BITORDER);
519 ret = krb5_store_creds(sp, creds);
521 ret = write_storage(context, sp, fd);
522 krb5_storage_free(sp);
524 fcc_unlock(context, fd);
528 rk_strerror_r(ret, buf, sizeof(buf));
530 krb5_set_error_message (context, ret, N_("close %s: %s", ""),
537 static krb5_error_code
538 init_fcc (krb5_context context,
540 krb5_storage **ret_sp,
542 krb5_deltat *kdc_offset)
552 ret = fcc_open(context, id, &fd, O_RDONLY | O_BINARY | O_CLOEXEC, 0);
556 sp = krb5_storage_from_fd(fd);
558 krb5_clear_error_message(context);
562 krb5_storage_set_eof_code(sp, KRB5_CC_END);
563 ret = krb5_ret_int8(sp, &pvno);
565 if(ret == KRB5_CC_END) {
567 krb5_set_error_message(context, ret,
568 N_("Empty credential cache file: %s", ""),
571 krb5_set_error_message(context, ret, N_("Error reading pvno "
572 "in cache file: %s", ""),
577 ret = KRB5_CCACHE_BADVNO;
578 krb5_set_error_message(context, ret, N_("Bad version number in credential "
579 "cache file: %s", ""),
583 ret = krb5_ret_int8(sp, &tag); /* should not be host byte order */
585 ret = KRB5_CC_FORMAT;
586 krb5_set_error_message(context, ret, "Error reading tag in "
587 "cache file: %s", FILENAME(id));
590 FCACHE(id)->version = tag;
591 storage_set_flags(context, sp, FCACHE(id)->version);
593 case KRB5_FCC_FVNO_4: {
596 ret = krb5_ret_int16 (sp, &length);
598 ret = KRB5_CC_FORMAT;
599 krb5_set_error_message(context, ret,
600 N_("Error reading tag length in "
601 "cache file: %s", ""), FILENAME(id));
605 int16_t dtag, data_len;
609 ret = krb5_ret_int16 (sp, &dtag);
611 ret = KRB5_CC_FORMAT;
612 krb5_set_error_message(context, ret, N_("Error reading dtag in "
613 "cache file: %s", ""),
617 ret = krb5_ret_int16 (sp, &data_len);
619 ret = KRB5_CC_FORMAT;
620 krb5_set_error_message(context, ret,
621 N_("Error reading dlength "
622 "in cache file: %s",""),
627 case FCC_TAG_DELTATIME : {
630 ret = krb5_ret_int32 (sp, &offset);
631 ret |= krb5_ret_int32 (sp, &context->kdc_usec_offset);
633 ret = KRB5_CC_FORMAT;
634 krb5_set_error_message(context, ret,
635 N_("Error reading kdc_sec in "
636 "cache file: %s", ""),
640 context->kdc_sec_offset = offset;
642 *kdc_offset = offset;
646 for (i = 0; i < data_len; ++i) {
647 ret = krb5_ret_int8 (sp, &dummy);
649 ret = KRB5_CC_FORMAT;
650 krb5_set_error_message(context, ret,
651 N_("Error reading unknown "
652 "tag in cache file: %s", ""),
659 length -= 4 + data_len;
663 case KRB5_FCC_FVNO_3:
664 case KRB5_FCC_FVNO_2:
665 case KRB5_FCC_FVNO_1:
668 ret = KRB5_CCACHE_BADVNO;
669 krb5_set_error_message(context, ret,
670 N_("Unknown version number (%d) in "
671 "credential cache file: %s", ""),
672 (int)tag, FILENAME(id));
681 krb5_storage_free(sp);
682 fcc_unlock(context, fd);
687 static krb5_error_code KRB5_CALLCONV
688 fcc_get_principal(krb5_context context,
690 krb5_principal *principal)
696 ret = init_fcc (context, id, &sp, &fd, NULL);
699 ret = krb5_ret_principal(sp, principal);
701 krb5_clear_error_message(context);
702 krb5_storage_free(sp);
703 fcc_unlock(context, fd);
708 static krb5_error_code KRB5_CALLCONV
709 fcc_end_get (krb5_context context,
711 krb5_cc_cursor *cursor);
713 static krb5_error_code KRB5_CALLCONV
714 fcc_get_first (krb5_context context,
716 krb5_cc_cursor *cursor)
719 krb5_principal principal;
721 if (FCACHE(id) == NULL)
722 return krb5_einval(context, 2);
724 *cursor = malloc(sizeof(struct fcc_cursor));
725 if (*cursor == NULL) {
726 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
729 memset(*cursor, 0, sizeof(struct fcc_cursor));
731 ret = init_fcc (context, id, &FCC_CURSOR(*cursor)->sp,
732 &FCC_CURSOR(*cursor)->fd, NULL);
738 ret = krb5_ret_principal (FCC_CURSOR(*cursor)->sp, &principal);
740 krb5_clear_error_message(context);
741 fcc_end_get(context, id, cursor);
744 krb5_free_principal (context, principal);
745 fcc_unlock(context, FCC_CURSOR(*cursor)->fd);
749 static krb5_error_code KRB5_CALLCONV
750 fcc_get_next (krb5_context context,
752 krb5_cc_cursor *cursor,
757 if (FCACHE(id) == NULL)
758 return krb5_einval(context, 2);
760 if (FCC_CURSOR(*cursor) == NULL)
761 return krb5_einval(context, 3);
763 if((ret = fcc_lock(context, id, FCC_CURSOR(*cursor)->fd, FALSE)) != 0)
766 ret = krb5_ret_creds(FCC_CURSOR(*cursor)->sp, creds);
768 krb5_clear_error_message(context);
770 fcc_unlock(context, FCC_CURSOR(*cursor)->fd);
774 static krb5_error_code KRB5_CALLCONV
775 fcc_end_get (krb5_context context,
777 krb5_cc_cursor *cursor)
780 if (FCACHE(id) == NULL)
781 return krb5_einval(context, 2);
783 if (FCC_CURSOR(*cursor) == NULL)
784 return krb5_einval(context, 3);
786 krb5_storage_free(FCC_CURSOR(*cursor)->sp);
787 close (FCC_CURSOR(*cursor)->fd);
793 static krb5_error_code KRB5_CALLCONV
794 fcc_remove_cred(krb5_context context,
800 krb5_ccache copy, newfile;
801 char *newname = NULL;
804 if (FCACHE(id) == NULL)
805 return krb5_einval(context, 2);
807 ret = krb5_cc_new_unique(context, krb5_cc_type_memory, NULL, ©);
811 ret = krb5_cc_copy_cache(context, id, copy);
813 krb5_cc_destroy(context, copy);
817 ret = krb5_cc_remove_cred(context, copy, which, cred);
819 krb5_cc_destroy(context, copy);
823 ret = asprintf(&newname, "FILE:%s.XXXXXX", FILENAME(id));
824 if (ret < 0 || newname == NULL) {
825 krb5_cc_destroy(context, copy);
829 fd = mkstemp(&newname[5]);
832 krb5_cc_destroy(context, copy);
837 ret = krb5_cc_resolve(context, newname, &newfile);
841 krb5_cc_destroy(context, copy);
845 ret = krb5_cc_copy_cache(context, copy, newfile);
846 krb5_cc_destroy(context, copy);
849 krb5_cc_destroy(context, newfile);
853 ret = rk_rename(&newname[5], FILENAME(id));
857 krb5_cc_close(context, newfile);
862 static krb5_error_code KRB5_CALLCONV
863 fcc_set_flags(krb5_context context,
867 if (FCACHE(id) == NULL)
868 return krb5_einval(context, 2);
873 static int KRB5_CALLCONV
874 fcc_get_version(krb5_context context,
877 if (FCACHE(id) == NULL)
880 return FCACHE(id)->version;
887 static krb5_error_code KRB5_CALLCONV
888 fcc_get_cache_first(krb5_context context, krb5_cc_cursor *cursor)
890 struct fcache_iter *iter;
892 iter = calloc(1, sizeof(*iter));
894 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
902 static krb5_error_code KRB5_CALLCONV
903 fcc_get_cache_next(krb5_context context, krb5_cc_cursor cursor, krb5_ccache *id)
905 struct fcache_iter *iter = cursor;
908 char *expandedfn = NULL;
911 return krb5_einval(context, 2);
914 krb5_clear_error_message(context);
919 fn = krb5_cc_default_name(context);
920 if (fn == NULL || strncasecmp(fn, "FILE:", 5) != 0) {
921 ret = _krb5_expand_default_cc_name(context,
922 KRB5_DEFAULT_CCNAME_FILE,
928 /* check if file exists, don't return a non existant "next" */
929 if (strncasecmp(fn, "FILE:", 5) == 0) {
931 ret = stat(fn + 5, &sb);
937 ret = krb5_cc_resolve(context, fn, id);
945 static krb5_error_code KRB5_CALLCONV
946 fcc_end_cache_get(krb5_context context, krb5_cc_cursor cursor)
948 struct fcache_iter *iter = cursor;
951 return krb5_einval(context, 2);
957 static krb5_error_code KRB5_CALLCONV
958 fcc_move(krb5_context context, krb5_ccache from, krb5_ccache to)
960 krb5_error_code ret = 0;
962 ret = rk_rename(FILENAME(from), FILENAME(to));
964 if (ret && errno != EXDEV) {
967 rk_strerror_r(ret, buf, sizeof(buf));
968 krb5_set_error_message(context, ret,
969 N_("Rename of file from %s "
970 "to %s failed: %s", ""),
971 FILENAME(from), FILENAME(to), buf);
973 } else if (ret && errno == EXDEV) {
974 /* make a copy and delete the orignal */
975 krb5_ssize_t sz1, sz2;
979 ret = fcc_open(context, from, &fd1, O_RDONLY | O_BINARY | O_CLOEXEC, 0);
983 unlink(FILENAME(to));
985 ret = fcc_open(context, to, &fd2,
986 O_WRONLY | O_CREAT | O_EXCL | O_BINARY | O_CLOEXEC, 0600);
990 while((sz1 = read(fd1, buf, sizeof(buf))) > 0) {
991 sz2 = write(fd2, buf, sz1);
994 krb5_set_error_message(context, ret,
995 N_("Failed to write data from one file "
996 "credential cache to the other", ""));
1002 krb5_set_error_message(context, ret,
1003 N_("Failed to read data from one file "
1004 "credential cache to the other", ""));
1008 fcc_unlock(context, fd2);
1012 fcc_unlock(context, fd1);
1015 _krb5_erase_file(context, FILENAME(from));
1018 _krb5_erase_file(context, FILENAME(to));
1023 /* make sure ->version is uptodate */
1027 if ((ret = init_fcc (context, to, &sp, &fd, NULL)) == 0) {
1029 krb5_storage_free(sp);
1030 fcc_unlock(context, fd);
1035 fcc_close(context, from);
1040 static krb5_error_code KRB5_CALLCONV
1041 fcc_get_default_name(krb5_context context, char **str)
1043 return _krb5_expand_default_cc_name(context,
1044 KRB5_DEFAULT_CCNAME_FILE,
1048 static krb5_error_code KRB5_CALLCONV
1049 fcc_lastchange(krb5_context context, krb5_ccache id, krb5_timestamp *mtime)
1051 krb5_error_code ret;
1055 ret = fcc_open(context, id, &fd, O_RDONLY | O_BINARY | O_CLOEXEC, 0);
1058 ret = fstat(fd, &sb);
1062 krb5_set_error_message(context, ret, N_("Failed to stat cache file", ""));
1065 *mtime = sb.st_mtime;
1069 static krb5_error_code KRB5_CALLCONV
1070 fcc_set_kdc_offset(krb5_context context, krb5_ccache id, krb5_deltat kdc_offset)
1075 static krb5_error_code KRB5_CALLCONV
1076 fcc_get_kdc_offset(krb5_context context, krb5_ccache id, krb5_deltat *kdc_offset)
1078 krb5_error_code ret;
1079 krb5_storage *sp = NULL;
1081 ret = init_fcc(context, id, &sp, &fd, kdc_offset);
1083 krb5_storage_free(sp);
1084 fcc_unlock(context, fd);
1092 * Variable containing the FILE based credential cache implemention.
1094 * @ingroup krb5_ccache
1097 KRB5_LIB_VARIABLE const krb5_cc_ops krb5_fcc_ops = {
1098 KRB5_CC_OPS_VERSION,
1107 NULL, /* fcc_retrieve */
1115 fcc_get_cache_first,
1119 fcc_get_default_name,