2 * Copyright (c) 1997-2003 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.52 2003/03/16 18:23:59 lha 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 krb5_cc_ops of a the ccache `id'.
187 krb5_cc_get_ops(krb5_context context
, krb5_ccache id
)
193 * Set the default cc name for `context' to `name'.
197 krb5_cc_set_default_name(krb5_context context
, const char *name
)
199 krb5_error_code ret
= 0;
204 e
= getenv("KRB5CCNAME");
208 asprintf(&p
,"FILE:/tmp/krb5cc_%u", (unsigned)getuid());
215 if (context
->default_cc_name
)
216 free(context
->default_cc_name
);
218 context
->default_cc_name
= p
;
224 * Return a pointer to a context static string containing the default ccache name.
228 krb5_cc_default_name(krb5_context context
)
230 if (context
->default_cc_name
== NULL
)
231 krb5_cc_set_default_name(context
, NULL
);
233 return context
->default_cc_name
;
237 * Open the default ccache in `id'.
238 * Return 0 or an error code.
242 krb5_cc_default(krb5_context context
,
245 const char *p
= krb5_cc_default_name(context
);
249 return krb5_cc_resolve(context
, p
, id
);
253 * Create a new ccache in `id' for `primary_principal'.
254 * Return 0 or an error code.
258 krb5_cc_initialize(krb5_context context
,
260 krb5_principal primary_principal
)
262 return id
->ops
->init(context
, id
, primary_principal
);
267 * Remove the ccache `id'.
268 * Return 0 or an error code.
272 krb5_cc_destroy(krb5_context context
,
277 ret
= id
->ops
->destroy(context
, id
);
278 krb5_cc_close (context
, id
);
283 * Stop using the ccache `id' and free the related resources.
284 * Return 0 or an error code.
288 krb5_cc_close(krb5_context context
,
292 ret
= id
->ops
->close(context
, id
);
298 * Store `creds' in the ccache `id'.
299 * Return 0 or an error code.
303 krb5_cc_store_cred(krb5_context context
,
307 return id
->ops
->store(context
, id
, creds
);
311 * Retrieve the credential identified by `mcreds' (and `whichfields')
312 * from `id' in `creds'.
313 * Return 0 or an error code.
317 krb5_cc_retrieve_cred(krb5_context context
,
319 krb5_flags whichfields
,
320 const krb5_creds
*mcreds
,
324 krb5_cc_cursor cursor
;
325 krb5_cc_start_seq_get(context
, id
, &cursor
);
326 while((ret
= krb5_cc_next_cred(context
, id
, &cursor
, creds
)) == 0){
327 if(krb5_compare_creds(context
, whichfields
, mcreds
, creds
)){
331 krb5_free_creds_contents (context
, creds
);
333 krb5_cc_end_seq_get(context
, id
, &cursor
);
338 * Return the principal of `id' in `principal'.
339 * Return 0 or an error code.
343 krb5_cc_get_principal(krb5_context context
,
345 krb5_principal
*principal
)
347 return id
->ops
->get_princ(context
, id
, principal
);
351 * Start iterating over `id', `cursor' is initialized to the
353 * Return 0 or an error code.
357 krb5_cc_start_seq_get (krb5_context context
,
358 const krb5_ccache id
,
359 krb5_cc_cursor
*cursor
)
361 return id
->ops
->get_first(context
, id
, cursor
);
365 * Retrieve the next cred pointed to by (`id', `cursor') in `creds'
366 * and advance `cursor'.
367 * Return 0 or an error code.
371 krb5_cc_next_cred (krb5_context context
,
372 const krb5_ccache id
,
373 krb5_cc_cursor
*cursor
,
376 return id
->ops
->get_next(context
, id
, cursor
, creds
);
380 * Destroy the cursor `cursor'.
384 krb5_cc_end_seq_get (krb5_context context
,
385 const krb5_ccache id
,
386 krb5_cc_cursor
*cursor
)
388 return id
->ops
->end_get(context
, id
, cursor
);
392 * Remove the credential identified by `cred', `which' from `id'.
396 krb5_cc_remove_cred(krb5_context context
,
401 if(id
->ops
->remove_cred
== NULL
) {
402 krb5_set_error_string(context
,
403 "ccache %s does not support remove_cred",
405 return EACCES
; /* XXX */
407 return (*id
->ops
->remove_cred
)(context
, id
, which
, cred
);
411 * Set the flags of `id' to `flags'.
415 krb5_cc_set_flags(krb5_context context
,
419 return id
->ops
->set_flags(context
, id
, flags
);
423 * Copy the contents of `from' to `to'.
427 krb5_cc_copy_cache(krb5_context context
,
428 const krb5_ccache from
,
432 krb5_cc_cursor cursor
;
434 krb5_principal princ
;
436 ret
= krb5_cc_get_principal(context
, from
, &princ
);
439 ret
= krb5_cc_initialize(context
, to
, princ
);
441 krb5_free_principal(context
, princ
);
444 ret
= krb5_cc_start_seq_get(context
, from
, &cursor
);
446 krb5_free_principal(context
, princ
);
449 while(ret
== 0 && krb5_cc_next_cred(context
, from
, &cursor
, &cred
) == 0){
450 ret
= krb5_cc_store_cred(context
, to
, &cred
);
451 krb5_free_creds_contents (context
, &cred
);
453 krb5_cc_end_seq_get(context
, from
, &cursor
);
454 krb5_free_principal(context
, princ
);
459 * Return the version of `id'.
463 krb5_cc_get_version(krb5_context context
,
464 const krb5_ccache id
)
466 if(id
->ops
->get_version
)
467 return id
->ops
->get_version(context
, id
);