Debian Codegraph @

lib/gssapi/krb5/acquire_cred.c

raw
1 /*
2 * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
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.
16 *
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.
20 *
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
31 * SUCH DAMAGE.
32 */
33
34 #include "gsskrb5_locl.h"
35
36 OM_uint32
37 __gsskrb5_ccache_lifetime(OM_uint32 *minor_status,
38 krb5_context context,
39 krb5_ccache id,
40 krb5_principal principal,
41 OM_uint32 *lifetime)
42 {
43 krb5_error_code kret;
44 time_t left;
45
46 kret = krb5_cc_get_lifetime(context, id, &left);
47 if (kret) {
48 *minor_status = kret;
49 return GSS_S_FAILURE;
50 }
51
52 *lifetime = left;
53
54 return GSS_S_COMPLETE;
55 }
56
57
58
59
60 static krb5_error_code
61 get_keytab(krb5_context context, krb5_keytab *keytab)
62 {
63 krb5_error_code kret;
64
65 HEIMDAL_MUTEX_lock(&gssapi_keytab_mutex);
66
67 if (_gsskrb5_keytab != NULL) {
68 char *name = NULL;
69
70 kret = krb5_kt_get_full_name(context, _gsskrb5_keytab, &name);
71 if (kret == 0) {
72 kret = krb5_kt_resolve(context, name, keytab);
73 krb5_xfree(name);
74 }
75 } else
76 kret = krb5_kt_default(context, keytab);
77
78 HEIMDAL_MUTEX_unlock(&gssapi_keytab_mutex);
79
80 return (kret);
81 }
82
83 /*
84 * This function produces a cred with a MEMORY ccache containing a TGT
85 * acquired with a password.
86 */
87 static OM_uint32
88 acquire_cred_with_password(OM_uint32 *minor_status,
89 krb5_context context,
90 const char *password,
91 OM_uint32 time_req,
92 gss_const_OID desired_mech,
93 gss_cred_usage_t cred_usage,
94 gsskrb5_cred handle)
95 {
96 OM_uint32 ret = GSS_S_FAILURE;
97 krb5_creds cred;
98 krb5_get_init_creds_opt *opt;
99 krb5_ccache ccache = NULL;
100 krb5_error_code kret;
101 time_t now;
102 OM_uint32 left;
103 const char *realm;
104
105 if (cred_usage == GSS_C_ACCEPT) {
106 /*
107 * TODO: Here we should eventually support user2user (when we get
108 * support for that via an extension to the mechanism
109 * allowing for more than two security context tokens),
110 * and/or new unique MEMORY keytabs (we have MEMORY keytab
111 * support, but we don't have a keytab equivalent of
112 * krb5_cc_new_unique()). Either way, for now we can't
113 * support this.
114 */
115 *minor_status = ENOTSUP; /* XXX Better error? */
116 return GSS_S_FAILURE;
117 }
118
119 memset(&cred, 0, sizeof(cred));
120
121 if (handle->principal == NULL) {
122 kret = krb5_get_default_principal(context, &handle->principal);
123 if (kret)
124 goto end;
125 }
126 kret = krb5_get_init_creds_opt_alloc(context, &opt);
127 if (kret)
128 goto end;
129
130 realm = krb5_principal_get_realm(context, handle->principal);
131
132 krb5_get_init_creds_opt_set_default_flags(context, "gss_krb5", realm, opt);
133
134 /*
135 * Get the current time before the AS exchange so we don't
136 * accidentally end up returning a value that puts advertised
137 * expiration past the real expiration.
138 *
139 * We need to do this because krb5_cc_get_lifetime() returns a
140 * relative time that we need to add to the current time. We ought
141 * to have a version of krb5_cc_get_lifetime() that returns absolute
142 * time...
143 */
144 krb5_timeofday(context, &now);
145
146 kret = krb5_get_init_creds_password(context, &cred, handle->principal,
147 password, NULL, NULL, 0, NULL, opt);
148 krb5_get_init_creds_opt_free(context, opt);
149 if (kret)
150 goto end;
151
152 kret = krb5_cc_new_unique(context, krb5_cc_type_memory, NULL, &ccache);
153 if (kret)
154 goto end;
155
156 kret = krb5_cc_initialize(context, ccache, cred.client);
157 if (kret)
158 goto end;
159
160 kret = krb5_cc_store_cred(context, ccache, &cred);
161 if (kret)
162 goto end;
163
164 handle->cred_flags |= GSS_CF_DESTROY_CRED_ON_RELEASE;
165
166 ret = __gsskrb5_ccache_lifetime(minor_status, context, ccache,
167 handle->principal, &left);
168 if (ret != GSS_S_COMPLETE)
169 goto end;
170 handle->endtime = now + left;
171 handle->ccache = ccache;
172 ccache = NULL;
173 ret = GSS_S_COMPLETE;
174 kret = 0;
175
176 end:
177 if (ccache != NULL)
178 krb5_cc_destroy(context, ccache);
179 if (cred.client != NULL)
180 krb5_free_cred_contents(context, &cred);
181 if (ret != GSS_S_COMPLETE && kret != 0)
182 *minor_status = kret;
183 return (ret);
184 }
185
186 /*
187 * Acquires an initiator credential from a ccache or using a keytab.
188 */
189 static OM_uint32
190 acquire_initiator_cred(OM_uint32 *minor_status,
191 krb5_context context,
192 OM_uint32 time_req,
193 gss_const_OID desired_mech,
194 gss_cred_usage_t cred_usage,
195 gsskrb5_cred handle)
196 {
197 OM_uint32 ret = GSS_S_FAILURE;
198 krb5_creds cred;
199 krb5_get_init_creds_opt *opt;
200 krb5_principal def_princ = NULL;
201 krb5_ccache def_ccache = NULL;
202 krb5_ccache ccache = NULL; /* we may store into this ccache */
203 krb5_keytab keytab = NULL;
204 krb5_error_code kret = 0;
205 OM_uint32 left;
206 time_t lifetime = 0;
207 time_t now;
208
209 memset(&cred, 0, sizeof(cred));
210
211 /*
212 * Get current time early so we can set handle->endtime to a value that
213 * cannot accidentally be past the real endtime. We need a variant of
214 * krb5_cc_get_lifetime() that returns absolute endtime.
215 */
216 krb5_timeofday(context, &now);
217
218 /*
219 * First look for a ccache that has the desired_name (which may be
220 * the default credential name).
221 *
222 * If we don't have an unexpired credential, acquire one with a
223 * keytab.
224 *
225 * If we acquire one with a keytab, save it in the ccache we found
226 * with the expired credential, if any.
227 *
228 * If we don't have any such ccache, then use a MEMORY ccache.
229 */
230
231 if (handle->principal != NULL) {
232 /*
233 * Not default credential case. See if we can find a ccache in
234 * the cccol for the desired_name.
235 */
236 kret = krb5_cc_cache_match(context,
237 handle->principal,
238 &ccache);
239 if (kret == 0) {
240 kret = krb5_cc_get_lifetime(context, ccache, &lifetime);
241 if (kret == 0) {
242 if (lifetime > 0)
243 goto found;
244 else
245 goto try_keytab;
246 }
247 }
248 /*
249 * Fall through. We shouldn't find this in the default ccache
250 * either, but we'll give it a try, then we'll try using a keytab.
251 */
252 }
253
254 /*
255 * Either desired_name was GSS_C_NO_NAME (default cred) or
256 * krb5_cc_cache_match() failed (or found expired).
257 */
258 kret = krb5_cc_default(context, &def_ccache);
259 if (kret != 0)
260 goto try_keytab;
261 kret = krb5_cc_get_lifetime(context, def_ccache, &lifetime);
262 if (kret != 0)
263 lifetime = 0;
264 kret = krb5_cc_get_principal(context, def_ccache, &def_princ);
265 if (kret != 0)
266 goto try_keytab;
267 /*
268 * Have a default ccache; see if it matches desired_name.
269 */
270 if (handle->principal == NULL ||
271 krb5_principal_compare(context, handle->principal,
272 def_princ) == TRUE) {
273 /*
274 * It matches.
275 *
276 * If we end up trying a keytab then we can write the result to
277 * the default ccache.
278 */
279 if (handle->principal == NULL) {
280 kret = krb5_copy_principal(context, def_princ, &handle->principal);
281 if (kret)
282 goto end;
283 }
284 if (ccache != NULL)
285 krb5_cc_close(context, ccache);
286 ccache = def_ccache;
287 def_ccache = NULL;
288 if (lifetime > 0)
289 goto found;
290 /* else we fall through and try using a keytab */
291 }
292
293 try_keytab:
294 if (handle->principal == NULL) {
295 /* We need to know what client principal to use */
296 kret = krb5_get_default_principal(context, &handle->principal);
297 if (kret)
298 goto end;
299 }
300 kret = get_keytab(context, &keytab);
301 if (kret)
302 goto end;
303
304 kret = krb5_get_init_creds_opt_alloc(context, &opt);
305 if (kret)
306 goto end;
307 krb5_timeofday(context, &now);
308 kret = krb5_get_init_creds_keytab(context, &cred, handle->principal,
309 keytab, 0, NULL, opt);
310 krb5_get_init_creds_opt_free(context, opt);
311 if (kret)
312 goto end;
313
314 /*
315 * We got a credential with a keytab. Save it if we can.
316 */
317 if (ccache == NULL) {
318 /*
319 * There's no ccache we can overwrite with the credentials we acquired
320 * with a keytab. We'll use a MEMORY ccache then.
321 *
322 * Note that an application that falls into this repeatedly will do an
323 * AS exchange every time it acquires a credential handle. Hopefully
324 * this doesn't happen much. A workaround is to kinit -k once so that
325 * we always re-initialize the matched/default ccache here. I.e., once
326 * there's a FILE/DIR ccache, we'll keep it frash automatically if we
327 * have a keytab, but if there's no FILE/DIR ccache, then we'll
328 * get a fresh credential *every* time we're asked.
329 */
330 kret = krb5_cc_new_unique(context, krb5_cc_type_memory, NULL, &ccache);
331 if (kret)
332 goto end;
333 handle->cred_flags |= GSS_CF_DESTROY_CRED_ON_RELEASE;
334 } /* else we'll re-initialize whichever ccache we matched above */
335
336 kret = krb5_cc_initialize(context, ccache, cred.client);
337 if (kret)
338 goto end;
339 kret = krb5_cc_store_cred(context, ccache, &cred);
340 if (kret)
341 goto end;
342
343 found:
344 assert(handle->principal != NULL);
345 ret = __gsskrb5_ccache_lifetime(minor_status, context, ccache,
346 handle->principal, &left);
347 if (ret != GSS_S_COMPLETE)
348 goto end;
349 handle->endtime = now + left;
350 handle->ccache = ccache;
351 ccache = NULL;
352 ret = GSS_S_COMPLETE;
353 kret = 0;
354
355 end:
356 if (ccache != NULL) {
357 if ((handle->cred_flags & GSS_CF_DESTROY_CRED_ON_RELEASE) != 0)
358 krb5_cc_destroy(context, ccache);
359 else
360 krb5_cc_close(context, ccache);
361 }
362 if (def_ccache != NULL)
363 krb5_cc_close(context, def_ccache);
364 if (cred.client != NULL)
365 krb5_free_cred_contents(context, &cred);
366 if (def_princ != NULL)
367 krb5_free_principal(context, def_princ);
368 if (keytab != NULL)
369 krb5_kt_close(context, keytab);
370 if (ret != GSS_S_COMPLETE && kret != 0)
371 *minor_status = kret;
372 return (ret);
373 }
374
375 static OM_uint32
376 acquire_acceptor_cred(OM_uint32 * minor_status,
377 krb5_context context,
378 OM_uint32 time_req,
379 gss_const_OID desired_mech,
380 gss_cred_usage_t cred_usage,
381 gsskrb5_cred handle)
382 {
383 OM_uint32 ret;
384 krb5_error_code kret;
385
386 ret = GSS_S_FAILURE;
387
388 kret = get_keytab(context, &handle->keytab);
389 if (kret)
390 goto end;
391
392 /* check that the requested principal exists in the keytab */
393 if (handle->principal) {
394 krb5_keytab_entry entry;
395
396 kret = krb5_kt_get_entry(context, handle->keytab,
397 handle->principal, 0, 0, &entry);
398 if (kret)
399 goto end;
400 krb5_kt_free_entry(context, &entry);
401 ret = GSS_S_COMPLETE;
402 } else {
403 /*
404 * Check if there is at least one entry in the keytab before
405 * declaring it as an useful keytab.
406 */
407 krb5_keytab_entry tmp;
408 krb5_kt_cursor c;
409
410 kret = krb5_kt_start_seq_get (context, handle->keytab, &c);
411 if (kret)
412 goto end;
413 if (krb5_kt_next_entry(context, handle->keytab, &tmp, &c) == 0) {
414 krb5_kt_free_entry(context, &tmp);
415 ret = GSS_S_COMPLETE; /* ok found one entry */
416 }
417 krb5_kt_end_seq_get (context, handle->keytab, &c);
418 }
419 end:
420 if (ret != GSS_S_COMPLETE) {
421 if (handle->keytab != NULL)
422 krb5_kt_close(context, handle->keytab);
423 if (kret != 0) {
424 *minor_status = kret;
425 }
426 }
427 return (ret);
428 }
429
430 OM_uint32 GSSAPI_CALLCONV _gsskrb5_acquire_cred
431 (OM_uint32 * minor_status,
432 gss_const_name_t desired_name,
433 OM_uint32 time_req,
434 const gss_OID_set desired_mechs,
435 gss_cred_usage_t cred_usage,
436 gss_cred_id_t * output_cred_handle,
437 gss_OID_set * actual_mechs,
438 OM_uint32 * time_rec
439 )
440 {
441 OM_uint32 ret;
442
443 if (desired_mechs) {
444 int present = 0;
445
446 ret = gss_test_oid_set_member(minor_status, GSS_KRB5_MECHANISM,
447 desired_mechs, &present);
448 if (ret)
449 return ret;
450 if (!present) {
451 *minor_status = 0;
452 return GSS_S_BAD_MECH;
453 }
454 }
455
456 ret = _gsskrb5_acquire_cred_ext(minor_status,
457 desired_name,
458 GSS_C_NO_OID,
459 NULL,
460 time_req,
461 GSS_KRB5_MECHANISM,
462 cred_usage,
463 output_cred_handle);
464 if (ret)
465 return ret;
466
467
468 ret = _gsskrb5_inquire_cred(minor_status, *output_cred_handle,
469 NULL, time_rec, NULL, actual_mechs);
470 if (ret) {
471 OM_uint32 tmp;
472 _gsskrb5_release_cred(&tmp, output_cred_handle);
473 }
474
475 return ret;
476 }
477
478 OM_uint32 GSSAPI_CALLCONV _gsskrb5_acquire_cred_ext
479 (OM_uint32 * minor_status,
480 gss_const_name_t desired_name,
481 gss_const_OID credential_type,
482 const void *credential_data,
483 OM_uint32 time_req,
484 gss_const_OID desired_mech,
485 gss_cred_usage_t cred_usage,
486 gss_cred_id_t * output_cred_handle
487 )
488 {
489 krb5_context context;
490 gsskrb5_cred handle;
491 OM_uint32 ret;
492
493 cred_usage &= GSS_C_OPTION_MASK;
494
495 if (cred_usage != GSS_C_ACCEPT && cred_usage != GSS_C_INITIATE &&
496 cred_usage != GSS_C_BOTH) {
497 *minor_status = GSS_KRB5_S_G_BAD_USAGE;
498 return GSS_S_FAILURE;
499 }
500
501 GSSAPI_KRB5_INIT(&context);
502
503 *output_cred_handle = GSS_C_NO_CREDENTIAL;
504
505 handle = calloc(1, sizeof(*handle));
506 if (handle == NULL) {
507 *minor_status = ENOMEM;
508 return GSS_S_FAILURE;
509 }
510
511 HEIMDAL_MUTEX_init(&handle->cred_id_mutex);
512
513 if (desired_name != GSS_C_NO_NAME) {
514 ret = _gsskrb5_canon_name(minor_status, context,
515 desired_name, &handle->principal);
516 if (ret) {
517 HEIMDAL_MUTEX_destroy(&handle->cred_id_mutex);
518 free(handle);
519 return ret;
520 }
521 }
522
523 if (credential_type != GSS_C_NO_OID &&
524 gss_oid_equal(credential_type, GSS_C_CRED_PASSWORD)) {
525 /* Acquire a cred with a password */
526 gss_const_buffer_t pwbuf = credential_data;
527 char *pw;
528
529 if (pwbuf == NULL) {
530 HEIMDAL_MUTEX_destroy(&handle->cred_id_mutex);
531 free(handle);
532 *minor_status = KRB5_NOCREDS_SUPPLIED; /* see below */
533 return GSS_S_CALL_INACCESSIBLE_READ;
534 }
535
536 /* NUL-terminate the password, if it wasn't already */
537 pw = strndup(pwbuf->value, pwbuf->length);
538 if (pw == NULL) {
539 HEIMDAL_MUTEX_destroy(&handle->cred_id_mutex);
540 free(handle);
541 *minor_status = krb5_enomem(context);
542 return GSS_S_CALL_INACCESSIBLE_READ;
543 }
544 ret = acquire_cred_with_password(minor_status, context, pw, time_req,
545 desired_mech, cred_usage, handle);
546 free(pw);
547 if (ret != GSS_S_COMPLETE) {
548 HEIMDAL_MUTEX_destroy(&handle->cred_id_mutex);
549 krb5_free_principal(context, handle->principal);
550 free(handle);
551 return (ret);
552 }
553 } else if (credential_type != GSS_C_NO_OID) {
554 /*
555 * _gss_acquire_cred_ext() called with something other than a password.
556 *
557 * Not supported.
558 *
559 * _gss_acquire_cred_ext() is not a supported public interface, so
560 * we don't have to try too hard as to minor status codes here.
561 */
562 HEIMDAL_MUTEX_destroy(&handle->cred_id_mutex);
563 free(handle);
564 *minor_status = ENOTSUP;
565 return GSS_S_FAILURE;
566 } else {
567 /*
568 * Acquire a credential from the background credential store (ccache,
569 * keytab).
570 */
571 if (cred_usage == GSS_C_INITIATE || cred_usage == GSS_C_BOTH) {
572 ret = acquire_initiator_cred(minor_status, context, time_req,
573 desired_mech, cred_usage, handle);
574 if (ret != GSS_S_COMPLETE) {
575 HEIMDAL_MUTEX_destroy(&handle->cred_id_mutex);
576 krb5_free_principal(context, handle->principal);
577 free(handle);
578 return (ret);
579 }
580 }
581 if (cred_usage == GSS_C_ACCEPT || cred_usage == GSS_C_BOTH) {
582 ret = acquire_acceptor_cred(minor_status, context, time_req,
583 desired_mech, cred_usage, handle);
584 if (ret != GSS_S_COMPLETE) {
585 HEIMDAL_MUTEX_destroy(&handle->cred_id_mutex);
586 krb5_free_principal(context, handle->principal);
587 free(handle);
588 return (ret);
589 }
590 }
591 }
592 ret = gss_create_empty_oid_set(minor_status, &handle->mechanisms);
593 if (ret == GSS_S_COMPLETE)
594 ret = gss_add_oid_set_member(minor_status, GSS_KRB5_MECHANISM,
595 &handle->mechanisms);
596 if (ret != GSS_S_COMPLETE) {
597 if (handle->mechanisms != NULL)
598 gss_release_oid_set(NULL, &handle->mechanisms);
599 HEIMDAL_MUTEX_destroy(&handle->cred_id_mutex);
600 krb5_free_principal(context, handle->principal);
601 free(handle);
602 return (ret);
603 }
604 handle->usage = cred_usage;
605 *minor_status = 0;
606 *output_cred_handle = (gss_cred_id_t)handle;
607 return (GSS_S_COMPLETE);
608 }