Add roken/rename.c to fix non-standard rename()
[heimdal.git] / lib / krb5 / fcache.c
blob218bd2cdbfed27fdd3017df49c087c170fa7086b
1 /*
2 * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
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
10 * are met:
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
33 * SUCH DAMAGE.
36 #include "krb5_locl.h"
38 typedef struct krb5_fcache{
39 char *filename;
40 int version;
41 }krb5_fcache;
43 struct fcc_cursor {
44 int fd;
45 krb5_storage *sp;
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,
63 krb5_ccache id)
65 return FILENAME(id);
68 int
69 _krb5_xlock(krb5_context context, int fd, krb5_boolean exclusive,
70 const char *filename)
72 int ret;
73 #ifdef HAVE_FCNTL
74 struct flock l;
76 l.l_start = 0;
77 l.l_len = 0;
78 l.l_type = exclusive ? F_WRLCK : F_RDLCK;
79 l.l_whence = SEEK_SET;
80 ret = fcntl(fd, F_SETLKW, &l);
81 #else
82 ret = flock(fd, exclusive ? LOCK_EX : LOCK_SH);
83 #endif
84 if(ret < 0)
85 ret = errno;
86 if(ret == EACCES) /* fcntl can return EACCES instead of EAGAIN */
87 ret = EAGAIN;
89 switch (ret) {
90 case 0:
91 break;
92 case EINVAL: /* filesystem doesn't support locking, let the user have it */
93 ret = 0;
94 break;
95 case EAGAIN:
96 krb5_set_error_message(context, ret,
97 N_("timed out locking cache file %s", "file"),
98 filename);
99 break;
100 default: {
101 char buf[128];
102 rk_strerror_r(ret, buf, sizeof(buf));
103 krb5_set_error_message(context, ret,
104 N_("error locking cache file %s: %s",
105 "file, error"), filename, buf);
106 break;
109 return ret;
113 _krb5_xunlock(krb5_context context, int fd)
115 int ret;
116 #ifdef HAVE_FCNTL
117 struct flock l;
118 l.l_start = 0;
119 l.l_len = 0;
120 l.l_type = F_UNLCK;
121 l.l_whence = SEEK_SET;
122 ret = fcntl(fd, F_SETLKW, &l);
123 #else
124 ret = flock(fd, LOCK_UN);
125 #endif
126 if (ret < 0)
127 ret = errno;
128 switch (ret) {
129 case 0:
130 break;
131 case EINVAL: /* filesystem doesn't support locking, let the user have it */
132 ret = 0;
133 break;
134 default: {
135 char buf[128];
136 rk_strerror_r(ret, buf, sizeof(buf));
137 krb5_set_error_message(context, ret,
138 N_("Failed to unlock file: %s", ""), buf);
139 break;
142 return ret;
145 static krb5_error_code
146 write_storage(krb5_context context, krb5_storage *sp, int fd)
148 krb5_error_code ret;
149 krb5_data data;
150 ssize_t sret;
152 ret = krb5_storage_to_data(sp, &data);
153 if (ret) {
154 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
155 return ret;
157 sret = write(fd, data.data, data.length);
158 ret = (sret != data.length);
159 krb5_data_free(&data);
160 if (ret) {
161 ret = errno;
162 krb5_set_error_message(context, ret,
163 N_("Failed to write FILE credential data", ""));
164 return ret;
166 return 0;
170 static krb5_error_code KRB5_CALLCONV
171 fcc_lock(krb5_context context, krb5_ccache id,
172 int fd, krb5_boolean exclusive)
174 return _krb5_xlock(context, fd, exclusive, fcc_get_name(context, id));
177 static krb5_error_code KRB5_CALLCONV
178 fcc_unlock(krb5_context context, int fd)
180 return _krb5_xunlock(context, fd);
183 static krb5_error_code KRB5_CALLCONV
184 fcc_resolve(krb5_context context, krb5_ccache *id, const char *res)
186 krb5_fcache *f;
187 f = malloc(sizeof(*f));
188 if(f == NULL) {
189 krb5_set_error_message(context, KRB5_CC_NOMEM,
190 N_("malloc: out of memory", ""));
191 return KRB5_CC_NOMEM;
193 f->filename = strdup(res);
194 if(f->filename == NULL){
195 free(f);
196 krb5_set_error_message(context, KRB5_CC_NOMEM,
197 N_("malloc: out of memory", ""));
198 return KRB5_CC_NOMEM;
200 f->version = 0;
201 (*id)->data.data = f;
202 (*id)->data.length = sizeof(*f);
203 return 0;
207 * Try to scrub the contents of `filename' safely.
210 static int
211 scrub_file (int fd)
213 off_t pos;
214 char buf[128];
216 pos = lseek(fd, 0, SEEK_END);
217 if (pos < 0)
218 return errno;
219 if (lseek(fd, 0, SEEK_SET) < 0)
220 return errno;
221 memset(buf, 0, sizeof(buf));
222 while(pos > 0) {
223 ssize_t tmp = write(fd, buf, min(sizeof(buf), pos));
225 if (tmp < 0)
226 return errno;
227 pos -= tmp;
229 #ifdef _MSC_VER
230 _commit (fd);
231 #else
232 fsync (fd);
233 #endif
234 return 0;
238 * Erase `filename' if it exists, trying to remove the contents if
239 * it's `safe'. We always try to remove the file, it it exists. It's
240 * only overwritten if it's a regular file (not a symlink and not a
241 * hardlink)
244 krb5_error_code
245 _krb5_erase_file(krb5_context context, const char *filename)
247 int fd;
248 struct stat sb1, sb2;
249 int ret;
251 ret = lstat (filename, &sb1);
252 if (ret < 0)
253 return errno;
255 fd = open(filename, O_RDWR | O_BINARY);
256 if(fd < 0) {
257 if(errno == ENOENT)
258 return 0;
259 else
260 return errno;
262 rk_cloexec(fd);
263 ret = _krb5_xlock(context, fd, 1, filename);
264 if (ret) {
265 close(fd);
266 return ret;
268 if (unlink(filename) < 0) {
269 _krb5_xunlock(context, fd);
270 close (fd);
271 return errno;
273 ret = fstat (fd, &sb2);
274 if (ret < 0) {
275 _krb5_xunlock(context, fd);
276 close (fd);
277 return errno;
280 /* check if someone was playing with symlinks */
282 if (sb1.st_dev != sb2.st_dev || sb1.st_ino != sb2.st_ino) {
283 _krb5_xunlock(context, fd);
284 close (fd);
285 return EPERM;
288 /* there are still hard links to this file */
290 if (sb2.st_nlink != 0) {
291 _krb5_xunlock(context, fd);
292 close (fd);
293 return 0;
296 ret = scrub_file (fd);
297 if (ret) {
298 _krb5_xunlock(context, fd);
299 close(fd);
300 return ret;
302 ret = _krb5_xunlock(context, fd);
303 close (fd);
304 return ret;
307 static krb5_error_code KRB5_CALLCONV
308 fcc_gen_new(krb5_context context, krb5_ccache *id)
310 char *file = NULL, *exp_file = NULL;
311 krb5_error_code ret;
312 krb5_fcache *f;
313 int fd;
315 f = malloc(sizeof(*f));
316 if(f == NULL) {
317 krb5_set_error_message(context, KRB5_CC_NOMEM,
318 N_("malloc: out of memory", ""));
319 return KRB5_CC_NOMEM;
321 ret = asprintf (&file, "%sXXXXXX", KRB5_DEFAULT_CCFILE_ROOT);
322 if(ret < 0 || file == NULL) {
323 free(f);
324 krb5_set_error_message(context, KRB5_CC_NOMEM,
325 N_("malloc: out of memory", ""));
326 return KRB5_CC_NOMEM;
328 ret = _krb5_expand_path_tokens(context, file, &exp_file);
329 free(file);
330 if (ret)
331 return ret;
333 file = exp_file;
335 fd = mkstemp(exp_file);
336 if(fd < 0) {
337 int ret = errno;
338 krb5_set_error_message(context, ret, N_("mkstemp %s failed", ""), exp_file);
339 free(f);
340 free(exp_file);
341 return ret;
343 close(fd);
344 f->filename = exp_file;
345 f->version = 0;
346 (*id)->data.data = f;
347 (*id)->data.length = sizeof(*f);
348 return 0;
351 static void
352 storage_set_flags(krb5_context context, krb5_storage *sp, int vno)
354 int flags = 0;
355 switch(vno) {
356 case KRB5_FCC_FVNO_1:
357 flags |= KRB5_STORAGE_PRINCIPAL_WRONG_NUM_COMPONENTS;
358 flags |= KRB5_STORAGE_PRINCIPAL_NO_NAME_TYPE;
359 flags |= KRB5_STORAGE_HOST_BYTEORDER;
360 break;
361 case KRB5_FCC_FVNO_2:
362 flags |= KRB5_STORAGE_HOST_BYTEORDER;
363 break;
364 case KRB5_FCC_FVNO_3:
365 flags |= KRB5_STORAGE_KEYBLOCK_KEYTYPE_TWICE;
366 break;
367 case KRB5_FCC_FVNO_4:
368 break;
369 default:
370 krb5_abortx(context,
371 "storage_set_flags called with bad vno (%x)", vno);
373 krb5_storage_set_flags(sp, flags);
376 static krb5_error_code KRB5_CALLCONV
377 fcc_open(krb5_context context,
378 krb5_ccache id,
379 int *fd_ret,
380 int flags,
381 mode_t mode)
383 krb5_boolean exclusive = ((flags | O_WRONLY) == flags ||
384 (flags | O_RDWR) == flags);
385 krb5_error_code ret;
386 const char *filename = FILENAME(id);
387 int fd;
388 fd = open(filename, flags, mode);
389 if(fd < 0) {
390 char buf[128];
391 ret = errno;
392 rk_strerror_r(ret, buf, sizeof(buf));
393 krb5_set_error_message(context, ret, N_("open(%s): %s", "file, error"),
394 filename, buf);
395 return ret;
397 rk_cloexec(fd);
399 if((ret = fcc_lock(context, id, fd, exclusive)) != 0) {
400 close(fd);
401 return ret;
403 *fd_ret = fd;
404 return 0;
407 static krb5_error_code KRB5_CALLCONV
408 fcc_initialize(krb5_context context,
409 krb5_ccache id,
410 krb5_principal primary_principal)
412 krb5_fcache *f = FCACHE(id);
413 int ret = 0;
414 int fd;
415 char *filename = f->filename;
417 unlink (filename);
419 ret = fcc_open(context, id, &fd, O_RDWR | O_CREAT | O_EXCL | O_BINARY | O_CLOEXEC, 0600);
420 if(ret)
421 return ret;
423 krb5_storage *sp;
424 sp = krb5_storage_emem();
425 krb5_storage_set_eof_code(sp, KRB5_CC_END);
426 if(context->fcache_vno != 0)
427 f->version = context->fcache_vno;
428 else
429 f->version = KRB5_FCC_FVNO_4;
430 ret |= krb5_store_int8(sp, 5);
431 ret |= krb5_store_int8(sp, f->version);
432 storage_set_flags(context, sp, f->version);
433 if(f->version == KRB5_FCC_FVNO_4 && ret == 0) {
434 /* V4 stuff */
435 if (context->kdc_sec_offset) {
436 ret |= krb5_store_int16 (sp, 12); /* length */
437 ret |= krb5_store_int16 (sp, FCC_TAG_DELTATIME); /* Tag */
438 ret |= krb5_store_int16 (sp, 8); /* length of data */
439 ret |= krb5_store_int32 (sp, context->kdc_sec_offset);
440 ret |= krb5_store_int32 (sp, context->kdc_usec_offset);
441 } else {
442 ret |= krb5_store_int16 (sp, 0);
445 ret |= krb5_store_principal(sp, primary_principal);
447 ret |= write_storage(context, sp, fd);
449 krb5_storage_free(sp);
451 fcc_unlock(context, fd);
452 if (close(fd) < 0)
453 if (ret == 0) {
454 char buf[128];
455 ret = errno;
456 rk_strerror_r(ret, buf, sizeof(buf));
457 krb5_set_error_message (context, ret, N_("close %s: %s", ""),
458 FILENAME(id), buf);
460 return ret;
463 static krb5_error_code KRB5_CALLCONV
464 fcc_close(krb5_context context,
465 krb5_ccache id)
467 free (FILENAME(id));
468 krb5_data_free(&id->data);
469 return 0;
472 static krb5_error_code KRB5_CALLCONV
473 fcc_destroy(krb5_context context,
474 krb5_ccache id)
476 _krb5_erase_file(context, FILENAME(id));
477 return 0;
480 static krb5_error_code KRB5_CALLCONV
481 fcc_store_cred(krb5_context context,
482 krb5_ccache id,
483 krb5_creds *creds)
485 int ret;
486 int fd;
488 ret = fcc_open(context, id, &fd, O_WRONLY | O_APPEND | O_BINARY | O_CLOEXEC, 0);
489 if(ret)
490 return ret;
492 krb5_storage *sp;
494 sp = krb5_storage_emem();
495 krb5_storage_set_eof_code(sp, KRB5_CC_END);
496 storage_set_flags(context, sp, FCACHE(id)->version);
497 if (!krb5_config_get_bool_default(context, NULL, TRUE,
498 "libdefaults",
499 "fcc-mit-ticketflags",
500 NULL))
501 krb5_storage_set_flags(sp, KRB5_STORAGE_CREDS_FLAGS_WRONG_BITORDER);
502 ret = krb5_store_creds(sp, creds);
503 if (ret == 0)
504 ret = write_storage(context, sp, fd);
505 krb5_storage_free(sp);
507 fcc_unlock(context, fd);
508 if (close(fd) < 0) {
509 if (ret == 0) {
510 char buf[128];
511 rk_strerror_r(ret, buf, sizeof(buf));
512 ret = errno;
513 krb5_set_error_message (context, ret, N_("close %s: %s", ""),
514 FILENAME(id), buf);
517 return ret;
520 static krb5_error_code
521 init_fcc (krb5_context context,
522 krb5_ccache id,
523 krb5_storage **ret_sp,
524 int *ret_fd,
525 krb5_deltat *kdc_offset)
527 int fd;
528 int8_t pvno, tag;
529 krb5_storage *sp;
530 krb5_error_code ret;
532 if (kdc_offset)
533 *kdc_offset = 0;
535 ret = fcc_open(context, id, &fd, O_RDONLY | O_BINARY | O_CLOEXEC, 0);
536 if(ret)
537 return ret;
539 sp = krb5_storage_from_fd(fd);
540 if(sp == NULL) {
541 krb5_clear_error_message(context);
542 ret = ENOMEM;
543 goto out;
545 krb5_storage_set_eof_code(sp, KRB5_CC_END);
546 ret = krb5_ret_int8(sp, &pvno);
547 if(ret != 0) {
548 if(ret == KRB5_CC_END) {
549 ret = ENOENT;
550 krb5_set_error_message(context, ret,
551 N_("Empty credential cache file: %s", ""),
552 FILENAME(id));
553 } else
554 krb5_set_error_message(context, ret, N_("Error reading pvno "
555 "in cache file: %s", ""),
556 FILENAME(id));
557 goto out;
559 if(pvno != 5) {
560 ret = KRB5_CCACHE_BADVNO;
561 krb5_set_error_message(context, ret, N_("Bad version number in credential "
562 "cache file: %s", ""),
563 FILENAME(id));
564 goto out;
566 ret = krb5_ret_int8(sp, &tag); /* should not be host byte order */
567 if(ret != 0) {
568 ret = KRB5_CC_FORMAT;
569 krb5_set_error_message(context, ret, "Error reading tag in "
570 "cache file: %s", FILENAME(id));
571 goto out;
573 FCACHE(id)->version = tag;
574 storage_set_flags(context, sp, FCACHE(id)->version);
575 switch (tag) {
576 case KRB5_FCC_FVNO_4: {
577 int16_t length;
579 ret = krb5_ret_int16 (sp, &length);
580 if(ret) {
581 ret = KRB5_CC_FORMAT;
582 krb5_set_error_message(context, ret,
583 N_("Error reading tag length in "
584 "cache file: %s", ""), FILENAME(id));
585 goto out;
587 while(length > 0) {
588 int16_t dtag, data_len;
589 int i;
590 int8_t dummy;
592 ret = krb5_ret_int16 (sp, &dtag);
593 if(ret) {
594 ret = KRB5_CC_FORMAT;
595 krb5_set_error_message(context, ret, N_("Error reading dtag in "
596 "cache file: %s", ""),
597 FILENAME(id));
598 goto out;
600 ret = krb5_ret_int16 (sp, &data_len);
601 if(ret) {
602 ret = KRB5_CC_FORMAT;
603 krb5_set_error_message(context, ret,
604 N_("Error reading dlength "
605 "in cache file: %s",""),
606 FILENAME(id));
607 goto out;
609 switch (dtag) {
610 case FCC_TAG_DELTATIME : {
611 int32_t offset;
613 ret = krb5_ret_int32 (sp, &offset);
614 ret |= krb5_ret_int32 (sp, &context->kdc_usec_offset);
615 if(ret) {
616 ret = KRB5_CC_FORMAT;
617 krb5_set_error_message(context, ret,
618 N_("Error reading kdc_sec in "
619 "cache file: %s", ""),
620 FILENAME(id));
621 goto out;
623 context->kdc_sec_offset = offset;
624 if (kdc_offset)
625 *kdc_offset = offset;
626 break;
628 default :
629 for (i = 0; i < data_len; ++i) {
630 ret = krb5_ret_int8 (sp, &dummy);
631 if(ret) {
632 ret = KRB5_CC_FORMAT;
633 krb5_set_error_message(context, ret,
634 N_("Error reading unknown "
635 "tag in cache file: %s", ""),
636 FILENAME(id));
637 goto out;
640 break;
642 length -= 4 + data_len;
644 break;
646 case KRB5_FCC_FVNO_3:
647 case KRB5_FCC_FVNO_2:
648 case KRB5_FCC_FVNO_1:
649 break;
650 default :
651 ret = KRB5_CCACHE_BADVNO;
652 krb5_set_error_message(context, ret,
653 N_("Unknown version number (%d) in "
654 "credential cache file: %s", ""),
655 (int)tag, FILENAME(id));
656 goto out;
658 *ret_sp = sp;
659 *ret_fd = fd;
661 return 0;
662 out:
663 if(sp != NULL)
664 krb5_storage_free(sp);
665 fcc_unlock(context, fd);
666 close(fd);
667 return ret;
670 static krb5_error_code KRB5_CALLCONV
671 fcc_get_principal(krb5_context context,
672 krb5_ccache id,
673 krb5_principal *principal)
675 krb5_error_code ret;
676 int fd;
677 krb5_storage *sp;
679 ret = init_fcc (context, id, &sp, &fd, NULL);
680 if (ret)
681 return ret;
682 ret = krb5_ret_principal(sp, principal);
683 if (ret)
684 krb5_clear_error_message(context);
685 krb5_storage_free(sp);
686 fcc_unlock(context, fd);
687 close(fd);
688 return ret;
691 static krb5_error_code KRB5_CALLCONV
692 fcc_end_get (krb5_context context,
693 krb5_ccache id,
694 krb5_cc_cursor *cursor);
696 static krb5_error_code KRB5_CALLCONV
697 fcc_get_first (krb5_context context,
698 krb5_ccache id,
699 krb5_cc_cursor *cursor)
701 krb5_error_code ret;
702 krb5_principal principal;
704 *cursor = malloc(sizeof(struct fcc_cursor));
705 if (*cursor == NULL) {
706 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
707 return ENOMEM;
709 memset(*cursor, 0, sizeof(struct fcc_cursor));
711 ret = init_fcc (context, id, &FCC_CURSOR(*cursor)->sp,
712 &FCC_CURSOR(*cursor)->fd, NULL);
713 if (ret) {
714 free(*cursor);
715 *cursor = NULL;
716 return ret;
718 ret = krb5_ret_principal (FCC_CURSOR(*cursor)->sp, &principal);
719 if(ret) {
720 krb5_clear_error_message(context);
721 fcc_end_get(context, id, cursor);
722 return ret;
724 krb5_free_principal (context, principal);
725 fcc_unlock(context, FCC_CURSOR(*cursor)->fd);
726 return 0;
729 static krb5_error_code KRB5_CALLCONV
730 fcc_get_next (krb5_context context,
731 krb5_ccache id,
732 krb5_cc_cursor *cursor,
733 krb5_creds *creds)
735 krb5_error_code ret;
736 if((ret = fcc_lock(context, id, FCC_CURSOR(*cursor)->fd, FALSE)) != 0)
737 return ret;
739 ret = krb5_ret_creds(FCC_CURSOR(*cursor)->sp, creds);
740 if (ret)
741 krb5_clear_error_message(context);
743 fcc_unlock(context, FCC_CURSOR(*cursor)->fd);
744 return ret;
747 static krb5_error_code KRB5_CALLCONV
748 fcc_end_get (krb5_context context,
749 krb5_ccache id,
750 krb5_cc_cursor *cursor)
752 krb5_storage_free(FCC_CURSOR(*cursor)->sp);
753 close (FCC_CURSOR(*cursor)->fd);
754 free(*cursor);
755 *cursor = NULL;
756 return 0;
759 static krb5_error_code KRB5_CALLCONV
760 fcc_remove_cred(krb5_context context,
761 krb5_ccache id,
762 krb5_flags which,
763 krb5_creds *cred)
765 krb5_error_code ret;
766 krb5_ccache copy, newfile;
767 char *newname = NULL;
768 int fd;
770 ret = krb5_cc_new_unique(context, krb5_cc_type_memory, NULL, &copy);
771 if (ret)
772 return ret;
774 ret = krb5_cc_copy_cache(context, id, copy);
775 if (ret) {
776 krb5_cc_destroy(context, copy);
777 return ret;
780 ret = krb5_cc_remove_cred(context, copy, which, cred);
781 if (ret) {
782 krb5_cc_destroy(context, copy);
783 return ret;
786 ret = asprintf(&newname, "FILE:%s.XXXXXX", FILENAME(id));
787 if (ret < 0 || newname == NULL) {
788 krb5_cc_destroy(context, copy);
789 return ENOMEM;
792 fd = mkstemp(&newname[5]);
793 if (fd < 0) {
794 ret = errno;
795 krb5_cc_destroy(context, copy);
796 return ret;
798 close(fd);
800 ret = krb5_cc_resolve(context, newname, &newfile);
801 if (ret) {
802 unlink(&newname[5]);
803 free(newname);
804 krb5_cc_destroy(context, copy);
805 return ret;
808 ret = krb5_cc_copy_cache(context, copy, newfile);
809 krb5_cc_destroy(context, copy);
810 if (ret) {
811 free(newname);
812 krb5_cc_destroy(context, newfile);
813 return ret;
816 ret = rk_rename(&newname[5], FILENAME(id));
817 if (ret)
818 ret = errno;
819 free(newname);
820 krb5_cc_close(context, newfile);
822 return ret;
825 static krb5_error_code KRB5_CALLCONV
826 fcc_set_flags(krb5_context context,
827 krb5_ccache id,
828 krb5_flags flags)
830 return 0; /* XXX */
833 static int KRB5_CALLCONV
834 fcc_get_version(krb5_context context,
835 krb5_ccache id)
837 return FCACHE(id)->version;
840 struct fcache_iter {
841 int first;
844 static krb5_error_code KRB5_CALLCONV
845 fcc_get_cache_first(krb5_context context, krb5_cc_cursor *cursor)
847 struct fcache_iter *iter;
849 iter = calloc(1, sizeof(*iter));
850 if (iter == NULL) {
851 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
852 return ENOMEM;
854 iter->first = 1;
855 *cursor = iter;
856 return 0;
859 static krb5_error_code KRB5_CALLCONV
860 fcc_get_cache_next(krb5_context context, krb5_cc_cursor cursor, krb5_ccache *id)
862 struct fcache_iter *iter = cursor;
863 krb5_error_code ret;
864 const char *fn;
865 char *expandedfn = NULL;
867 if (!iter->first) {
868 krb5_clear_error_message(context);
869 return KRB5_CC_END;
871 iter->first = 0;
873 fn = krb5_cc_default_name(context);
874 if (fn == NULL || strncasecmp(fn, "FILE:", 5) != 0) {
875 ret = _krb5_expand_default_cc_name(context,
876 KRB5_DEFAULT_CCNAME_FILE,
877 &expandedfn);
878 if (ret)
879 return ret;
880 fn = expandedfn;
882 /* check if file exists, don't return a non existant "next" */
883 if (strncasecmp(fn, "FILE:", 5) == 0) {
884 struct stat sb;
885 ret = stat(fn + 5, &sb);
886 if (ret) {
887 ret = KRB5_CC_END;
888 goto out;
891 ret = krb5_cc_resolve(context, fn, id);
892 out:
893 if (expandedfn)
894 free(expandedfn);
896 return ret;
899 static krb5_error_code KRB5_CALLCONV
900 fcc_end_cache_get(krb5_context context, krb5_cc_cursor cursor)
902 struct fcache_iter *iter = cursor;
903 free(iter);
904 return 0;
907 static krb5_error_code KRB5_CALLCONV
908 fcc_move(krb5_context context, krb5_ccache from, krb5_ccache to)
910 krb5_error_code ret = 0;
912 ret = rk_rename(FILENAME(from), FILENAME(to));
914 if (ret && errno != EXDEV) {
915 char buf[128];
916 ret = errno;
917 rk_strerror_r(ret, buf, sizeof(buf));
918 krb5_set_error_message(context, ret,
919 N_("Rename of file from %s "
920 "to %s failed: %s", ""),
921 FILENAME(from), FILENAME(to), buf);
922 return ret;
923 } else if (ret && errno == EXDEV) {
924 /* make a copy and delete the orignal */
925 krb5_ssize_t sz1, sz2;
926 int fd1, fd2;
927 char buf[BUFSIZ];
929 ret = fcc_open(context, from, &fd1, O_RDONLY | O_BINARY | O_CLOEXEC, 0);
930 if(ret)
931 return ret;
933 unlink(FILENAME(to));
935 ret = fcc_open(context, to, &fd2,
936 O_WRONLY | O_CREAT | O_EXCL | O_BINARY | O_CLOEXEC, 0600);
937 if(ret)
938 goto out1;
940 while((sz1 = read(fd1, buf, sizeof(buf))) > 0) {
941 sz2 = write(fd2, buf, sz1);
942 if (sz1 != sz2) {
943 ret = EIO;
944 krb5_set_error_message(context, ret,
945 N_("Failed to write data from one file "
946 "credential cache to the other", ""));
947 goto out2;
950 if (sz1 < 0) {
951 ret = EIO;
952 krb5_set_error_message(context, ret,
953 N_("Failed to read data from one file "
954 "credential cache to the other", ""));
955 goto out2;
957 out2:
958 fcc_unlock(context, fd2);
959 close(fd2);
961 out1:
962 fcc_unlock(context, fd1);
963 close(fd1);
965 _krb5_erase_file(context, FILENAME(from));
967 if (ret) {
968 _krb5_erase_file(context, FILENAME(to));
969 return ret;
973 /* make sure ->version is uptodate */
975 krb5_storage *sp;
976 int fd;
977 if ((ret = init_fcc (context, to, &sp, &fd, NULL)) == 0) {
978 if (sp)
979 krb5_storage_free(sp);
980 fcc_unlock(context, fd);
981 close(fd);
985 fcc_close(context, from);
987 return ret;
990 static krb5_error_code KRB5_CALLCONV
991 fcc_get_default_name(krb5_context context, char **str)
993 return _krb5_expand_default_cc_name(context,
994 KRB5_DEFAULT_CCNAME_FILE,
995 str);
998 static krb5_error_code KRB5_CALLCONV
999 fcc_lastchange(krb5_context context, krb5_ccache id, krb5_timestamp *mtime)
1001 krb5_error_code ret;
1002 struct stat sb;
1003 int fd;
1005 ret = fcc_open(context, id, &fd, O_RDONLY | O_BINARY | O_CLOEXEC, 0);
1006 if(ret)
1007 return ret;
1008 ret = fstat(fd, &sb);
1009 close(fd);
1010 if (ret) {
1011 ret = errno;
1012 krb5_set_error_message(context, ret, N_("Failed to stat cache file", ""));
1013 return ret;
1015 *mtime = sb.st_mtime;
1016 return 0;
1019 static krb5_error_code KRB5_CALLCONV
1020 fcc_set_kdc_offset(krb5_context context, krb5_ccache id, krb5_deltat kdc_offset)
1022 return 0;
1025 static krb5_error_code KRB5_CALLCONV
1026 fcc_get_kdc_offset(krb5_context context, krb5_ccache id, krb5_deltat *kdc_offset)
1028 krb5_error_code ret;
1029 krb5_storage *sp = NULL;
1030 int fd;
1031 ret = init_fcc(context, id, &sp, &fd, kdc_offset);
1032 if (sp)
1033 krb5_storage_free(sp);
1034 fcc_unlock(context, fd);
1035 close(fd);
1037 return ret;
1042 * Variable containing the FILE based credential cache implemention.
1044 * @ingroup krb5_ccache
1047 KRB5_LIB_VARIABLE const krb5_cc_ops krb5_fcc_ops = {
1048 KRB5_CC_OPS_VERSION,
1049 "FILE",
1050 fcc_get_name,
1051 fcc_resolve,
1052 fcc_gen_new,
1053 fcc_initialize,
1054 fcc_destroy,
1055 fcc_close,
1056 fcc_store_cred,
1057 NULL, /* fcc_retrieve */
1058 fcc_get_principal,
1059 fcc_get_first,
1060 fcc_get_next,
1061 fcc_end_get,
1062 fcc_remove_cred,
1063 fcc_set_flags,
1064 fcc_get_version,
1065 fcc_get_cache_first,
1066 fcc_get_cache_next,
1067 fcc_end_cache_get,
1068 fcc_move,
1069 fcc_get_default_name,
1070 NULL,
1071 fcc_lastchange,
1072 fcc_set_kdc_offset,
1073 fcc_get_kdc_offset