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: cache.c,v 1.49 2002/05/29 16:08:23 joda Exp $");
39 * Add a new ccache type with operations `ops', overwriting any
40 * existing one if `override'.
41 * Return an error code or 0.
45 krb5_cc_register(krb5_context context,
46 const krb5_cc_ops *ops,
47 krb5_boolean override)
51 for(i = 0; i < context->num_cc_ops && context->cc_ops[i].prefix; i++) {
52 if(strcmp(context->cc_ops[i].prefix, ops->prefix) == 0) {
54 krb5_set_error_string(context,
55 "ccache type %s already exists",
57 return KRB5_CC_TYPE_EXISTS;
62 if(i == context->num_cc_ops) {
63 krb5_cc_ops *o = realloc(context->cc_ops,
64 (context->num_cc_ops + 1) *
65 sizeof(*context->cc_ops));
67 krb5_set_error_string(context, "malloc: out of memory");
70 context->num_cc_ops++;
72 memset(context->cc_ops + i, 0,
73 (context->num_cc_ops - i) * sizeof(*context->cc_ops));
75 memcpy(&context->cc_ops[i], ops, sizeof(context->cc_ops[i]));
80 * Allocate memory for a new ccache in `id' with operations `ops'
81 * and name `residual'.
82 * Return 0 or an error code.
85 static krb5_error_code
86 allocate_ccache (krb5_context context,
87 const krb5_cc_ops *ops,
94 p = malloc(sizeof(*p));
96 krb5_set_error_string(context, "malloc: out of memory");
101 ret = p->ops->resolve(context, id, residual);
108 * Find and allocate a ccache in `id' from the specification in `residual'.
109 * If the ccache name doesn't contain any colon, interpret it as a file name.
110 * Return 0 or an error code.
114 krb5_cc_resolve(krb5_context context,
120 for(i = 0; i < context->num_cc_ops && context->cc_ops[i].prefix; i++) {
121 size_t prefix_len = strlen(context->cc_ops[i].prefix);
123 if(strncmp(context->cc_ops[i].prefix, name, prefix_len) == 0
124 && name[prefix_len] == ':') {
125 return allocate_ccache (context, &context->cc_ops[i],
126 name + prefix_len + 1,
130 if (strchr (name, ':') == NULL)
131 return allocate_ccache (context, &krb5_fcc_ops, name, id);
133 krb5_set_error_string(context, "unknown ccache type %s", name);
134 return KRB5_CC_UNKNOWN_TYPE;
139 * Generate a new ccache of type `ops' in `id'.
140 * Return 0 or an error code.
144 krb5_cc_gen_new(krb5_context context,
145 const krb5_cc_ops *ops,
150 p = malloc (sizeof(*p));
152 krb5_set_error_string(context, "malloc: out of memory");
153 return KRB5_CC_NOMEM;
157 return p->ops->gen_new(context, id);
161 * Return the name of the ccache `id'
165 krb5_cc_get_name(krb5_context context,
168 return id->ops->get_name(context, id);
172 * Return the type of the ccache `id'.
176 krb5_cc_get_type(krb5_context context,
179 return id->ops->prefix;
183 * Return a pointer to a static string containing the default ccache name.
187 krb5_cc_default_name(krb5_context context)
189 static char name[1024];
192 p = getenv("KRB5CCNAME");
194 strlcpy (name, p, sizeof(name));
198 "FILE:/tmp/krb5cc_%u",
204 * Open the default ccache in `id'.
205 * Return 0 or an error code.
209 krb5_cc_default(krb5_context context,
212 return krb5_cc_resolve(context,
213 krb5_cc_default_name(context),
218 * Create a new ccache in `id' for `primary_principal'.
219 * Return 0 or an error code.
223 krb5_cc_initialize(krb5_context context,
225 krb5_principal primary_principal)
227 return id->ops->init(context, id, primary_principal);
232 * Remove the ccache `id'.
233 * Return 0 or an error code.
237 krb5_cc_destroy(krb5_context context,
242 ret = id->ops->destroy(context, id);
243 krb5_cc_close (context, id);
248 * Stop using the ccache `id' and free the related resources.
249 * Return 0 or an error code.
253 krb5_cc_close(krb5_context context,
257 ret = id->ops->close(context, id);
263 * Store `creds' in the ccache `id'.
264 * Return 0 or an error code.
268 krb5_cc_store_cred(krb5_context context,
272 return id->ops->store(context, id, creds);
276 * Retrieve the credential identified by `mcreds' (and `whichfields')
277 * from `id' in `creds'.
278 * Return 0 or an error code.
282 krb5_cc_retrieve_cred(krb5_context context,
284 krb5_flags whichfields,
285 const krb5_creds *mcreds,
289 krb5_cc_cursor cursor;
290 krb5_cc_start_seq_get(context, id, &cursor);
291 while((ret = krb5_cc_next_cred(context, id, &cursor, creds)) == 0){
292 if(krb5_compare_creds(context, whichfields, mcreds, creds)){
296 krb5_free_creds_contents (context, creds);
298 krb5_cc_end_seq_get(context, id, &cursor);
303 * Return the principal of `id' in `principal'.
304 * Return 0 or an error code.
308 krb5_cc_get_principal(krb5_context context,
310 krb5_principal *principal)
312 return id->ops->get_princ(context, id, principal);
316 * Start iterating over `id', `cursor' is initialized to the
318 * Return 0 or an error code.
322 krb5_cc_start_seq_get (krb5_context context,
323 const krb5_ccache id,
324 krb5_cc_cursor *cursor)
326 return id->ops->get_first(context, id, cursor);
330 * Retrieve the next cred pointed to by (`id', `cursor') in `creds'
331 * and advance `cursor'.
332 * Return 0 or an error code.
336 krb5_cc_next_cred (krb5_context context,
337 const krb5_ccache id,
338 krb5_cc_cursor *cursor,
341 return id->ops->get_next(context, id, cursor, creds);
345 * Destroy the cursor `cursor'.
349 krb5_cc_end_seq_get (krb5_context context,
350 const krb5_ccache id,
351 krb5_cc_cursor *cursor)
353 return id->ops->end_get(context, id, cursor);
357 * Remove the credential identified by `cred', `which' from `id'.
361 krb5_cc_remove_cred(krb5_context context,
366 if(id->ops->remove_cred == NULL) {
367 krb5_set_error_string(context,
368 "ccache %s does not support remove_cred",
370 return EACCES; /* XXX */
372 return (*id->ops->remove_cred)(context, id, which, cred);
376 * Set the flags of `id' to `flags'.
380 krb5_cc_set_flags(krb5_context context,
384 return id->ops->set_flags(context, id, flags);
388 * Copy the contents of `from' to `to'.
392 krb5_cc_copy_cache(krb5_context context,
393 const krb5_ccache from,
397 krb5_cc_cursor cursor;
399 krb5_principal princ;
401 ret = krb5_cc_get_principal(context, from, &princ);
404 ret = krb5_cc_initialize(context, to, princ);
406 krb5_free_principal(context, princ);
409 ret = krb5_cc_start_seq_get(context, from, &cursor);
411 krb5_free_principal(context, princ);
414 while(ret == 0 && krb5_cc_next_cred(context, from, &cursor, &cred) == 0){
415 ret = krb5_cc_store_cred(context, to, &cred);
416 krb5_free_creds_contents (context, &cred);
418 krb5_cc_end_seq_get(context, from, &cursor);
419 krb5_free_principal(context, princ);
424 * Return the version of `id'.
428 krb5_cc_get_version(krb5_context context,
429 const krb5_ccache id)
431 if(id->ops->get_version)
432 return id->ops->get_version(context, id);