2 * Copyright (c) 1997-2005 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"
39 * Add a new ccache type with operations `ops', overwriting any
40 * existing one if `override'.
41 * Return an error code or 0.
44 krb5_error_code KRB5_LIB_FUNCTION
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.
113 krb5_error_code KRB5_LIB_FUNCTION
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.
143 krb5_error_code KRB5_LIB_FUNCTION
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 * Generates a new unique ccache of `type` in `id'. If `type' is NULL,
162 * the library chooses the default credential cache type. The supplied
163 * `hint' (that can be NULL) is a string that the credential cache
164 * type can use to base the name of the credential on, this is to make
165 * its easier for the user to differentiate the credentials.
167 * Returns 0 or an error code.
170 krb5_error_code KRB5_LIB_FUNCTION
171 krb5_cc_new_unique(krb5_context context
, const char *type
,
172 const char *hint
, krb5_ccache
*id
)
174 const krb5_cc_ops
*ops
;
179 ops
= krb5_cc_get_prefix_ops(context
, type
);
181 krb5_set_error_string(context
, "Credential cache type %s is unknown",
183 return KRB5_CC_UNKNOWN_TYPE
;
186 return krb5_cc_gen_new(context
, ops
, id
);
190 * Return the name of the ccache `id'
193 const char* KRB5_LIB_FUNCTION
194 krb5_cc_get_name(krb5_context context
,
197 return id
->ops
->get_name(context
, id
);
201 * Return the type of the ccache `id'.
204 const char* KRB5_LIB_FUNCTION
205 krb5_cc_get_type(krb5_context context
,
208 return id
->ops
->prefix
;
212 * Return krb5_cc_ops of a the ccache `id'.
216 krb5_cc_get_ops(krb5_context context
, krb5_ccache id
)
222 * Expand variables in `str' into `res'
226 _krb5_expand_default_cc_name(krb5_context context
, const char *str
, char **res
)
228 size_t tlen
, len
= 0;
229 char *tmp
, *tmp2
, *append
;
233 while (str
&& *str
) {
234 tmp
= strstr(str
, "%{");
235 if (tmp
&& tmp
!= str
) {
236 append
= malloc((tmp
- str
) + 1);
238 memcpy(append
, str
, tmp
- str
);
239 append
[tmp
- str
] = '\0';
243 tmp2
= strchr(tmp
, '}');
247 krb5_set_error_string(context
, "variable missing }");
248 return KRB5_CONFIG_BADFORMAT
;
250 if (strncasecmp(tmp
, "%{uid}", 6) == 0)
251 asprintf(&append
, "%u", (unsigned)getuid());
252 else if (strncasecmp(tmp
, "%{null}", 7) == 0)
257 krb5_set_error_string(context
,
258 "expand default cache unknown "
260 (int)(tmp2
- tmp
) - 2, tmp
+ 2);
261 return KRB5_CONFIG_BADFORMAT
;
263 if (append
== NULL
) {
266 krb5_set_error_string(context
, "malloc - out of memory");
271 append
= (char *)str
;
275 tlen
= strlen(append
);
276 tmp
= realloc(*res
, len
+ tlen
+ 1);
280 krb5_set_error_string(context
, "malloc - out of memory");
284 memcpy(*res
+ len
, append
, tlen
+ 1);
293 * Set the default cc name for `context' to `name'.
296 krb5_error_code KRB5_LIB_FUNCTION
297 krb5_cc_set_default_name(krb5_context context
, const char *name
)
299 krb5_error_code ret
= 0;
303 const char *e
= NULL
;
306 e
= getenv("KRB5CCNAME");
311 e
= krb5_config_get_string(context
, NULL
, "libdefaults",
312 "default_cc_name", NULL
);
314 ret
= _krb5_expand_default_cc_name(context
, e
, &p
);
320 asprintf(&p
,"FILE:/tmp/krb5cc_%u", (unsigned)getuid());
325 krb5_set_error_string(context
, "malloc - out of memory");
329 if (context
->default_cc_name
)
330 free(context
->default_cc_name
);
332 context
->default_cc_name
= p
;
338 * Return a pointer to a context static string containing the default
342 const char* KRB5_LIB_FUNCTION
343 krb5_cc_default_name(krb5_context context
)
345 if (context
->default_cc_name
== NULL
)
346 krb5_cc_set_default_name(context
, NULL
);
348 return context
->default_cc_name
;
352 * Open the default ccache in `id'.
353 * Return 0 or an error code.
356 krb5_error_code KRB5_LIB_FUNCTION
357 krb5_cc_default(krb5_context context
,
360 const char *p
= krb5_cc_default_name(context
);
363 krb5_set_error_string(context
, "malloc - out of memory");
366 return krb5_cc_resolve(context
, p
, id
);
370 * Create a new ccache in `id' for `primary_principal'.
371 * Return 0 or an error code.
374 krb5_error_code KRB5_LIB_FUNCTION
375 krb5_cc_initialize(krb5_context context
,
377 krb5_principal primary_principal
)
379 return id
->ops
->init(context
, id
, primary_principal
);
384 * Remove the ccache `id'.
385 * Return 0 or an error code.
388 krb5_error_code KRB5_LIB_FUNCTION
389 krb5_cc_destroy(krb5_context context
,
394 ret
= id
->ops
->destroy(context
, id
);
395 krb5_cc_close (context
, id
);
400 * Stop using the ccache `id' and free the related resources.
401 * Return 0 or an error code.
404 krb5_error_code KRB5_LIB_FUNCTION
405 krb5_cc_close(krb5_context context
,
409 ret
= id
->ops
->close(context
, id
);
415 * Store `creds' in the ccache `id'.
416 * Return 0 or an error code.
419 krb5_error_code KRB5_LIB_FUNCTION
420 krb5_cc_store_cred(krb5_context context
,
424 return id
->ops
->store(context
, id
, creds
);
428 * Retrieve the credential identified by `mcreds' (and `whichfields')
429 * from `id' in `creds'.
430 * Return 0 or an error code.
433 krb5_error_code KRB5_LIB_FUNCTION
434 krb5_cc_retrieve_cred(krb5_context context
,
436 krb5_flags whichfields
,
437 const krb5_creds
*mcreds
,
441 krb5_cc_cursor cursor
;
443 if (id
->ops
->retrieve
!= NULL
) {
444 return id
->ops
->retrieve(context
, id
, whichfields
,
448 krb5_cc_start_seq_get(context
, id
, &cursor
);
449 while((ret
= krb5_cc_next_cred(context
, id
, &cursor
, creds
)) == 0){
450 if(krb5_compare_creds(context
, whichfields
, mcreds
, creds
)){
454 krb5_free_cred_contents (context
, creds
);
456 krb5_cc_end_seq_get(context
, id
, &cursor
);
461 * Return the principal of `id' in `principal'.
462 * Return 0 or an error code.
465 krb5_error_code KRB5_LIB_FUNCTION
466 krb5_cc_get_principal(krb5_context context
,
468 krb5_principal
*principal
)
470 return id
->ops
->get_princ(context
, id
, principal
);
474 * Start iterating over `id', `cursor' is initialized to the
476 * Return 0 or an error code.
479 krb5_error_code KRB5_LIB_FUNCTION
480 krb5_cc_start_seq_get (krb5_context context
,
481 const krb5_ccache id
,
482 krb5_cc_cursor
*cursor
)
484 return id
->ops
->get_first(context
, id
, cursor
);
488 * Retrieve the next cred pointed to by (`id', `cursor') in `creds'
489 * and advance `cursor'.
490 * Return 0 or an error code.
493 krb5_error_code KRB5_LIB_FUNCTION
494 krb5_cc_next_cred (krb5_context context
,
495 const krb5_ccache id
,
496 krb5_cc_cursor
*cursor
,
499 return id
->ops
->get_next(context
, id
, cursor
, creds
);
502 /* like krb5_cc_next_cred, but allow for selective retrieval */
504 krb5_error_code KRB5_LIB_FUNCTION
505 krb5_cc_next_cred_match(krb5_context context
,
506 const krb5_ccache id
,
507 krb5_cc_cursor
* cursor
,
509 krb5_flags whichfields
,
510 const krb5_creds
* mcreds
)
514 ret
= krb5_cc_next_cred(context
, id
, cursor
, creds
);
517 if (mcreds
== NULL
|| krb5_compare_creds(context
, whichfields
, mcreds
, creds
))
519 krb5_free_cred_contents(context
, creds
);
524 * Destroy the cursor `cursor'.
527 krb5_error_code KRB5_LIB_FUNCTION
528 krb5_cc_end_seq_get (krb5_context context
,
529 const krb5_ccache id
,
530 krb5_cc_cursor
*cursor
)
532 return id
->ops
->end_get(context
, id
, cursor
);
536 * Remove the credential identified by `cred', `which' from `id'.
539 krb5_error_code KRB5_LIB_FUNCTION
540 krb5_cc_remove_cred(krb5_context context
,
545 if(id
->ops
->remove_cred
== NULL
) {
546 krb5_set_error_string(context
,
547 "ccache %s does not support remove_cred",
549 return EACCES
; /* XXX */
551 return (*id
->ops
->remove_cred
)(context
, id
, which
, cred
);
555 * Set the flags of `id' to `flags'.
558 krb5_error_code KRB5_LIB_FUNCTION
559 krb5_cc_set_flags(krb5_context context
,
563 return id
->ops
->set_flags(context
, id
, flags
);
567 * Copy the contents of `from' to `to'.
570 krb5_error_code KRB5_LIB_FUNCTION
571 krb5_cc_copy_cache_match(krb5_context context
,
572 const krb5_ccache from
,
574 krb5_flags whichfields
,
575 const krb5_creds
* mcreds
,
576 unsigned int *matched
)
579 krb5_cc_cursor cursor
;
581 krb5_principal princ
;
583 ret
= krb5_cc_get_principal(context
, from
, &princ
);
586 ret
= krb5_cc_initialize(context
, to
, princ
);
588 krb5_free_principal(context
, princ
);
591 ret
= krb5_cc_start_seq_get(context
, from
, &cursor
);
593 krb5_free_principal(context
, princ
);
599 krb5_cc_next_cred_match(context
, from
, &cursor
, &cred
,
600 whichfields
, mcreds
) == 0) {
603 ret
= krb5_cc_store_cred(context
, to
, &cred
);
604 krb5_free_cred_contents(context
, &cred
);
606 krb5_cc_end_seq_get(context
, from
, &cursor
);
607 krb5_free_principal(context
, princ
);
611 krb5_error_code KRB5_LIB_FUNCTION
612 krb5_cc_copy_cache(krb5_context context
,
613 const krb5_ccache from
,
616 return krb5_cc_copy_cache_match(context
, from
, to
, 0, NULL
, NULL
);
620 * Return the version of `id'.
623 krb5_error_code KRB5_LIB_FUNCTION
624 krb5_cc_get_version(krb5_context context
,
625 const krb5_ccache id
)
627 if(id
->ops
->get_version
)
628 return id
->ops
->get_version(context
, id
);
634 * Clear `mcreds' so it can be used with krb5_cc_retrieve_cred
637 void KRB5_LIB_FUNCTION
638 krb5_cc_clear_mcred(krb5_creds
*mcred
)
640 memset(mcred
, 0, sizeof(*mcred
));
644 * Get the cc ops that is registered in `context' to handle the
645 * `prefix'. Returns NULL if ops not found.
649 krb5_cc_get_prefix_ops(krb5_context context
, const char *prefix
)
653 for(i
= 0; i
< context
->num_cc_ops
&& context
->cc_ops
[i
].prefix
; i
++) {
654 if(strcmp(context
->cc_ops
[i
].prefix
, prefix
) == 0)
655 return &context
->cc_ops
[i
];