cache.c revision 1.1.1.2.12.1 1 1.1.1.2.12.1 snj /* $NetBSD: cache.c,v 1.1.1.2.12.1 2017/08/20 05:44:21 snj Exp $ */
2 1.1 elric
3 1.1 elric /*
4 1.1 elric * Copyright (c) 1997 - 2008 Kungliga Tekniska Hgskolan
5 1.1 elric * (Royal Institute of Technology, Stockholm, Sweden).
6 1.1 elric * All rights reserved.
7 1.1 elric *
8 1.1 elric * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
9 1.1 elric *
10 1.1 elric * Redistribution and use in source and binary forms, with or without
11 1.1 elric * modification, are permitted provided that the following conditions
12 1.1 elric * are met:
13 1.1 elric *
14 1.1 elric * 1. Redistributions of source code must retain the above copyright
15 1.1 elric * notice, this list of conditions and the following disclaimer.
16 1.1 elric *
17 1.1 elric * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 elric * notice, this list of conditions and the following disclaimer in the
19 1.1 elric * documentation and/or other materials provided with the distribution.
20 1.1 elric *
21 1.1 elric * 3. Neither the name of the Institute nor the names of its contributors
22 1.1 elric * may be used to endorse or promote products derived from this software
23 1.1 elric * without specific prior written permission.
24 1.1 elric *
25 1.1 elric * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26 1.1 elric * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 1.1 elric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 1.1 elric * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29 1.1 elric * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 1.1 elric * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 1.1 elric * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 1.1 elric * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 1.1 elric * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 1.1 elric * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 1.1 elric * SUCH DAMAGE.
36 1.1 elric */
37 1.1 elric
38 1.1 elric #include "krb5_locl.h"
39 1.1 elric
40 1.1 elric /**
41 1.1 elric * @page krb5_ccache_intro The credential cache functions
42 1.1 elric * @section section_krb5_ccache Kerberos credential caches
43 1.1.1.2 pettai *
44 1.1 elric * krb5_ccache structure holds a Kerberos credential cache.
45 1.1 elric *
46 1.1 elric * Heimdal support the follow types of credential caches:
47 1.1 elric *
48 1.1 elric * - SCC
49 1.1 elric * Store the credential in a database
50 1.1 elric * - FILE
51 1.1 elric * Store the credential in memory
52 1.1 elric * - MEMORY
53 1.1 elric * Store the credential in memory
54 1.1 elric * - API
55 1.1 elric * A credential cache server based solution for Mac OS X
56 1.1 elric * - KCM
57 1.1 elric * A credential cache server based solution for all platforms
58 1.1 elric *
59 1.1 elric * @subsection Example
60 1.1 elric *
61 1.1 elric * This is a minimalistic version of klist:
62 1.1 elric @code
63 1.1 elric #include <krb5/krb5.h>
64 1.1 elric
65 1.1 elric int
66 1.1 elric main (int argc, char **argv)
67 1.1 elric {
68 1.1 elric krb5_context context;
69 1.1 elric krb5_cc_cursor cursor;
70 1.1 elric krb5_error_code ret;
71 1.1 elric krb5_ccache id;
72 1.1 elric krb5_creds creds;
73 1.1 elric
74 1.1 elric if (krb5_init_context (&context) != 0)
75 1.1 elric errx(1, "krb5_context");
76 1.1 elric
77 1.1 elric ret = krb5_cc_default (context, &id);
78 1.1 elric if (ret)
79 1.1 elric krb5_err(context, 1, ret, "krb5_cc_default");
80 1.1 elric
81 1.1 elric ret = krb5_cc_start_seq_get(context, id, &cursor);
82 1.1 elric if (ret)
83 1.1 elric krb5_err(context, 1, ret, "krb5_cc_start_seq_get");
84 1.1 elric
85 1.1 elric while((ret = krb5_cc_next_cred(context, id, &cursor, &creds)) == 0){
86 1.1 elric char *principal;
87 1.1 elric
88 1.1 elric krb5_unparse_name(context, creds.server, &principal);
89 1.1 elric printf("principal: %s\\n", principal);
90 1.1 elric free(principal);
91 1.1 elric krb5_free_cred_contents (context, &creds);
92 1.1 elric }
93 1.1 elric ret = krb5_cc_end_seq_get(context, id, &cursor);
94 1.1 elric if (ret)
95 1.1 elric krb5_err(context, 1, ret, "krb5_cc_end_seq_get");
96 1.1 elric
97 1.1 elric krb5_cc_close(context, id);
98 1.1 elric
99 1.1 elric krb5_free_context(context);
100 1.1 elric return 0;
101 1.1 elric }
102 1.1 elric * @endcode
103 1.1 elric */
104 1.1 elric
105 1.1 elric /**
106 1.1 elric * Add a new ccache type with operations `ops', overwriting any
107 1.1 elric * existing one if `override'.
108 1.1 elric *
109 1.1 elric * @param context a Keberos context
110 1.1 elric * @param ops type of plugin symbol
111 1.1 elric * @param override flag to select if the registration is to overide
112 1.1 elric * an existing ops with the same name.
113 1.1 elric *
114 1.1 elric * @return Return an error code or 0, see krb5_get_error_message().
115 1.1 elric *
116 1.1 elric * @ingroup krb5_ccache
117 1.1 elric */
118 1.1 elric
119 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
120 1.1 elric krb5_cc_register(krb5_context context,
121 1.1 elric const krb5_cc_ops *ops,
122 1.1 elric krb5_boolean override)
123 1.1 elric {
124 1.1 elric int i;
125 1.1 elric
126 1.1 elric for(i = 0; i < context->num_cc_ops && context->cc_ops[i]->prefix; i++) {
127 1.1 elric if(strcmp(context->cc_ops[i]->prefix, ops->prefix) == 0) {
128 1.1 elric if(!override) {
129 1.1 elric krb5_set_error_message(context,
130 1.1 elric KRB5_CC_TYPE_EXISTS,
131 1.1 elric N_("cache type %s already exists", "type"),
132 1.1 elric ops->prefix);
133 1.1 elric return KRB5_CC_TYPE_EXISTS;
134 1.1 elric }
135 1.1 elric break;
136 1.1 elric }
137 1.1 elric }
138 1.1 elric if(i == context->num_cc_ops) {
139 1.1 elric const krb5_cc_ops **o = realloc(rk_UNCONST(context->cc_ops),
140 1.1 elric (context->num_cc_ops + 1) *
141 1.1 elric sizeof(context->cc_ops[0]));
142 1.1 elric if(o == NULL) {
143 1.1 elric krb5_set_error_message(context, KRB5_CC_NOMEM,
144 1.1 elric N_("malloc: out of memory", ""));
145 1.1 elric return KRB5_CC_NOMEM;
146 1.1 elric }
147 1.1 elric context->cc_ops = o;
148 1.1 elric context->cc_ops[context->num_cc_ops] = NULL;
149 1.1 elric context->num_cc_ops++;
150 1.1 elric }
151 1.1 elric context->cc_ops[i] = ops;
152 1.1 elric return 0;
153 1.1 elric }
154 1.1 elric
155 1.1 elric /*
156 1.1 elric * Allocate the memory for a `id' and the that function table to
157 1.1 elric * `ops'. Returns 0 or and error code.
158 1.1 elric */
159 1.1 elric
160 1.1.1.2.12.1 snj KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
161 1.1 elric _krb5_cc_allocate(krb5_context context,
162 1.1 elric const krb5_cc_ops *ops,
163 1.1 elric krb5_ccache *id)
164 1.1 elric {
165 1.1 elric krb5_ccache p;
166 1.1 elric
167 1.1.1.2.12.1 snj p = calloc(1, sizeof(*p));
168 1.1.1.2.12.1 snj if (p == NULL) {
169 1.1 elric krb5_set_error_message(context, KRB5_CC_NOMEM,
170 1.1 elric N_("malloc: out of memory", ""));
171 1.1 elric return KRB5_CC_NOMEM;
172 1.1 elric }
173 1.1 elric p->ops = ops;
174 1.1 elric *id = p;
175 1.1 elric
176 1.1 elric return 0;
177 1.1 elric }
178 1.1 elric
179 1.1 elric /*
180 1.1 elric * Allocate memory for a new ccache in `id' with operations `ops'
181 1.1 elric * and name `residual'. Return 0 or an error code.
182 1.1 elric */
183 1.1 elric
184 1.1 elric static krb5_error_code
185 1.1 elric allocate_ccache (krb5_context context,
186 1.1 elric const krb5_cc_ops *ops,
187 1.1 elric const char *residual,
188 1.1 elric krb5_ccache *id)
189 1.1 elric {
190 1.1 elric krb5_error_code ret;
191 1.1 elric #ifdef KRB5_USE_PATH_TOKENS
192 1.1 elric char * exp_residual = NULL;
193 1.1.1.2.12.1 snj int filepath;
194 1.1.1.2.12.1 snj
195 1.1.1.2.12.1 snj filepath = (strcmp("FILE", ops->prefix) == 0
196 1.1.1.2.12.1 snj || strcmp("DIR", ops->prefix) == 0
197 1.1.1.2.12.1 snj || strcmp("SCC", ops->prefix) == 0);
198 1.1 elric
199 1.1.1.2.12.1 snj ret = _krb5_expand_path_tokens(context, residual, filepath, &exp_residual);
200 1.1 elric if (ret)
201 1.1 elric return ret;
202 1.1 elric
203 1.1 elric residual = exp_residual;
204 1.1 elric #endif
205 1.1 elric
206 1.1 elric ret = _krb5_cc_allocate(context, ops, id);
207 1.1 elric if (ret) {
208 1.1 elric #ifdef KRB5_USE_PATH_TOKENS
209 1.1 elric if (exp_residual)
210 1.1 elric free(exp_residual);
211 1.1 elric #endif
212 1.1 elric return ret;
213 1.1 elric }
214 1.1 elric
215 1.1 elric ret = (*id)->ops->resolve(context, id, residual);
216 1.1 elric if(ret) {
217 1.1 elric free(*id);
218 1.1 elric *id = NULL;
219 1.1 elric }
220 1.1 elric
221 1.1 elric #ifdef KRB5_USE_PATH_TOKENS
222 1.1 elric if (exp_residual)
223 1.1 elric free(exp_residual);
224 1.1 elric #endif
225 1.1 elric
226 1.1 elric return ret;
227 1.1 elric }
228 1.1 elric
229 1.1 elric static int
230 1.1 elric is_possible_path_name(const char * name)
231 1.1 elric {
232 1.1 elric const char * colon;
233 1.1 elric
234 1.1 elric if ((colon = strchr(name, ':')) == NULL)
235 1.1 elric return TRUE;
236 1.1 elric
237 1.1 elric #ifdef _WIN32
238 1.1 elric /* <drive letter>:\path\to\cache ? */
239 1.1 elric
240 1.1 elric if (colon == name + 1 &&
241 1.1 elric strchr(colon + 1, ':') == NULL)
242 1.1 elric return TRUE;
243 1.1 elric #endif
244 1.1 elric
245 1.1 elric return FALSE;
246 1.1 elric }
247 1.1 elric
248 1.1 elric /**
249 1.1 elric * Find and allocate a ccache in `id' from the specification in `residual'.
250 1.1 elric * If the ccache name doesn't contain any colon, interpret it as a file name.
251 1.1 elric *
252 1.1 elric * @param context a Keberos context.
253 1.1 elric * @param name string name of a credential cache.
254 1.1 elric * @param id return pointer to a found credential cache.
255 1.1 elric *
256 1.1 elric * @return Return 0 or an error code. In case of an error, id is set
257 1.1 elric * to NULL, see krb5_get_error_message().
258 1.1 elric *
259 1.1 elric * @ingroup krb5_ccache
260 1.1 elric */
261 1.1 elric
262 1.1 elric
263 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
264 1.1 elric krb5_cc_resolve(krb5_context context,
265 1.1 elric const char *name,
266 1.1 elric krb5_ccache *id)
267 1.1 elric {
268 1.1 elric int i;
269 1.1 elric
270 1.1 elric *id = NULL;
271 1.1 elric
272 1.1 elric for(i = 0; i < context->num_cc_ops && context->cc_ops[i]->prefix; i++) {
273 1.1 elric size_t prefix_len = strlen(context->cc_ops[i]->prefix);
274 1.1 elric
275 1.1 elric if(strncmp(context->cc_ops[i]->prefix, name, prefix_len) == 0
276 1.1 elric && name[prefix_len] == ':') {
277 1.1 elric return allocate_ccache (context, context->cc_ops[i],
278 1.1 elric name + prefix_len + 1,
279 1.1 elric id);
280 1.1 elric }
281 1.1 elric }
282 1.1 elric if (is_possible_path_name(name))
283 1.1 elric return allocate_ccache (context, &krb5_fcc_ops, name, id);
284 1.1 elric else {
285 1.1 elric krb5_set_error_message(context, KRB5_CC_UNKNOWN_TYPE,
286 1.1 elric N_("unknown ccache type %s", "name"), name);
287 1.1 elric return KRB5_CC_UNKNOWN_TYPE;
288 1.1 elric }
289 1.1 elric }
290 1.1 elric
291 1.1 elric /**
292 1.1 elric * Generates a new unique ccache of `type` in `id'. If `type' is NULL,
293 1.1 elric * the library chooses the default credential cache type. The supplied
294 1.1 elric * `hint' (that can be NULL) is a string that the credential cache
295 1.1 elric * type can use to base the name of the credential on, this is to make
296 1.1 elric * it easier for the user to differentiate the credentials.
297 1.1 elric *
298 1.1 elric * @return Return an error code or 0, see krb5_get_error_message().
299 1.1 elric *
300 1.1 elric * @ingroup krb5_ccache
301 1.1 elric */
302 1.1 elric
303 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
304 1.1 elric krb5_cc_new_unique(krb5_context context, const char *type,
305 1.1 elric const char *hint, krb5_ccache *id)
306 1.1 elric {
307 1.1 elric const krb5_cc_ops *ops;
308 1.1 elric krb5_error_code ret;
309 1.1 elric
310 1.1 elric ops = krb5_cc_get_prefix_ops(context, type);
311 1.1 elric if (ops == NULL) {
312 1.1 elric krb5_set_error_message(context, KRB5_CC_UNKNOWN_TYPE,
313 1.1 elric "Credential cache type %s is unknown", type);
314 1.1 elric return KRB5_CC_UNKNOWN_TYPE;
315 1.1 elric }
316 1.1 elric
317 1.1 elric ret = _krb5_cc_allocate(context, ops, id);
318 1.1 elric if (ret)
319 1.1 elric return ret;
320 1.1 elric ret = (*id)->ops->gen_new(context, id);
321 1.1 elric if (ret) {
322 1.1 elric free(*id);
323 1.1 elric *id = NULL;
324 1.1 elric }
325 1.1 elric return ret;
326 1.1 elric }
327 1.1 elric
328 1.1 elric /**
329 1.1 elric * Return the name of the ccache `id'
330 1.1 elric *
331 1.1 elric * @ingroup krb5_ccache
332 1.1 elric */
333 1.1 elric
334 1.1 elric
335 1.1 elric KRB5_LIB_FUNCTION const char* KRB5_LIB_CALL
336 1.1 elric krb5_cc_get_name(krb5_context context,
337 1.1 elric krb5_ccache id)
338 1.1 elric {
339 1.1 elric return id->ops->get_name(context, id);
340 1.1 elric }
341 1.1 elric
342 1.1 elric /**
343 1.1 elric * Return the type of the ccache `id'.
344 1.1 elric *
345 1.1 elric * @ingroup krb5_ccache
346 1.1 elric */
347 1.1 elric
348 1.1 elric
349 1.1 elric KRB5_LIB_FUNCTION const char* KRB5_LIB_CALL
350 1.1 elric krb5_cc_get_type(krb5_context context,
351 1.1 elric krb5_ccache id)
352 1.1 elric {
353 1.1 elric return id->ops->prefix;
354 1.1 elric }
355 1.1 elric
356 1.1 elric /**
357 1.1 elric * Return the complete resolvable name the cache
358 1.1 elric
359 1.1 elric * @param context a Keberos context
360 1.1 elric * @param id return pointer to a found credential cache
361 1.1 elric * @param str the returned name of a credential cache, free with krb5_xfree()
362 1.1 elric *
363 1.1 elric * @return Returns 0 or an error (and then *str is set to NULL).
364 1.1 elric *
365 1.1 elric * @ingroup krb5_ccache
366 1.1 elric */
367 1.1 elric
368 1.1 elric
369 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
370 1.1 elric krb5_cc_get_full_name(krb5_context context,
371 1.1 elric krb5_ccache id,
372 1.1 elric char **str)
373 1.1 elric {
374 1.1 elric const char *type, *name;
375 1.1 elric
376 1.1 elric *str = NULL;
377 1.1 elric
378 1.1 elric type = krb5_cc_get_type(context, id);
379 1.1 elric if (type == NULL) {
380 1.1 elric krb5_set_error_message(context, KRB5_CC_UNKNOWN_TYPE,
381 1.1 elric "cache have no name of type");
382 1.1 elric return KRB5_CC_UNKNOWN_TYPE;
383 1.1 elric }
384 1.1 elric
385 1.1 elric name = krb5_cc_get_name(context, id);
386 1.1 elric if (name == NULL) {
387 1.1 elric krb5_set_error_message(context, KRB5_CC_BADNAME,
388 1.1 elric "cache of type %s have no name", type);
389 1.1 elric return KRB5_CC_BADNAME;
390 1.1 elric }
391 1.1 elric
392 1.1 elric if (asprintf(str, "%s:%s", type, name) == -1) {
393 1.1 elric *str = NULL;
394 1.1.1.2.12.1 snj return krb5_enomem(context);
395 1.1 elric }
396 1.1 elric return 0;
397 1.1 elric }
398 1.1 elric
399 1.1 elric /**
400 1.1 elric * Return krb5_cc_ops of a the ccache `id'.
401 1.1 elric *
402 1.1 elric * @ingroup krb5_ccache
403 1.1 elric */
404 1.1 elric
405 1.1 elric
406 1.1 elric KRB5_LIB_FUNCTION const krb5_cc_ops * KRB5_LIB_CALL
407 1.1 elric krb5_cc_get_ops(krb5_context context, krb5_ccache id)
408 1.1 elric {
409 1.1 elric return id->ops;
410 1.1 elric }
411 1.1 elric
412 1.1 elric /*
413 1.1 elric * Expand variables in `str' into `res'
414 1.1 elric */
415 1.1 elric
416 1.1.1.2.12.1 snj KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
417 1.1 elric _krb5_expand_default_cc_name(krb5_context context, const char *str, char **res)
418 1.1 elric {
419 1.1.1.2.12.1 snj int filepath;
420 1.1.1.2.12.1 snj
421 1.1.1.2.12.1 snj filepath = (strncmp("FILE:", str, 5) == 0
422 1.1.1.2.12.1 snj || strncmp("DIR:", str, 4) == 0
423 1.1.1.2.12.1 snj || strncmp("SCC:", str, 4) == 0);
424 1.1.1.2.12.1 snj
425 1.1.1.2.12.1 snj return _krb5_expand_path_tokens(context, str, filepath, res);
426 1.1 elric }
427 1.1 elric
428 1.1 elric /*
429 1.1 elric * Return non-zero if envirnoment that will determine default krb5cc
430 1.1 elric * name has changed.
431 1.1 elric */
432 1.1 elric
433 1.1 elric static int
434 1.1 elric environment_changed(krb5_context context)
435 1.1 elric {
436 1.1 elric const char *e;
437 1.1 elric
438 1.1 elric /* if the cc name was set, don't change it */
439 1.1 elric if (context->default_cc_name_set)
440 1.1 elric return 0;
441 1.1 elric
442 1.1 elric /* XXX performance: always ask KCM/API if default name has changed */
443 1.1 elric if (context->default_cc_name &&
444 1.1 elric (strncmp(context->default_cc_name, "KCM:", 4) == 0 ||
445 1.1 elric strncmp(context->default_cc_name, "API:", 4) == 0))
446 1.1 elric return 1;
447 1.1 elric
448 1.1 elric if(issuid())
449 1.1 elric return 0;
450 1.1 elric
451 1.1 elric e = getenv("KRB5CCNAME");
452 1.1 elric if (e == NULL) {
453 1.1 elric if (context->default_cc_name_env) {
454 1.1 elric free(context->default_cc_name_env);
455 1.1 elric context->default_cc_name_env = NULL;
456 1.1 elric return 1;
457 1.1 elric }
458 1.1 elric } else {
459 1.1 elric if (context->default_cc_name_env == NULL)
460 1.1 elric return 1;
461 1.1 elric if (strcmp(e, context->default_cc_name_env) != 0)
462 1.1 elric return 1;
463 1.1 elric }
464 1.1 elric return 0;
465 1.1 elric }
466 1.1 elric
467 1.1 elric /**
468 1.1 elric * Switch the default default credential cache for a specific
469 1.1 elric * credcache type (and name for some implementations).
470 1.1 elric *
471 1.1 elric * @return Return an error code or 0, see krb5_get_error_message().
472 1.1 elric *
473 1.1 elric * @ingroup krb5_ccache
474 1.1 elric */
475 1.1 elric
476 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
477 1.1 elric krb5_cc_switch(krb5_context context, krb5_ccache id)
478 1.1 elric {
479 1.1.1.2 pettai #ifdef _WIN32
480 1.1.1.2 pettai _krb5_set_default_cc_name_to_registry(context, id);
481 1.1.1.2 pettai #endif
482 1.1 elric
483 1.1 elric if (id->ops->set_default == NULL)
484 1.1 elric return 0;
485 1.1 elric
486 1.1 elric return (*id->ops->set_default)(context, id);
487 1.1 elric }
488 1.1 elric
489 1.1 elric /**
490 1.1 elric * Return true if the default credential cache support switch
491 1.1 elric *
492 1.1 elric * @ingroup krb5_ccache
493 1.1 elric */
494 1.1 elric
495 1.1 elric KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
496 1.1 elric krb5_cc_support_switch(krb5_context context, const char *type)
497 1.1 elric {
498 1.1 elric const krb5_cc_ops *ops;
499 1.1 elric
500 1.1 elric ops = krb5_cc_get_prefix_ops(context, type);
501 1.1 elric if (ops && ops->set_default)
502 1.1 elric return 1;
503 1.1 elric return FALSE;
504 1.1 elric }
505 1.1 elric
506 1.1 elric /**
507 1.1 elric * Set the default cc name for `context' to `name'.
508 1.1 elric *
509 1.1 elric * @ingroup krb5_ccache
510 1.1 elric */
511 1.1 elric
512 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
513 1.1 elric krb5_cc_set_default_name(krb5_context context, const char *name)
514 1.1 elric {
515 1.1 elric krb5_error_code ret = 0;
516 1.1 elric char *p = NULL, *exp_p = NULL;
517 1.1.1.2.12.1 snj int filepath;
518 1.1.1.2.12.1 snj const krb5_cc_ops *ops = KRB5_DEFAULT_CCTYPE;
519 1.1 elric
520 1.1 elric if (name == NULL) {
521 1.1 elric const char *e = NULL;
522 1.1 elric
523 1.1.1.2.12.1 snj if (!issuid()) {
524 1.1 elric e = getenv("KRB5CCNAME");
525 1.1 elric if (e) {
526 1.1 elric p = strdup(e);
527 1.1 elric if (context->default_cc_name_env)
528 1.1 elric free(context->default_cc_name_env);
529 1.1 elric context->default_cc_name_env = strdup(e);
530 1.1 elric }
531 1.1 elric }
532 1.1 elric
533 1.1 elric #ifdef _WIN32
534 1.1.1.2.12.1 snj if (p == NULL) {
535 1.1.1.2.12.1 snj p = _krb5_get_default_cc_name_from_registry(context);
536 1.1 elric }
537 1.1 elric #endif
538 1.1.1.2.12.1 snj if (p == NULL) {
539 1.1 elric e = krb5_config_get_string(context, NULL, "libdefaults",
540 1.1 elric "default_cc_name", NULL);
541 1.1 elric if (e) {
542 1.1 elric ret = _krb5_expand_default_cc_name(context, e, &p);
543 1.1 elric if (ret)
544 1.1 elric return ret;
545 1.1 elric }
546 1.1.1.2.12.1 snj }
547 1.1.1.2.12.1 snj if (p == NULL) {
548 1.1.1.2.12.1 snj e = krb5_config_get_string(context, NULL, "libdefaults",
549 1.1.1.2.12.1 snj "default_cc_type", NULL);
550 1.1.1.2.12.1 snj if (e) {
551 1.1.1.2.12.1 snj ops = krb5_cc_get_prefix_ops(context, e);
552 1.1.1.2.12.1 snj if (ops == NULL) {
553 1.1.1.2.12.1 snj krb5_set_error_message(context,
554 1.1.1.2.12.1 snj KRB5_CC_UNKNOWN_TYPE,
555 1.1.1.2.12.1 snj "Credential cache type %s "
556 1.1.1.2.12.1 snj "is unknown", e);
557 1.1.1.2.12.1 snj return KRB5_CC_UNKNOWN_TYPE;
558 1.1 elric }
559 1.1 elric }
560 1.1 elric }
561 1.1.1.2.12.1 snj #ifdef _WIN32
562 1.1.1.2.12.1 snj if (p == NULL) {
563 1.1.1.2.12.1 snj /*
564 1.1.1.2.12.1 snj * If the MSLSA ccache type has a principal name,
565 1.1.1.2.12.1 snj * use it as the default.
566 1.1.1.2.12.1 snj */
567 1.1.1.2.12.1 snj krb5_ccache id;
568 1.1.1.2.12.1 snj ret = krb5_cc_resolve(context, "MSLSA:", &id);
569 1.1.1.2.12.1 snj if (ret == 0) {
570 1.1.1.2.12.1 snj krb5_principal princ;
571 1.1.1.2.12.1 snj ret = krb5_cc_get_principal(context, id, &princ);
572 1.1.1.2.12.1 snj if (ret == 0) {
573 1.1.1.2.12.1 snj krb5_free_principal(context, princ);
574 1.1.1.2.12.1 snj p = strdup("MSLSA:");
575 1.1.1.2.12.1 snj }
576 1.1.1.2.12.1 snj krb5_cc_close(context, id);
577 1.1.1.2.12.1 snj }
578 1.1.1.2.12.1 snj }
579 1.1.1.2.12.1 snj if (p == NULL) {
580 1.1.1.2.12.1 snj /*
581 1.1.1.2.12.1 snj * If the API:krb5cc ccache can be resolved,
582 1.1.1.2.12.1 snj * use it as the default.
583 1.1.1.2.12.1 snj */
584 1.1.1.2.12.1 snj krb5_ccache api_id;
585 1.1.1.2.12.1 snj ret = krb5_cc_resolve(context, "API:krb5cc", &api_id);
586 1.1.1.2.12.1 snj if (ret == 0)
587 1.1.1.2.12.1 snj krb5_cc_close(context, api_id);
588 1.1.1.2.12.1 snj }
589 1.1.1.2.12.1 snj /* Otherwise, fallback to the FILE ccache */
590 1.1.1.2.12.1 snj #endif
591 1.1.1.2.12.1 snj if (p == NULL) {
592 1.1.1.2.12.1 snj ret = (*ops->get_default_name)(context, &p);
593 1.1.1.2.12.1 snj if (ret)
594 1.1.1.2.12.1 snj return ret;
595 1.1.1.2.12.1 snj }
596 1.1 elric context->default_cc_name_set = 0;
597 1.1 elric } else {
598 1.1 elric p = strdup(name);
599 1.1.1.2.12.1 snj if (p == NULL)
600 1.1.1.2.12.1 snj return krb5_enomem(context);
601 1.1 elric context->default_cc_name_set = 1;
602 1.1 elric }
603 1.1 elric
604 1.1.1.2.12.1 snj filepath = (strncmp("FILE:", p, 5) == 0
605 1.1.1.2.12.1 snj || strncmp("DIR:", p, 4) == 0
606 1.1.1.2.12.1 snj || strncmp("SCC:", p, 4) == 0);
607 1.1 elric
608 1.1.1.2.12.1 snj ret = _krb5_expand_path_tokens(context, p, filepath, &exp_p);
609 1.1 elric free(p);
610 1.1.1.2.12.1 snj p = exp_p;
611 1.1 elric if (ret)
612 1.1 elric return ret;
613 1.1 elric
614 1.1 elric if (context->default_cc_name)
615 1.1 elric free(context->default_cc_name);
616 1.1 elric
617 1.1.1.2.12.1 snj context->default_cc_name = p;
618 1.1 elric
619 1.1 elric return 0;
620 1.1 elric }
621 1.1 elric
622 1.1 elric /**
623 1.1 elric * Return a pointer to a context static string containing the default
624 1.1 elric * ccache name.
625 1.1 elric *
626 1.1 elric * @return String to the default credential cache name.
627 1.1 elric *
628 1.1 elric * @ingroup krb5_ccache
629 1.1 elric */
630 1.1 elric
631 1.1 elric
632 1.1 elric KRB5_LIB_FUNCTION const char* KRB5_LIB_CALL
633 1.1 elric krb5_cc_default_name(krb5_context context)
634 1.1 elric {
635 1.1 elric if (context->default_cc_name == NULL || environment_changed(context))
636 1.1 elric krb5_cc_set_default_name(context, NULL);
637 1.1 elric
638 1.1 elric return context->default_cc_name;
639 1.1 elric }
640 1.1 elric
641 1.1 elric /**
642 1.1 elric * Open the default ccache in `id'.
643 1.1 elric *
644 1.1 elric * @return Return an error code or 0, see krb5_get_error_message().
645 1.1 elric *
646 1.1 elric * @ingroup krb5_ccache
647 1.1 elric */
648 1.1 elric
649 1.1 elric
650 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
651 1.1 elric krb5_cc_default(krb5_context context,
652 1.1 elric krb5_ccache *id)
653 1.1 elric {
654 1.1 elric const char *p = krb5_cc_default_name(context);
655 1.1 elric
656 1.1.1.2.12.1 snj if (p == NULL)
657 1.1.1.2.12.1 snj return krb5_enomem(context);
658 1.1 elric return krb5_cc_resolve(context, p, id);
659 1.1 elric }
660 1.1 elric
661 1.1 elric /**
662 1.1 elric * Create a new ccache in `id' for `primary_principal'.
663 1.1 elric *
664 1.1 elric * @return Return an error code or 0, see krb5_get_error_message().
665 1.1 elric *
666 1.1 elric * @ingroup krb5_ccache
667 1.1 elric */
668 1.1 elric
669 1.1 elric
670 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
671 1.1 elric krb5_cc_initialize(krb5_context context,
672 1.1 elric krb5_ccache id,
673 1.1 elric krb5_principal primary_principal)
674 1.1 elric {
675 1.1.1.2.12.1 snj krb5_error_code ret;
676 1.1.1.2.12.1 snj
677 1.1.1.2.12.1 snj ret = (*id->ops->init)(context, id, primary_principal);
678 1.1.1.2.12.1 snj if (ret == 0)
679 1.1.1.2.12.1 snj id->initialized = 1;
680 1.1.1.2.12.1 snj return ret;
681 1.1 elric }
682 1.1 elric
683 1.1 elric
684 1.1 elric /**
685 1.1 elric * Remove the ccache `id'.
686 1.1 elric *
687 1.1 elric * @return Return an error code or 0, see krb5_get_error_message().
688 1.1 elric *
689 1.1 elric * @ingroup krb5_ccache
690 1.1 elric */
691 1.1 elric
692 1.1 elric
693 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
694 1.1 elric krb5_cc_destroy(krb5_context context,
695 1.1 elric krb5_ccache id)
696 1.1 elric {
697 1.1 elric krb5_error_code ret;
698 1.1 elric
699 1.1 elric ret = (*id->ops->destroy)(context, id);
700 1.1 elric krb5_cc_close (context, id);
701 1.1 elric return ret;
702 1.1 elric }
703 1.1 elric
704 1.1 elric /**
705 1.1 elric * Stop using the ccache `id' and free the related resources.
706 1.1 elric *
707 1.1 elric * @return Return an error code or 0, see krb5_get_error_message().
708 1.1 elric *
709 1.1 elric * @ingroup krb5_ccache
710 1.1 elric */
711 1.1 elric
712 1.1 elric
713 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
714 1.1 elric krb5_cc_close(krb5_context context,
715 1.1 elric krb5_ccache id)
716 1.1 elric {
717 1.1 elric krb5_error_code ret;
718 1.1 elric ret = (*id->ops->close)(context, id);
719 1.1 elric free(id);
720 1.1 elric return ret;
721 1.1 elric }
722 1.1 elric
723 1.1 elric /**
724 1.1 elric * Store `creds' in the ccache `id'.
725 1.1 elric *
726 1.1 elric * @return Return an error code or 0, see krb5_get_error_message().
727 1.1 elric *
728 1.1 elric * @ingroup krb5_ccache
729 1.1 elric */
730 1.1 elric
731 1.1 elric
732 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
733 1.1 elric krb5_cc_store_cred(krb5_context context,
734 1.1 elric krb5_ccache id,
735 1.1 elric krb5_creds *creds)
736 1.1 elric {
737 1.1.1.2.12.1 snj krb5_error_code ret;
738 1.1.1.2.12.1 snj krb5_data realm;
739 1.1.1.2.12.1 snj
740 1.1.1.2.12.1 snj ret = (*id->ops->store)(context, id, creds);
741 1.1.1.2.12.1 snj
742 1.1.1.2.12.1 snj /* Look for and mark the first root TGT's realm as the start realm */
743 1.1.1.2.12.1 snj if (ret == 0 && id->initialized &&
744 1.1.1.2.12.1 snj krb5_principal_is_root_krbtgt(context, creds->server)) {
745 1.1.1.2.12.1 snj
746 1.1.1.2.12.1 snj id->initialized = 0;
747 1.1.1.2.12.1 snj realm.length = strlen(creds->server->realm);
748 1.1.1.2.12.1 snj realm.data = creds->server->realm;
749 1.1.1.2.12.1 snj (void) krb5_cc_set_config(context, id, NULL, "start_realm", &realm);
750 1.1.1.2.12.1 snj } else if (ret == 0 && id->initialized &&
751 1.1.1.2.12.1 snj krb5_is_config_principal(context, creds->server) &&
752 1.1.1.2.12.1 snj strcmp(creds->server->name.name_string.val[1], "start_realm") == 0) {
753 1.1.1.2.12.1 snj
754 1.1.1.2.12.1 snj /*
755 1.1.1.2.12.1 snj * But if the caller is storing a start_realm ccconfig, then
756 1.1.1.2.12.1 snj * stop looking for root TGTs to mark as the start_realm.
757 1.1.1.2.12.1 snj *
758 1.1.1.2.12.1 snj * By honoring any start_realm cc config stored, we interop
759 1.1.1.2.12.1 snj * both, with ccache implementations that don't preserve
760 1.1.1.2.12.1 snj * insertion order, and Kerberos implementations that store this
761 1.1.1.2.12.1 snj * cc config before the TGT.
762 1.1.1.2.12.1 snj */
763 1.1.1.2.12.1 snj id->initialized = 0;
764 1.1.1.2.12.1 snj }
765 1.1.1.2.12.1 snj return ret;
766 1.1 elric }
767 1.1 elric
768 1.1 elric /**
769 1.1 elric * Retrieve the credential identified by `mcreds' (and `whichfields')
770 1.1 elric * from `id' in `creds'. 'creds' must be free by the caller using
771 1.1 elric * krb5_free_cred_contents.
772 1.1 elric *
773 1.1 elric * @param context A Kerberos 5 context
774 1.1 elric * @param id a Kerberos 5 credential cache
775 1.1 elric * @param whichfields what fields to use for matching credentials, same
776 1.1 elric * flags as whichfields in krb5_compare_creds()
777 1.1 elric * @param mcreds template credential to use for comparing
778 1.1 elric * @param creds returned credential, free with krb5_free_cred_contents()
779 1.1 elric *
780 1.1 elric * @return Return an error code or 0, see krb5_get_error_message().
781 1.1 elric *
782 1.1 elric * @ingroup krb5_ccache
783 1.1 elric */
784 1.1 elric
785 1.1 elric
786 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
787 1.1 elric krb5_cc_retrieve_cred(krb5_context context,
788 1.1 elric krb5_ccache id,
789 1.1 elric krb5_flags whichfields,
790 1.1 elric const krb5_creds *mcreds,
791 1.1 elric krb5_creds *creds)
792 1.1 elric {
793 1.1 elric krb5_error_code ret;
794 1.1 elric krb5_cc_cursor cursor;
795 1.1 elric
796 1.1 elric if (id->ops->retrieve != NULL) {
797 1.1 elric return (*id->ops->retrieve)(context, id, whichfields,
798 1.1 elric mcreds, creds);
799 1.1 elric }
800 1.1 elric
801 1.1 elric ret = krb5_cc_start_seq_get(context, id, &cursor);
802 1.1 elric if (ret)
803 1.1 elric return ret;
804 1.1 elric while((ret = krb5_cc_next_cred(context, id, &cursor, creds)) == 0){
805 1.1 elric if(krb5_compare_creds(context, whichfields, mcreds, creds)){
806 1.1 elric ret = 0;
807 1.1 elric break;
808 1.1 elric }
809 1.1 elric krb5_free_cred_contents (context, creds);
810 1.1 elric }
811 1.1 elric krb5_cc_end_seq_get(context, id, &cursor);
812 1.1 elric return ret;
813 1.1 elric }
814 1.1 elric
815 1.1 elric /**
816 1.1 elric * Return the principal of `id' in `principal'.
817 1.1 elric *
818 1.1 elric * @return Return an error code or 0, see krb5_get_error_message().
819 1.1 elric *
820 1.1 elric * @ingroup krb5_ccache
821 1.1 elric */
822 1.1 elric
823 1.1 elric
824 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
825 1.1 elric krb5_cc_get_principal(krb5_context context,
826 1.1 elric krb5_ccache id,
827 1.1 elric krb5_principal *principal)
828 1.1 elric {
829 1.1 elric return (*id->ops->get_princ)(context, id, principal);
830 1.1 elric }
831 1.1 elric
832 1.1 elric /**
833 1.1 elric * Start iterating over `id', `cursor' is initialized to the
834 1.1 elric * beginning. Caller must free the cursor with krb5_cc_end_seq_get().
835 1.1 elric *
836 1.1 elric * @return Return an error code or 0, see krb5_get_error_message().
837 1.1 elric *
838 1.1 elric * @ingroup krb5_ccache
839 1.1 elric */
840 1.1 elric
841 1.1 elric
842 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
843 1.1 elric krb5_cc_start_seq_get (krb5_context context,
844 1.1 elric const krb5_ccache id,
845 1.1 elric krb5_cc_cursor *cursor)
846 1.1 elric {
847 1.1 elric return (*id->ops->get_first)(context, id, cursor);
848 1.1 elric }
849 1.1 elric
850 1.1 elric /**
851 1.1 elric * Retrieve the next cred pointed to by (`id', `cursor') in `creds'
852 1.1 elric * and advance `cursor'.
853 1.1 elric *
854 1.1 elric * @return Return an error code or 0, see krb5_get_error_message().
855 1.1 elric *
856 1.1 elric * @ingroup krb5_ccache
857 1.1 elric */
858 1.1 elric
859 1.1 elric
860 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
861 1.1 elric krb5_cc_next_cred (krb5_context context,
862 1.1 elric const krb5_ccache id,
863 1.1 elric krb5_cc_cursor *cursor,
864 1.1 elric krb5_creds *creds)
865 1.1 elric {
866 1.1 elric return (*id->ops->get_next)(context, id, cursor, creds);
867 1.1 elric }
868 1.1 elric
869 1.1 elric /**
870 1.1 elric * Destroy the cursor `cursor'.
871 1.1 elric *
872 1.1 elric * @ingroup krb5_ccache
873 1.1 elric */
874 1.1 elric
875 1.1 elric
876 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
877 1.1 elric krb5_cc_end_seq_get (krb5_context context,
878 1.1 elric const krb5_ccache id,
879 1.1 elric krb5_cc_cursor *cursor)
880 1.1 elric {
881 1.1 elric return (*id->ops->end_get)(context, id, cursor);
882 1.1 elric }
883 1.1 elric
884 1.1 elric /**
885 1.1 elric * Remove the credential identified by `cred', `which' from `id'.
886 1.1 elric *
887 1.1 elric * @ingroup krb5_ccache
888 1.1 elric */
889 1.1 elric
890 1.1 elric
891 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
892 1.1 elric krb5_cc_remove_cred(krb5_context context,
893 1.1 elric krb5_ccache id,
894 1.1 elric krb5_flags which,
895 1.1 elric krb5_creds *cred)
896 1.1 elric {
897 1.1 elric if(id->ops->remove_cred == NULL) {
898 1.1 elric krb5_set_error_message(context,
899 1.1 elric EACCES,
900 1.1 elric "ccache %s does not support remove_cred",
901 1.1 elric id->ops->prefix);
902 1.1 elric return EACCES; /* XXX */
903 1.1 elric }
904 1.1 elric return (*id->ops->remove_cred)(context, id, which, cred);
905 1.1 elric }
906 1.1 elric
907 1.1 elric /**
908 1.1 elric * Set the flags of `id' to `flags'.
909 1.1 elric *
910 1.1 elric * @ingroup krb5_ccache
911 1.1 elric */
912 1.1 elric
913 1.1 elric
914 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
915 1.1 elric krb5_cc_set_flags(krb5_context context,
916 1.1 elric krb5_ccache id,
917 1.1 elric krb5_flags flags)
918 1.1 elric {
919 1.1 elric return (*id->ops->set_flags)(context, id, flags);
920 1.1 elric }
921 1.1.1.2 pettai
922 1.1 elric /**
923 1.1 elric * Get the flags of `id', store them in `flags'.
924 1.1 elric *
925 1.1 elric * @ingroup krb5_ccache
926 1.1 elric */
927 1.1 elric
928 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
929 1.1 elric krb5_cc_get_flags(krb5_context context,
930 1.1 elric krb5_ccache id,
931 1.1 elric krb5_flags *flags)
932 1.1 elric {
933 1.1 elric *flags = 0;
934 1.1 elric return 0;
935 1.1 elric }
936 1.1 elric
937 1.1 elric /**
938 1.1 elric * Copy the contents of `from' to `to' if the given match function
939 1.1 elric * return true.
940 1.1 elric *
941 1.1 elric * @param context A Kerberos 5 context.
942 1.1 elric * @param from the cache to copy data from.
943 1.1 elric * @param to the cache to copy data to.
944 1.1 elric * @param match a match function that should return TRUE if cred argument should be copied, if NULL, all credentials are copied.
945 1.1 elric * @param matchctx context passed to match function.
946 1.1 elric * @param matched set to true if there was a credential that matched, may be NULL.
947 1.1 elric *
948 1.1 elric * @return Return an error code or 0, see krb5_get_error_message().
949 1.1 elric *
950 1.1 elric * @ingroup krb5_ccache
951 1.1 elric */
952 1.1 elric
953 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
954 1.1 elric krb5_cc_copy_match_f(krb5_context context,
955 1.1 elric const krb5_ccache from,
956 1.1 elric krb5_ccache to,
957 1.1 elric krb5_boolean (*match)(krb5_context, void *, const krb5_creds *),
958 1.1 elric void *matchctx,
959 1.1 elric unsigned int *matched)
960 1.1 elric {
961 1.1 elric krb5_error_code ret;
962 1.1 elric krb5_cc_cursor cursor;
963 1.1 elric krb5_creds cred;
964 1.1 elric krb5_principal princ;
965 1.1 elric
966 1.1 elric if (matched)
967 1.1 elric *matched = 0;
968 1.1 elric
969 1.1 elric ret = krb5_cc_get_principal(context, from, &princ);
970 1.1 elric if (ret)
971 1.1 elric return ret;
972 1.1 elric ret = krb5_cc_initialize(context, to, princ);
973 1.1 elric if (ret) {
974 1.1 elric krb5_free_principal(context, princ);
975 1.1 elric return ret;
976 1.1 elric }
977 1.1 elric ret = krb5_cc_start_seq_get(context, from, &cursor);
978 1.1 elric if (ret) {
979 1.1 elric krb5_free_principal(context, princ);
980 1.1 elric return ret;
981 1.1 elric }
982 1.1 elric
983 1.1 elric while ((ret = krb5_cc_next_cred(context, from, &cursor, &cred)) == 0) {
984 1.1.1.2.12.1 snj if (match == NULL || (*match)(context, matchctx, &cred)) {
985 1.1 elric if (matched)
986 1.1 elric (*matched)++;
987 1.1 elric ret = krb5_cc_store_cred(context, to, &cred);
988 1.1 elric if (ret)
989 1.1 elric break;
990 1.1 elric }
991 1.1 elric krb5_free_cred_contents(context, &cred);
992 1.1 elric }
993 1.1 elric krb5_cc_end_seq_get(context, from, &cursor);
994 1.1 elric krb5_free_principal(context, princ);
995 1.1 elric if (ret == KRB5_CC_END)
996 1.1 elric ret = 0;
997 1.1 elric return ret;
998 1.1 elric }
999 1.1 elric
1000 1.1 elric /**
1001 1.1 elric * Just like krb5_cc_copy_match_f(), but copy everything.
1002 1.1 elric *
1003 1.1 elric * @ingroup @krb5_ccache
1004 1.1 elric */
1005 1.1 elric
1006 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1007 1.1 elric krb5_cc_copy_cache(krb5_context context,
1008 1.1 elric const krb5_ccache from,
1009 1.1 elric krb5_ccache to)
1010 1.1 elric {
1011 1.1 elric return krb5_cc_copy_match_f(context, from, to, NULL, NULL, NULL);
1012 1.1 elric }
1013 1.1 elric
1014 1.1 elric /**
1015 1.1 elric * Return the version of `id'.
1016 1.1 elric *
1017 1.1 elric * @ingroup krb5_ccache
1018 1.1 elric */
1019 1.1 elric
1020 1.1 elric
1021 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1022 1.1 elric krb5_cc_get_version(krb5_context context,
1023 1.1 elric const krb5_ccache id)
1024 1.1 elric {
1025 1.1 elric if(id->ops->get_version)
1026 1.1 elric return (*id->ops->get_version)(context, id);
1027 1.1 elric else
1028 1.1 elric return 0;
1029 1.1 elric }
1030 1.1 elric
1031 1.1 elric /**
1032 1.1 elric * Clear `mcreds' so it can be used with krb5_cc_retrieve_cred
1033 1.1 elric *
1034 1.1 elric * @ingroup krb5_ccache
1035 1.1 elric */
1036 1.1 elric
1037 1.1 elric
1038 1.1 elric KRB5_LIB_FUNCTION void KRB5_LIB_CALL
1039 1.1 elric krb5_cc_clear_mcred(krb5_creds *mcred)
1040 1.1 elric {
1041 1.1 elric memset(mcred, 0, sizeof(*mcred));
1042 1.1 elric }
1043 1.1 elric
1044 1.1 elric /**
1045 1.1 elric * Get the cc ops that is registered in `context' to handle the
1046 1.1 elric * prefix. prefix can be a complete credential cache name or a
1047 1.1 elric * prefix, the function will only use part up to the first colon (:)
1048 1.1 elric * if there is one. If prefix the argument is NULL, the default ccache
1049 1.1 elric * implemtation is returned.
1050 1.1 elric *
1051 1.1 elric * @return Returns NULL if ops not found.
1052 1.1 elric *
1053 1.1 elric * @ingroup krb5_ccache
1054 1.1 elric */
1055 1.1 elric
1056 1.1 elric
1057 1.1 elric KRB5_LIB_FUNCTION const krb5_cc_ops * KRB5_LIB_CALL
1058 1.1 elric krb5_cc_get_prefix_ops(krb5_context context, const char *prefix)
1059 1.1 elric {
1060 1.1 elric char *p, *p1;
1061 1.1 elric int i;
1062 1.1 elric
1063 1.1 elric if (prefix == NULL)
1064 1.1 elric return KRB5_DEFAULT_CCTYPE;
1065 1.1.1.2.12.1 snj
1066 1.1.1.2.12.1 snj /* Is absolute path? Or UNC path? */
1067 1.1.1.2.12.1 snj if (ISPATHSEP(prefix[0]))
1068 1.1 elric return &krb5_fcc_ops;
1069 1.1 elric
1070 1.1.1.2.12.1 snj #ifdef _WIN32
1071 1.1.1.2.12.1 snj /* Is drive letter? */
1072 1.1.1.2.12.1 snj if (isalpha(prefix[0]) && prefix[1] == ':')
1073 1.1.1.2.12.1 snj return &krb5_fcc_ops;
1074 1.1.1.2.12.1 snj #endif
1075 1.1.1.2.12.1 snj
1076 1.1 elric p = strdup(prefix);
1077 1.1 elric if (p == NULL) {
1078 1.1.1.2.12.1 snj krb5_enomem(context);
1079 1.1 elric return NULL;
1080 1.1 elric }
1081 1.1 elric p1 = strchr(p, ':');
1082 1.1 elric if (p1)
1083 1.1 elric *p1 = '\0';
1084 1.1 elric
1085 1.1 elric for(i = 0; i < context->num_cc_ops && context->cc_ops[i]->prefix; i++) {
1086 1.1 elric if(strcmp(context->cc_ops[i]->prefix, p) == 0) {
1087 1.1 elric free(p);
1088 1.1 elric return context->cc_ops[i];
1089 1.1 elric }
1090 1.1 elric }
1091 1.1 elric free(p);
1092 1.1 elric return NULL;
1093 1.1 elric }
1094 1.1 elric
1095 1.1 elric struct krb5_cc_cache_cursor_data {
1096 1.1 elric const krb5_cc_ops *ops;
1097 1.1 elric krb5_cc_cursor cursor;
1098 1.1 elric };
1099 1.1 elric
1100 1.1 elric /**
1101 1.1 elric * Start iterating over all caches of specified type. See also
1102 1.1 elric * krb5_cccol_cursor_new().
1103 1.1 elric
1104 1.1 elric * @param context A Kerberos 5 context
1105 1.1 elric * @param type optional type to iterate over, if NULL, the default cache is used.
1106 1.1 elric * @param cursor cursor should be freed with krb5_cc_cache_end_seq_get().
1107 1.1 elric *
1108 1.1 elric * @return Return an error code or 0, see krb5_get_error_message().
1109 1.1 elric *
1110 1.1 elric * @ingroup krb5_ccache
1111 1.1 elric */
1112 1.1 elric
1113 1.1 elric
1114 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1115 1.1 elric krb5_cc_cache_get_first (krb5_context context,
1116 1.1 elric const char *type,
1117 1.1 elric krb5_cc_cache_cursor *cursor)
1118 1.1 elric {
1119 1.1 elric const krb5_cc_ops *ops;
1120 1.1 elric krb5_error_code ret;
1121 1.1 elric
1122 1.1 elric if (type == NULL)
1123 1.1 elric type = krb5_cc_default_name(context);
1124 1.1 elric
1125 1.1 elric ops = krb5_cc_get_prefix_ops(context, type);
1126 1.1 elric if (ops == NULL) {
1127 1.1 elric krb5_set_error_message(context, KRB5_CC_UNKNOWN_TYPE,
1128 1.1 elric "Unknown type \"%s\" when iterating "
1129 1.1 elric "trying to iterate the credential caches", type);
1130 1.1 elric return KRB5_CC_UNKNOWN_TYPE;
1131 1.1 elric }
1132 1.1 elric
1133 1.1 elric if (ops->get_cache_first == NULL) {
1134 1.1 elric krb5_set_error_message(context, KRB5_CC_NOSUPP,
1135 1.1 elric N_("Credential cache type %s doesn't support "
1136 1.1 elric "iterations over caches", "type"),
1137 1.1 elric ops->prefix);
1138 1.1 elric return KRB5_CC_NOSUPP;
1139 1.1 elric }
1140 1.1 elric
1141 1.1 elric *cursor = calloc(1, sizeof(**cursor));
1142 1.1.1.2.12.1 snj if (*cursor == NULL)
1143 1.1.1.2.12.1 snj return krb5_enomem(context);
1144 1.1 elric
1145 1.1 elric (*cursor)->ops = ops;
1146 1.1 elric
1147 1.1 elric ret = ops->get_cache_first(context, &(*cursor)->cursor);
1148 1.1 elric if (ret) {
1149 1.1 elric free(*cursor);
1150 1.1 elric *cursor = NULL;
1151 1.1 elric }
1152 1.1 elric return ret;
1153 1.1 elric }
1154 1.1 elric
1155 1.1 elric /**
1156 1.1 elric * Retrieve the next cache pointed to by (`cursor') in `id'
1157 1.1 elric * and advance `cursor'.
1158 1.1 elric *
1159 1.1 elric * @param context A Kerberos 5 context
1160 1.1 elric * @param cursor the iterator cursor, returned by krb5_cc_cache_get_first()
1161 1.1 elric * @param id next ccache
1162 1.1 elric *
1163 1.1 elric * @return Return 0 or an error code. Returns KRB5_CC_END when the end
1164 1.1 elric * of caches is reached, see krb5_get_error_message().
1165 1.1 elric *
1166 1.1 elric * @ingroup krb5_ccache
1167 1.1 elric */
1168 1.1 elric
1169 1.1 elric
1170 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1171 1.1 elric krb5_cc_cache_next (krb5_context context,
1172 1.1 elric krb5_cc_cache_cursor cursor,
1173 1.1 elric krb5_ccache *id)
1174 1.1 elric {
1175 1.1 elric return cursor->ops->get_cache_next(context, cursor->cursor, id);
1176 1.1 elric }
1177 1.1 elric
1178 1.1 elric /**
1179 1.1 elric * Destroy the cursor `cursor'.
1180 1.1 elric *
1181 1.1 elric * @return Return an error code or 0, see krb5_get_error_message().
1182 1.1 elric *
1183 1.1 elric * @ingroup krb5_ccache
1184 1.1 elric */
1185 1.1 elric
1186 1.1 elric
1187 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1188 1.1 elric krb5_cc_cache_end_seq_get (krb5_context context,
1189 1.1 elric krb5_cc_cache_cursor cursor)
1190 1.1 elric {
1191 1.1 elric krb5_error_code ret;
1192 1.1 elric ret = cursor->ops->end_cache_get(context, cursor->cursor);
1193 1.1 elric cursor->ops = NULL;
1194 1.1 elric free(cursor);
1195 1.1 elric return ret;
1196 1.1 elric }
1197 1.1 elric
1198 1.1 elric /**
1199 1.1 elric * Search for a matching credential cache that have the
1200 1.1 elric * `principal' as the default principal. On success, `id' needs to be
1201 1.1 elric * freed with krb5_cc_close() or krb5_cc_destroy().
1202 1.1 elric *
1203 1.1 elric * @param context A Kerberos 5 context
1204 1.1 elric * @param client The principal to search for
1205 1.1 elric * @param id the returned credential cache
1206 1.1 elric *
1207 1.1 elric * @return On failure, error code is returned and `id' is set to NULL.
1208 1.1 elric *
1209 1.1 elric * @ingroup krb5_ccache
1210 1.1 elric */
1211 1.1 elric
1212 1.1 elric
1213 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1214 1.1 elric krb5_cc_cache_match (krb5_context context,
1215 1.1 elric krb5_principal client,
1216 1.1 elric krb5_ccache *id)
1217 1.1 elric {
1218 1.1 elric krb5_cccol_cursor cursor;
1219 1.1 elric krb5_error_code ret;
1220 1.1 elric krb5_ccache cache = NULL;
1221 1.1.1.2.12.1 snj krb5_ccache expired_match = NULL;
1222 1.1 elric
1223 1.1 elric *id = NULL;
1224 1.1 elric
1225 1.1 elric ret = krb5_cccol_cursor_new (context, &cursor);
1226 1.1 elric if (ret)
1227 1.1 elric return ret;
1228 1.1 elric
1229 1.1.1.2.12.1 snj while (krb5_cccol_cursor_next(context, cursor, &cache) == 0 && cache != NULL) {
1230 1.1 elric krb5_principal principal;
1231 1.1.1.2.12.1 snj krb5_boolean match;
1232 1.1.1.2.12.1 snj time_t lifetime;
1233 1.1 elric
1234 1.1 elric ret = krb5_cc_get_principal(context, cache, &principal);
1235 1.1.1.2.12.1 snj if (ret)
1236 1.1.1.2.12.1 snj goto next;
1237 1.1.1.2 pettai
1238 1.1.1.2.12.1 snj if (client->name.name_string.len == 0)
1239 1.1.1.2.12.1 snj match = (strcmp(client->realm, principal->realm) == 0);
1240 1.1.1.2.12.1 snj else
1241 1.1 elric match = krb5_principal_compare(context, principal, client);
1242 1.1.1.2.12.1 snj krb5_free_principal(context, principal);
1243 1.1.1.2.12.1 snj
1244 1.1.1.2.12.1 snj if (!match)
1245 1.1.1.2.12.1 snj goto next;
1246 1.1.1.2.12.1 snj
1247 1.1.1.2.12.1 snj if (expired_match == NULL &&
1248 1.1.1.2.12.1 snj (krb5_cc_get_lifetime(context, cache, &lifetime) != 0 || lifetime == 0)) {
1249 1.1.1.2.12.1 snj expired_match = cache;
1250 1.1.1.2.12.1 snj cache = NULL;
1251 1.1.1.2.12.1 snj goto next;
1252 1.1 elric }
1253 1.1.1.2.12.1 snj break;
1254 1.1 elric
1255 1.1.1.2.12.1 snj next:
1256 1.1.1.2.12.1 snj if (cache)
1257 1.1.1.2.12.1 snj krb5_cc_close(context, cache);
1258 1.1 elric cache = NULL;
1259 1.1 elric }
1260 1.1 elric
1261 1.1 elric krb5_cccol_cursor_free(context, &cursor);
1262 1.1 elric
1263 1.1.1.2.12.1 snj if (cache == NULL && expired_match) {
1264 1.1.1.2.12.1 snj cache = expired_match;
1265 1.1.1.2.12.1 snj expired_match = NULL;
1266 1.1.1.2.12.1 snj } else if (expired_match) {
1267 1.1.1.2.12.1 snj krb5_cc_close(context, expired_match);
1268 1.1.1.2.12.1 snj } else if (cache == NULL) {
1269 1.1 elric char *str;
1270 1.1 elric
1271 1.1 elric krb5_unparse_name(context, client, &str);
1272 1.1 elric
1273 1.1 elric krb5_set_error_message(context, KRB5_CC_NOTFOUND,
1274 1.1 elric N_("Principal %s not found in any "
1275 1.1 elric "credential cache", ""),
1276 1.1 elric str ? str : "<out of memory>");
1277 1.1 elric if (str)
1278 1.1 elric free(str);
1279 1.1 elric return KRB5_CC_NOTFOUND;
1280 1.1 elric }
1281 1.1.1.2.12.1 snj
1282 1.1 elric *id = cache;
1283 1.1 elric
1284 1.1 elric return 0;
1285 1.1 elric }
1286 1.1 elric
1287 1.1 elric /**
1288 1.1 elric * Move the content from one credential cache to another. The
1289 1.1 elric * operation is an atomic switch.
1290 1.1 elric *
1291 1.1 elric * @param context a Keberos context
1292 1.1 elric * @param from the credential cache to move the content from
1293 1.1 elric * @param to the credential cache to move the content to
1294 1.1 elric
1295 1.1 elric * @return On sucess, from is freed. On failure, error code is
1296 1.1 elric * returned and from and to are both still allocated, see krb5_get_error_message().
1297 1.1 elric *
1298 1.1 elric * @ingroup krb5_ccache
1299 1.1 elric */
1300 1.1 elric
1301 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1302 1.1 elric krb5_cc_move(krb5_context context, krb5_ccache from, krb5_ccache to)
1303 1.1 elric {
1304 1.1 elric krb5_error_code ret;
1305 1.1 elric
1306 1.1 elric if (strcmp(from->ops->prefix, to->ops->prefix) != 0) {
1307 1.1 elric krb5_set_error_message(context, KRB5_CC_NOSUPP,
1308 1.1 elric N_("Moving credentials between diffrent "
1309 1.1 elric "types not yet supported", ""));
1310 1.1 elric return KRB5_CC_NOSUPP;
1311 1.1 elric }
1312 1.1 elric
1313 1.1 elric ret = (*to->ops->move)(context, from, to);
1314 1.1 elric if (ret == 0) {
1315 1.1 elric memset(from, 0, sizeof(*from));
1316 1.1 elric free(from);
1317 1.1 elric }
1318 1.1 elric return ret;
1319 1.1 elric }
1320 1.1 elric
1321 1.1 elric #define KRB5_CONF_NAME "krb5_ccache_conf_data"
1322 1.1 elric #define KRB5_REALM_NAME "X-CACHECONF:"
1323 1.1 elric
1324 1.1 elric static krb5_error_code
1325 1.1 elric build_conf_principals(krb5_context context, krb5_ccache id,
1326 1.1 elric krb5_const_principal principal,
1327 1.1 elric const char *name, krb5_creds *cred)
1328 1.1 elric {
1329 1.1 elric krb5_principal client;
1330 1.1 elric krb5_error_code ret;
1331 1.1 elric char *pname = NULL;
1332 1.1 elric
1333 1.1 elric memset(cred, 0, sizeof(*cred));
1334 1.1 elric
1335 1.1 elric ret = krb5_cc_get_principal(context, id, &client);
1336 1.1 elric if (ret)
1337 1.1 elric return ret;
1338 1.1 elric
1339 1.1 elric if (principal) {
1340 1.1 elric ret = krb5_unparse_name(context, principal, &pname);
1341 1.1 elric if (ret)
1342 1.1 elric return ret;
1343 1.1 elric }
1344 1.1 elric
1345 1.1 elric ret = krb5_make_principal(context, &cred->server,
1346 1.1 elric KRB5_REALM_NAME,
1347 1.1 elric KRB5_CONF_NAME, name, pname, NULL);
1348 1.1 elric free(pname);
1349 1.1 elric if (ret) {
1350 1.1 elric krb5_free_principal(context, client);
1351 1.1 elric return ret;
1352 1.1 elric }
1353 1.1 elric ret = krb5_copy_principal(context, client, &cred->client);
1354 1.1 elric krb5_free_principal(context, client);
1355 1.1 elric return ret;
1356 1.1 elric }
1357 1.1.1.2 pettai
1358 1.1 elric /**
1359 1.1 elric * Return TRUE (non zero) if the principal is a configuration
1360 1.1 elric * principal (generated part of krb5_cc_set_config()). Returns FALSE
1361 1.1 elric * (zero) if not a configuration principal.
1362 1.1 elric *
1363 1.1 elric * @param context a Keberos context
1364 1.1 elric * @param principal principal to check if it a configuration principal
1365 1.1 elric *
1366 1.1 elric * @ingroup krb5_ccache
1367 1.1 elric */
1368 1.1 elric
1369 1.1 elric KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
1370 1.1 elric krb5_is_config_principal(krb5_context context,
1371 1.1 elric krb5_const_principal principal)
1372 1.1 elric {
1373 1.1 elric if (strcmp(principal->realm, KRB5_REALM_NAME) != 0)
1374 1.1 elric return FALSE;
1375 1.1 elric
1376 1.1 elric if (principal->name.name_string.len == 0 ||
1377 1.1 elric strcmp(principal->name.name_string.val[0], KRB5_CONF_NAME) != 0)
1378 1.1 elric return FALSE;
1379 1.1.1.2 pettai
1380 1.1 elric return TRUE;
1381 1.1 elric }
1382 1.1 elric
1383 1.1 elric /**
1384 1.1 elric * Store some configuration for the credential cache in the cache.
1385 1.1 elric * Existing configuration under the same name is over-written.
1386 1.1 elric *
1387 1.1 elric * @param context a Keberos context
1388 1.1 elric * @param id the credential cache to store the data for
1389 1.1 elric * @param principal configuration for a specific principal, if
1390 1.1 elric * NULL, global for the whole cache.
1391 1.1 elric * @param name name under which the configuraion is stored.
1392 1.1 elric * @param data data to store, if NULL, configure is removed.
1393 1.1 elric *
1394 1.1 elric * @ingroup krb5_ccache
1395 1.1 elric */
1396 1.1 elric
1397 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1398 1.1 elric krb5_cc_set_config(krb5_context context, krb5_ccache id,
1399 1.1 elric krb5_const_principal principal,
1400 1.1 elric const char *name, krb5_data *data)
1401 1.1 elric {
1402 1.1 elric krb5_error_code ret;
1403 1.1 elric krb5_creds cred;
1404 1.1 elric
1405 1.1 elric ret = build_conf_principals(context, id, principal, name, &cred);
1406 1.1 elric if (ret)
1407 1.1 elric goto out;
1408 1.1 elric
1409 1.1 elric /* Remove old configuration */
1410 1.1 elric ret = krb5_cc_remove_cred(context, id, 0, &cred);
1411 1.1 elric if (ret && ret != KRB5_CC_NOTFOUND)
1412 1.1 elric goto out;
1413 1.1 elric
1414 1.1 elric if (data) {
1415 1.1 elric /* not that anyone care when this expire */
1416 1.1 elric cred.times.authtime = time(NULL);
1417 1.1 elric cred.times.endtime = cred.times.authtime + 3600 * 24 * 30;
1418 1.1.1.2 pettai
1419 1.1 elric ret = krb5_data_copy(&cred.ticket, data->data, data->length);
1420 1.1 elric if (ret)
1421 1.1 elric goto out;
1422 1.1.1.2 pettai
1423 1.1 elric ret = krb5_cc_store_cred(context, id, &cred);
1424 1.1 elric }
1425 1.1 elric
1426 1.1 elric out:
1427 1.1 elric krb5_free_cred_contents (context, &cred);
1428 1.1 elric return ret;
1429 1.1 elric }
1430 1.1 elric
1431 1.1 elric /**
1432 1.1 elric * Get some configuration for the credential cache in the cache.
1433 1.1 elric *
1434 1.1 elric * @param context a Keberos context
1435 1.1 elric * @param id the credential cache to store the data for
1436 1.1 elric * @param principal configuration for a specific principal, if
1437 1.1 elric * NULL, global for the whole cache.
1438 1.1 elric * @param name name under which the configuraion is stored.
1439 1.1 elric * @param data data to fetched, free with krb5_data_free()
1440 1.1 elric *
1441 1.1 elric * @ingroup krb5_ccache
1442 1.1 elric */
1443 1.1 elric
1444 1.1 elric
1445 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1446 1.1 elric krb5_cc_get_config(krb5_context context, krb5_ccache id,
1447 1.1 elric krb5_const_principal principal,
1448 1.1 elric const char *name, krb5_data *data)
1449 1.1 elric {
1450 1.1 elric krb5_creds mcred, cred;
1451 1.1 elric krb5_error_code ret;
1452 1.1 elric
1453 1.1 elric memset(&cred, 0, sizeof(cred));
1454 1.1 elric krb5_data_zero(data);
1455 1.1 elric
1456 1.1 elric ret = build_conf_principals(context, id, principal, name, &mcred);
1457 1.1 elric if (ret)
1458 1.1 elric goto out;
1459 1.1 elric
1460 1.1 elric ret = krb5_cc_retrieve_cred(context, id, 0, &mcred, &cred);
1461 1.1 elric if (ret)
1462 1.1 elric goto out;
1463 1.1 elric
1464 1.1 elric ret = krb5_data_copy(data, cred.ticket.data, cred.ticket.length);
1465 1.1 elric
1466 1.1 elric out:
1467 1.1 elric krb5_free_cred_contents (context, &cred);
1468 1.1 elric krb5_free_cred_contents (context, &mcred);
1469 1.1 elric return ret;
1470 1.1 elric }
1471 1.1 elric
1472 1.1 elric /*
1473 1.1 elric *
1474 1.1 elric */
1475 1.1 elric
1476 1.1 elric struct krb5_cccol_cursor_data {
1477 1.1 elric int idx;
1478 1.1 elric krb5_cc_cache_cursor cursor;
1479 1.1 elric };
1480 1.1 elric
1481 1.1 elric /**
1482 1.1 elric * Get a new cache interation cursor that will interate over all
1483 1.1 elric * credentials caches independent of type.
1484 1.1 elric *
1485 1.1 elric * @param context a Keberos context
1486 1.1 elric * @param cursor passed into krb5_cccol_cursor_next() and free with krb5_cccol_cursor_free().
1487 1.1 elric *
1488 1.1 elric * @return Returns 0 or and error code, see krb5_get_error_message().
1489 1.1 elric *
1490 1.1 elric * @ingroup krb5_ccache
1491 1.1 elric */
1492 1.1 elric
1493 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1494 1.1 elric krb5_cccol_cursor_new(krb5_context context, krb5_cccol_cursor *cursor)
1495 1.1 elric {
1496 1.1 elric *cursor = calloc(1, sizeof(**cursor));
1497 1.1.1.2.12.1 snj if (*cursor == NULL)
1498 1.1.1.2.12.1 snj return krb5_enomem(context);
1499 1.1 elric (*cursor)->idx = 0;
1500 1.1 elric (*cursor)->cursor = NULL;
1501 1.1 elric
1502 1.1 elric return 0;
1503 1.1 elric }
1504 1.1 elric
1505 1.1 elric /**
1506 1.1.1.2 pettai * Get next credential cache from the iteration.
1507 1.1 elric *
1508 1.1 elric * @param context A Kerberos 5 context
1509 1.1 elric * @param cursor the iteration cursor
1510 1.1 elric * @param cache the returned cursor, pointer is set to NULL on failure
1511 1.1 elric * and a cache on success. The returned cache needs to be freed
1512 1.1 elric * with krb5_cc_close() or destroyed with krb5_cc_destroy().
1513 1.1 elric * MIT Kerberos behavies slightly diffrent and sets cache to NULL
1514 1.1 elric * when all caches are iterated over and return 0.
1515 1.1 elric *
1516 1.1 elric * @return Return 0 or and error, KRB5_CC_END is returned at the end
1517 1.1 elric * of iteration. See krb5_get_error_message().
1518 1.1 elric *
1519 1.1 elric * @ingroup krb5_ccache
1520 1.1 elric */
1521 1.1 elric
1522 1.1 elric
1523 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1524 1.1 elric krb5_cccol_cursor_next(krb5_context context, krb5_cccol_cursor cursor,
1525 1.1 elric krb5_ccache *cache)
1526 1.1 elric {
1527 1.1 elric krb5_error_code ret;
1528 1.1.1.2 pettai
1529 1.1 elric *cache = NULL;
1530 1.1 elric
1531 1.1 elric while (cursor->idx < context->num_cc_ops) {
1532 1.1 elric
1533 1.1 elric if (cursor->cursor == NULL) {
1534 1.1.1.2 pettai ret = krb5_cc_cache_get_first (context,
1535 1.1 elric context->cc_ops[cursor->idx]->prefix,
1536 1.1 elric &cursor->cursor);
1537 1.1 elric if (ret) {
1538 1.1 elric cursor->idx++;
1539 1.1 elric continue;
1540 1.1 elric }
1541 1.1 elric }
1542 1.1 elric ret = krb5_cc_cache_next(context, cursor->cursor, cache);
1543 1.1 elric if (ret == 0)
1544 1.1 elric break;
1545 1.1 elric
1546 1.1 elric krb5_cc_cache_end_seq_get(context, cursor->cursor);
1547 1.1 elric cursor->cursor = NULL;
1548 1.1 elric if (ret != KRB5_CC_END)
1549 1.1 elric break;
1550 1.1 elric
1551 1.1 elric cursor->idx++;
1552 1.1 elric }
1553 1.1 elric if (cursor->idx >= context->num_cc_ops) {
1554 1.1 elric krb5_set_error_message(context, KRB5_CC_END,
1555 1.1 elric N_("Reached end of credential caches", ""));
1556 1.1 elric return KRB5_CC_END;
1557 1.1 elric }
1558 1.1 elric
1559 1.1 elric return 0;
1560 1.1 elric }
1561 1.1 elric
1562 1.1 elric /**
1563 1.1 elric * End an iteration and free all resources, can be done before end is reached.
1564 1.1 elric *
1565 1.1 elric * @param context A Kerberos 5 context
1566 1.1 elric * @param cursor the iteration cursor to be freed.
1567 1.1 elric *
1568 1.1 elric * @return Return 0 or and error, KRB5_CC_END is returned at the end
1569 1.1 elric * of iteration. See krb5_get_error_message().
1570 1.1 elric *
1571 1.1 elric * @ingroup krb5_ccache
1572 1.1 elric */
1573 1.1 elric
1574 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1575 1.1 elric krb5_cccol_cursor_free(krb5_context context, krb5_cccol_cursor *cursor)
1576 1.1 elric {
1577 1.1 elric krb5_cccol_cursor c = *cursor;
1578 1.1 elric
1579 1.1 elric *cursor = NULL;
1580 1.1 elric if (c) {
1581 1.1 elric if (c->cursor)
1582 1.1 elric krb5_cc_cache_end_seq_get(context, c->cursor);
1583 1.1 elric free(c);
1584 1.1 elric }
1585 1.1 elric return 0;
1586 1.1 elric }
1587 1.1 elric
1588 1.1 elric /**
1589 1.1 elric * Return the last time the credential cache was modified.
1590 1.1 elric *
1591 1.1 elric * @param context A Kerberos 5 context
1592 1.1 elric * @param id The credential cache to probe
1593 1.1 elric * @param mtime the last modification time, set to 0 on error.
1594 1.1 elric
1595 1.1 elric * @return Return 0 or and error. See krb5_get_error_message().
1596 1.1 elric *
1597 1.1 elric * @ingroup krb5_ccache
1598 1.1 elric */
1599 1.1 elric
1600 1.1 elric
1601 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1602 1.1 elric krb5_cc_last_change_time(krb5_context context,
1603 1.1.1.2 pettai krb5_ccache id,
1604 1.1 elric krb5_timestamp *mtime)
1605 1.1 elric {
1606 1.1 elric *mtime = 0;
1607 1.1 elric return (*id->ops->lastchange)(context, id, mtime);
1608 1.1 elric }
1609 1.1 elric
1610 1.1 elric /**
1611 1.1 elric * Return the last modfication time for a cache collection. The query
1612 1.1 elric * can be limited to a specific cache type. If the function return 0
1613 1.1 elric * and mtime is 0, there was no credentials in the caches.
1614 1.1 elric *
1615 1.1 elric * @param context A Kerberos 5 context
1616 1.1 elric * @param type The credential cache to probe, if NULL, all type are traversed.
1617 1.1 elric * @param mtime the last modification time, set to 0 on error.
1618 1.1 elric
1619 1.1 elric * @return Return 0 or and error. See krb5_get_error_message().
1620 1.1 elric *
1621 1.1 elric * @ingroup krb5_ccache
1622 1.1 elric */
1623 1.1 elric
1624 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1625 1.1 elric krb5_cccol_last_change_time(krb5_context context,
1626 1.1 elric const char *type,
1627 1.1 elric krb5_timestamp *mtime)
1628 1.1 elric {
1629 1.1 elric krb5_cccol_cursor cursor;
1630 1.1 elric krb5_error_code ret;
1631 1.1 elric krb5_ccache id;
1632 1.1 elric krb5_timestamp t = 0;
1633 1.1 elric
1634 1.1 elric *mtime = 0;
1635 1.1 elric
1636 1.1 elric ret = krb5_cccol_cursor_new (context, &cursor);
1637 1.1 elric if (ret)
1638 1.1 elric return ret;
1639 1.1 elric
1640 1.1 elric while (krb5_cccol_cursor_next(context, cursor, &id) == 0 && id != NULL) {
1641 1.1 elric
1642 1.1 elric if (type && strcmp(krb5_cc_get_type(context, id), type) != 0)
1643 1.1 elric continue;
1644 1.1 elric
1645 1.1 elric ret = krb5_cc_last_change_time(context, id, &t);
1646 1.1 elric krb5_cc_close(context, id);
1647 1.1 elric if (ret)
1648 1.1 elric continue;
1649 1.1 elric if (t > *mtime)
1650 1.1 elric *mtime = t;
1651 1.1 elric }
1652 1.1 elric
1653 1.1 elric krb5_cccol_cursor_free(context, &cursor);
1654 1.1 elric
1655 1.1 elric return 0;
1656 1.1 elric }
1657 1.1 elric /**
1658 1.1 elric * Return a friendly name on credential cache. Free the result with krb5_xfree().
1659 1.1 elric *
1660 1.1 elric * @return Return an error code or 0, see krb5_get_error_message().
1661 1.1 elric *
1662 1.1 elric * @ingroup krb5_ccache
1663 1.1 elric */
1664 1.1 elric
1665 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1666 1.1 elric krb5_cc_get_friendly_name(krb5_context context,
1667 1.1 elric krb5_ccache id,
1668 1.1 elric char **name)
1669 1.1 elric {
1670 1.1 elric krb5_error_code ret;
1671 1.1 elric krb5_data data;
1672 1.1 elric
1673 1.1 elric ret = krb5_cc_get_config(context, id, NULL, "FriendlyName", &data);
1674 1.1 elric if (ret) {
1675 1.1 elric krb5_principal principal;
1676 1.1 elric ret = krb5_cc_get_principal(context, id, &principal);
1677 1.1 elric if (ret)
1678 1.1 elric return ret;
1679 1.1 elric ret = krb5_unparse_name(context, principal, name);
1680 1.1 elric krb5_free_principal(context, principal);
1681 1.1 elric } else {
1682 1.1 elric ret = asprintf(name, "%.*s", (int)data.length, (char *)data.data);
1683 1.1 elric krb5_data_free(&data);
1684 1.1.1.2.12.1 snj if (ret <= 0)
1685 1.1.1.2.12.1 snj ret = krb5_enomem(context);
1686 1.1.1.2.12.1 snj else
1687 1.1 elric ret = 0;
1688 1.1 elric }
1689 1.1 elric
1690 1.1 elric return ret;
1691 1.1 elric }
1692 1.1 elric
1693 1.1 elric /**
1694 1.1 elric * Set the friendly name on credential cache.
1695 1.1 elric *
1696 1.1 elric * @return Return an error code or 0, see krb5_get_error_message().
1697 1.1 elric *
1698 1.1 elric * @ingroup krb5_ccache
1699 1.1 elric */
1700 1.1 elric
1701 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1702 1.1 elric krb5_cc_set_friendly_name(krb5_context context,
1703 1.1 elric krb5_ccache id,
1704 1.1 elric const char *name)
1705 1.1 elric {
1706 1.1 elric krb5_data data;
1707 1.1 elric
1708 1.1 elric data.data = rk_UNCONST(name);
1709 1.1 elric data.length = strlen(name);
1710 1.1 elric
1711 1.1 elric return krb5_cc_set_config(context, id, NULL, "FriendlyName", &data);
1712 1.1 elric }
1713 1.1 elric
1714 1.1 elric /**
1715 1.1 elric * Get the lifetime of the initial ticket in the cache
1716 1.1 elric *
1717 1.1 elric * Get the lifetime of the initial ticket in the cache, if the initial
1718 1.1 elric * ticket was not found, the error code KRB5_CC_END is returned.
1719 1.1 elric *
1720 1.1 elric * @param context A Kerberos 5 context.
1721 1.1 elric * @param id a credential cache
1722 1.1 elric * @param t the relative lifetime of the initial ticket
1723 1.1 elric *
1724 1.1 elric * @return Return an error code or 0, see krb5_get_error_message().
1725 1.1 elric *
1726 1.1 elric * @ingroup krb5_ccache
1727 1.1 elric */
1728 1.1 elric
1729 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1730 1.1 elric krb5_cc_get_lifetime(krb5_context context, krb5_ccache id, time_t *t)
1731 1.1 elric {
1732 1.1.1.2.12.1 snj krb5_data config_start_realm;
1733 1.1.1.2.12.1 snj char *start_realm;
1734 1.1 elric krb5_cc_cursor cursor;
1735 1.1 elric krb5_error_code ret;
1736 1.1 elric krb5_creds cred;
1737 1.1.1.2.12.1 snj time_t now, endtime = 0;
1738 1.1 elric
1739 1.1 elric *t = 0;
1740 1.1.1.2.12.1 snj krb5_timeofday(context, &now);
1741 1.1.1.2.12.1 snj
1742 1.1.1.2.12.1 snj ret = krb5_cc_get_config(context, id, NULL, "start_realm", &config_start_realm);
1743 1.1.1.2.12.1 snj if (ret == 0) {
1744 1.1.1.2.12.1 snj start_realm = strndup(config_start_realm.data, config_start_realm.length);
1745 1.1.1.2.12.1 snj krb5_data_free(&config_start_realm);
1746 1.1.1.2.12.1 snj } else {
1747 1.1.1.2.12.1 snj krb5_principal client;
1748 1.1.1.2.12.1 snj
1749 1.1.1.2.12.1 snj ret = krb5_cc_get_principal(context, id, &client);
1750 1.1.1.2.12.1 snj if (ret)
1751 1.1.1.2.12.1 snj return ret;
1752 1.1.1.2.12.1 snj start_realm = strdup(krb5_principal_get_realm(context, client));
1753 1.1.1.2.12.1 snj krb5_free_principal(context, client);
1754 1.1.1.2.12.1 snj }
1755 1.1.1.2.12.1 snj if (start_realm == NULL)
1756 1.1.1.2.12.1 snj return krb5_enomem(context);
1757 1.1.1.2 pettai
1758 1.1 elric ret = krb5_cc_start_seq_get(context, id, &cursor);
1759 1.1.1.2.12.1 snj if (ret) {
1760 1.1.1.2.12.1 snj free(start_realm);
1761 1.1 elric return ret;
1762 1.1.1.2.12.1 snj }
1763 1.1 elric
1764 1.1 elric while ((ret = krb5_cc_next_cred(context, id, &cursor, &cred)) == 0) {
1765 1.1.1.2.12.1 snj /**
1766 1.1.1.2.12.1 snj * If we find the start krbtgt in the cache, use that as the lifespan.
1767 1.1.1.2.12.1 snj */
1768 1.1.1.2.12.1 snj if (krb5_principal_is_root_krbtgt(context, cred.server) &&
1769 1.1.1.2.12.1 snj strcmp(cred.server->realm, start_realm) == 0) {
1770 1.1 elric if (now < cred.times.endtime)
1771 1.1.1.2.12.1 snj endtime = cred.times.endtime;
1772 1.1 elric krb5_free_cred_contents(context, &cred);
1773 1.1 elric break;
1774 1.1 elric }
1775 1.1.1.2.12.1 snj /*
1776 1.1.1.2.12.1 snj * Skip config entries
1777 1.1.1.2.12.1 snj */
1778 1.1.1.2.12.1 snj if (krb5_is_config_principal(context, cred.server)) {
1779 1.1.1.2.12.1 snj krb5_free_cred_contents(context, &cred);
1780 1.1.1.2.12.1 snj continue;
1781 1.1.1.2.12.1 snj }
1782 1.1.1.2.12.1 snj /**
1783 1.1.1.2.12.1 snj * If there was no krbtgt, use the shortest lifetime of
1784 1.1.1.2.12.1 snj * service tickets that have yet to expire. If all
1785 1.1.1.2.12.1 snj * credentials are expired, krb5_cc_get_lifetime() will fail.
1786 1.1.1.2.12.1 snj */
1787 1.1.1.2.12.1 snj if ((endtime == 0 || cred.times.endtime < endtime) && now < cred.times.endtime)
1788 1.1.1.2.12.1 snj endtime = cred.times.endtime;
1789 1.1 elric krb5_free_cred_contents(context, &cred);
1790 1.1 elric }
1791 1.1.1.2.12.1 snj free(start_realm);
1792 1.1.1.2.12.1 snj
1793 1.1.1.2.12.1 snj /* if we found an endtime use that */
1794 1.1.1.2.12.1 snj if (endtime) {
1795 1.1.1.2.12.1 snj *t = endtime - now;
1796 1.1.1.2.12.1 snj ret = 0;
1797 1.1.1.2.12.1 snj }
1798 1.1.1.2 pettai
1799 1.1 elric krb5_cc_end_seq_get(context, id, &cursor);
1800 1.1 elric
1801 1.1 elric return ret;
1802 1.1 elric }
1803 1.1 elric
1804 1.1 elric /**
1805 1.1 elric * Set the time offset betwen the client and the KDC
1806 1.1 elric *
1807 1.1 elric * If the backend doesn't support KDC offset, use the context global setting.
1808 1.1 elric *
1809 1.1 elric * @param context A Kerberos 5 context.
1810 1.1 elric * @param id a credential cache
1811 1.1 elric * @param offset the offset in seconds
1812 1.1 elric *
1813 1.1 elric * @return Return an error code or 0, see krb5_get_error_message().
1814 1.1 elric *
1815 1.1 elric * @ingroup krb5_ccache
1816 1.1 elric */
1817 1.1 elric
1818 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1819 1.1 elric krb5_cc_set_kdc_offset(krb5_context context, krb5_ccache id, krb5_deltat offset)
1820 1.1 elric {
1821 1.1 elric if (id->ops->set_kdc_offset == NULL) {
1822 1.1 elric context->kdc_sec_offset = offset;
1823 1.1 elric context->kdc_usec_offset = 0;
1824 1.1 elric return 0;
1825 1.1 elric }
1826 1.1 elric return (*id->ops->set_kdc_offset)(context, id, offset);
1827 1.1 elric }
1828 1.1 elric
1829 1.1 elric /**
1830 1.1 elric * Get the time offset betwen the client and the KDC
1831 1.1 elric *
1832 1.1 elric * If the backend doesn't support KDC offset, use the context global setting.
1833 1.1 elric *
1834 1.1 elric * @param context A Kerberos 5 context.
1835 1.1 elric * @param id a credential cache
1836 1.1 elric * @param offset the offset in seconds
1837 1.1 elric *
1838 1.1 elric * @return Return an error code or 0, see krb5_get_error_message().
1839 1.1 elric *
1840 1.1 elric * @ingroup krb5_ccache
1841 1.1 elric */
1842 1.1 elric
1843 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1844 1.1 elric krb5_cc_get_kdc_offset(krb5_context context, krb5_ccache id, krb5_deltat *offset)
1845 1.1 elric {
1846 1.1 elric if (id->ops->get_kdc_offset == NULL) {
1847 1.1 elric *offset = context->kdc_sec_offset;
1848 1.1 elric return 0;
1849 1.1 elric }
1850 1.1 elric return (*id->ops->get_kdc_offset)(context, id, offset);
1851 1.1 elric }
1852 1.1 elric
1853 1.1 elric #ifdef _WIN32
1854 1.1.1.2 pettai #define REGPATH_MIT_KRB5 "SOFTWARE\\MIT\\Kerberos5"
1855 1.1.1.2.12.1 snj
1856 1.1.1.2.12.1 snj static char *
1857 1.1.1.2.12.1 snj _get_default_cc_name_from_registry(krb5_context context, HKEY hkBase)
1858 1.1 elric {
1859 1.1 elric HKEY hk_k5 = 0;
1860 1.1 elric LONG code;
1861 1.1.1.2.12.1 snj char *ccname = NULL;
1862 1.1 elric
1863 1.1.1.2.12.1 snj code = RegOpenKeyEx(hkBase,
1864 1.1.1.2 pettai REGPATH_MIT_KRB5,
1865 1.1 elric 0, KEY_READ, &hk_k5);
1866 1.1 elric
1867 1.1 elric if (code != ERROR_SUCCESS)
1868 1.1 elric return NULL;
1869 1.1 elric
1870 1.1.1.2 pettai ccname = _krb5_parse_reg_value_as_string(context, hk_k5, "ccname",
1871 1.1 elric REG_NONE, 0);
1872 1.1 elric
1873 1.1 elric RegCloseKey(hk_k5);
1874 1.1 elric
1875 1.1 elric return ccname;
1876 1.1 elric }
1877 1.1 elric
1878 1.1.1.2.12.1 snj KRB5_LIB_FUNCTION char * KRB5_LIB_CALL
1879 1.1.1.2.12.1 snj _krb5_get_default_cc_name_from_registry(krb5_context context)
1880 1.1.1.2.12.1 snj {
1881 1.1.1.2.12.1 snj char *ccname;
1882 1.1.1.2.12.1 snj
1883 1.1.1.2.12.1 snj ccname = _get_default_cc_name_from_registry(context, HKEY_CURRENT_USER);
1884 1.1.1.2.12.1 snj if (ccname == NULL)
1885 1.1.1.2.12.1 snj ccname = _get_default_cc_name_from_registry(context,
1886 1.1.1.2.12.1 snj HKEY_LOCAL_MACHINE);
1887 1.1.1.2.12.1 snj
1888 1.1.1.2.12.1 snj return ccname;
1889 1.1.1.2.12.1 snj }
1890 1.1.1.2.12.1 snj
1891 1.1.1.2.12.1 snj KRB5_LIB_FUNCTION int KRB5_LIB_CALL
1892 1.1.1.2 pettai _krb5_set_default_cc_name_to_registry(krb5_context context, krb5_ccache id)
1893 1.1.1.2 pettai {
1894 1.1.1.2 pettai HKEY hk_k5 = 0;
1895 1.1.1.2 pettai LONG code;
1896 1.1.1.2 pettai int ret = -1;
1897 1.1.1.2 pettai char * ccname = NULL;
1898 1.1.1.2 pettai
1899 1.1.1.2 pettai code = RegOpenKeyEx(HKEY_CURRENT_USER,
1900 1.1.1.2 pettai REGPATH_MIT_KRB5,
1901 1.1.1.2 pettai 0, KEY_READ|KEY_WRITE, &hk_k5);
1902 1.1.1.2 pettai
1903 1.1.1.2 pettai if (code != ERROR_SUCCESS)
1904 1.1.1.2 pettai return -1;
1905 1.1.1.2 pettai
1906 1.1.1.2 pettai ret = asprintf(&ccname, "%s:%s", krb5_cc_get_type(context, id), krb5_cc_get_name(context, id));
1907 1.1.1.2 pettai if (ret < 0)
1908 1.1.1.2 pettai goto cleanup;
1909 1.1.1.2 pettai
1910 1.1.1.2 pettai ret = _krb5_store_string_to_reg_value(context, hk_k5, "ccname",
1911 1.1.1.2 pettai REG_SZ, ccname, -1, 0);
1912 1.1.1.2 pettai
1913 1.1.1.2 pettai cleanup:
1914 1.1.1.2 pettai
1915 1.1.1.2 pettai if (ccname)
1916 1.1.1.2 pettai free(ccname);
1917 1.1.1.2 pettai
1918 1.1.1.2 pettai RegCloseKey(hk_k5);
1919 1.1.1.2 pettai
1920 1.1.1.2 pettai return ret;
1921 1.1.1.2 pettai }
1922 1.1 elric #endif
1923