2 * Copyright (c) 1997 - 2002 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.34 2002/04/18 14:01:29 joda 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,
68 static krb5_error_code
69 fcc_resolve(krb5_context context, krb5_ccache *id, const char *res)
72 f = malloc(sizeof(*f));
74 krb5_set_error_string(context, "malloc: out of memory");
77 f->filename = strdup(res);
78 if(f->filename == NULL){
80 krb5_set_error_string(context, "malloc: out of memory");
85 (*id)->data.length = sizeof(*f);
90 * Try to scrub the contents of `filename' safely.
99 pos = lseek(fd, 0, SEEK_END);
102 if (lseek(fd, 0, SEEK_SET) < 0)
104 memset(buf, 0, sizeof(buf));
106 ssize_t tmp = write(fd, buf, min(sizeof(buf), pos));
117 * Erase `filename' if it exists, trying to remove the contents if
118 * it's `safe'. We always try to remove the file, it it exists. It's
119 * only overwritten if it's a regular file (not a symlink and not a
123 static krb5_error_code
124 erase_file(const char *filename)
127 struct stat sb1, sb2;
130 ret = lstat (filename, &sb1);
134 fd = open(filename, O_RDWR | O_BINARY);
141 if (unlink(filename) < 0) {
146 ret = fstat (fd, &sb2);
152 /* check if someone was playing with symlinks */
154 if (sb1.st_dev != sb2.st_dev || sb1.st_ino != sb2.st_ino) {
159 /* there are still hard links to this file */
161 if (sb2.st_nlink != 0) {
166 ret = scrub_file (fd);
171 static krb5_error_code
172 fcc_gen_new(krb5_context context, krb5_ccache *id)
178 f = malloc(sizeof(*f));
180 krb5_set_error_string(context, "malloc: out of memory");
181 return KRB5_CC_NOMEM;
183 asprintf (&file, "%sXXXXXX", KRB5_DEFAULT_CCFILE_ROOT);
186 krb5_set_error_string(context, "malloc: out of memory");
187 return KRB5_CC_NOMEM;
193 krb5_set_error_string(context, "mkstemp %s", file);
199 (*id)->data.data = f;
200 (*id)->data.length = sizeof(*f);
205 storage_set_flags(krb5_context context, krb5_storage *sp, int vno)
209 case KRB5_FCC_FVNO_1:
210 flags |= KRB5_STORAGE_PRINCIPAL_WRONG_NUM_COMPONENTS;
211 flags |= KRB5_STORAGE_PRINCIPAL_NO_NAME_TYPE;
212 flags |= KRB5_STORAGE_HOST_BYTEORDER;
214 case KRB5_FCC_FVNO_2:
215 flags |= KRB5_STORAGE_HOST_BYTEORDER;
217 case KRB5_FCC_FVNO_3:
218 flags |= KRB5_STORAGE_KEYBLOCK_KEYTYPE_TWICE;
220 case KRB5_FCC_FVNO_4:
224 "storage_set_flags called with bad vno (%x)", vno);
226 krb5_storage_set_flags(sp, flags);
229 static krb5_error_code
230 fcc_initialize(krb5_context context,
232 krb5_principal primary_principal)
234 krb5_fcache *f = FCACHE(id);
237 char *filename = f->filename;
241 fd = open(filename, O_RDWR | O_CREAT | O_EXCL | O_BINARY, 0600);
244 krb5_set_error_string(context, "open(%s): %s", filename,
250 sp = krb5_storage_from_fd(fd);
251 krb5_storage_set_eof_code(sp, KRB5_CC_END);
252 if(context->fcache_vno != 0)
253 f->version = context->fcache_vno;
255 f->version = KRB5_FCC_FVNO_4;
256 ret |= krb5_store_int8(sp, 5);
257 ret |= krb5_store_int8(sp, f->version);
258 storage_set_flags(context, sp, f->version);
259 if(f->version == KRB5_FCC_FVNO_4 && ret == 0) {
261 if (context->kdc_sec_offset) {
262 ret |= krb5_store_int16 (sp, 12); /* length */
263 ret |= krb5_store_int16 (sp, FCC_TAG_DELTATIME); /* Tag */
264 ret |= krb5_store_int16 (sp, 8); /* length of data */
265 ret |= krb5_store_int32 (sp, context->kdc_sec_offset);
266 ret |= krb5_store_int32 (sp, context->kdc_usec_offset);
268 ret |= krb5_store_int16 (sp, 0);
271 ret |= krb5_store_principal(sp, primary_principal);
272 krb5_storage_free(sp);
277 krb5_set_error_string (context, "close %s: %s", filename,
284 static krb5_error_code
285 fcc_close(krb5_context context,
289 krb5_data_free(&id->data);
293 static krb5_error_code
294 fcc_destroy(krb5_context context,
305 static krb5_error_code
306 fcc_store_cred(krb5_context context,
316 fd = open(f, O_WRONLY | O_APPEND | O_BINARY);
319 krb5_set_error_string (context, "open(%s): %s", f, strerror(ret));
324 sp = krb5_storage_from_fd(fd);
325 krb5_storage_set_eof_code(sp, KRB5_CC_END);
326 storage_set_flags(context, sp, FCACHE(id)->version);
327 ret = krb5_store_creds(sp, creds);
328 krb5_storage_free(sp);
333 krb5_set_error_string (context, "close %s: %s", f, strerror(ret));
338 static krb5_error_code
339 fcc_read_cred (krb5_context context,
346 storage_set_flags(context, sp, fc->version);
348 ret = krb5_ret_creds(sp, creds);
352 static krb5_error_code
353 init_fcc (krb5_context context,
355 krb5_storage **ret_sp,
363 fd = open(fcache->filename, O_RDONLY | O_BINARY);
366 krb5_set_error_string(context, "open(%s): %s", fcache->filename,
370 sp = krb5_storage_from_fd(fd);
371 krb5_storage_set_eof_code(sp, KRB5_CC_END);
372 ret = krb5_ret_int8(sp, &pvno);
373 if(ret == KRB5_CC_END)
378 krb5_storage_free(sp);
380 return KRB5_CCACHE_BADVNO;
382 krb5_ret_int8(sp, &tag); /* should not be host byte order */
383 fcache->version = tag;
384 storage_set_flags(context, sp, fcache->version);
386 case KRB5_FCC_FVNO_4: {
389 krb5_ret_int16 (sp, &length);
391 int16_t tag, data_len;
395 krb5_ret_int16 (sp, &tag);
396 krb5_ret_int16 (sp, &data_len);
398 case FCC_TAG_DELTATIME :
399 krb5_ret_int32 (sp, &context->kdc_sec_offset);
400 krb5_ret_int32 (sp, &context->kdc_usec_offset);
403 for (i = 0; i < data_len; ++i)
404 krb5_ret_int8 (sp, &dummy);
407 length -= 4 + data_len;
411 case KRB5_FCC_FVNO_3:
412 case KRB5_FCC_FVNO_2:
413 case KRB5_FCC_FVNO_1:
416 krb5_storage_free (sp);
418 return KRB5_CCACHE_BADVNO;
425 static krb5_error_code
426 fcc_get_principal(krb5_context context,
428 krb5_principal *principal)
431 krb5_fcache *f = FCACHE(id);
435 ret = init_fcc (context, f, &sp, &fd);
438 ret = krb5_ret_principal(sp, principal);
439 krb5_storage_free(sp);
444 static krb5_error_code
445 fcc_get_first (krb5_context context,
447 krb5_cc_cursor *cursor)
450 krb5_principal principal;
451 krb5_fcache *f = FCACHE(id);
453 *cursor = malloc(sizeof(struct fcc_cursor));
455 ret = init_fcc (context, f, &FCC_CURSOR(*cursor)->sp,
456 &FCC_CURSOR(*cursor)->fd);
459 krb5_ret_principal (FCC_CURSOR(*cursor)->sp, &principal);
460 krb5_free_principal (context, principal);
464 static krb5_error_code
465 fcc_get_next (krb5_context context,
467 krb5_cc_cursor *cursor,
470 return fcc_read_cred (context, FCACHE(id), FCC_CURSOR(*cursor)->sp, creds);
473 static krb5_error_code
474 fcc_end_get (krb5_context context,
476 krb5_cc_cursor *cursor)
478 krb5_storage_free(FCC_CURSOR(*cursor)->sp);
479 close (FCC_CURSOR(*cursor)->fd);
484 static krb5_error_code
485 fcc_remove_cred(krb5_context context,
493 static krb5_error_code
494 fcc_set_flags(krb5_context context,
501 static krb5_error_code
502 fcc_get_version(krb5_context context,
505 return FCACHE(id)->version;
508 const krb5_cc_ops krb5_fcc_ops = {
517 NULL, /* fcc_retrieve */